Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Conditional Statement

Yongseok Son
Department of Computer Science and Engineering
Chung-Ang University
Control Structure
❖ Conditional statement: if, switch
▪ Determine a block of statements to execute depending on
whether the condition is true or false

❖ Repetition statement: for, while, do-while


▪ Loop: repeat a block of statements a number of times
▪ Conditional loop: repeat while the condition is true

❖ Other control structures: goto

slide 2
Conditional Statement
❖ Conditional statements
▪ Conditional statements helps you to make decision based on
certain conditions
▪ These conditions are specified by a set of conditional
statements having boolean expressions which are evaluated
to a boolean value true or false
▪ They allow us to check a condition and execute certain parts
of code depending on whether the condition is true or false
• For example
✓ If statement
✓ If-else statement
✓ If-else if statement
✓ Switch statement

slide 3
if statement
❖ It is used to execute the code if condition is true
▪ If the expression is evaluated to nonzero (true) then if
block statement(s) are executed
▪ If the expression is evaluated to zero (false) then control
passes to next statement following it

if False
condition
if (expression)
{
True //code to be executed
}
Code to be executed

slide 4
if statement
❖ Example of if statement

...
if (num1 >= num2)
diff = num1 – num2;
...

true
num1 >= num2 diff = num1 – num2

false

slide 5
Compound Statement
❖ Compound statements are made up of two or more
statements that are executed together
▪ This usually occurs while handling conditions where a
series of statements are executed when a TRUE or FALSE
is evaluated
▪ Block { } are placed before and after compound statements

❖ Example
if ( num1 >= num2 ){
printf(“num1 is greater than num2\n“);
printf(“The difference is: %d\n”, num1- num2);
} else {
printf(“num2 is greater than or equal to num1\n”);
printf(“The difference is: %d\n”, num2 – num1);
}

slide 6
if-else statement
❖ The if-else statement is used to execute the corresponding
code if condition is true or false
▪ It is also called two way selection statement
▪ If the expression is evaluated to nonzero (true) if block statement(s)
are executed
▪ If the expression is evaluated to zero (false) then else block
statement(s) are executed

if (expression){ False
if

condition
//code to be executed
… True
}
else{ <if block> <else block>
… Code to be executed Code to be executed
//code to be executed

}

slide 7
if-else statement
❖ Example of if-else statement
...
if (num1 >= num2)
diff = num1 – num2;
else
diff = num2 – num1;
...

true
num1 >= num2 diff = num1 – num2
?

false

diff = num2 – num1

slide 8
if-else if statement
❖ The if else-if statement is used to execute one code from multiple
conditions
▪ It is also called multipath decision statement
▪ It is a chain of if..else statements in which each if statement is
associated with else if statement and last would be an else statement

if(condition1) if True
{ condition
<if block>
//statements False Code to be executed
}
else if(condition2) else if True
{ condition
//statements <else if block>
False Code to be executed
}
else if(condition3) else if True
{ condition
//statements False <else if block>
Code to be executed
}
else else block
{
//statements
}

slide 9
if-else if statement
❖ Example of if-else if statement
#include <stdio.h>
int main(void)
{
int grade;
printf(“enter your grade: ”);
scanf(“%d”, &grade);

if ( grade >= 90 ) // 90 and above


printf(“A”);
else if ( grade >= 80 ) // 80-89
printf(“B”);
else if ( grade >= 70 ) // 70-79
printf(“C”);
else if ( grade >= 60 ) // 60-69
printf(“D”);
else // less than 60
printf(“F”);
return 0;
}

slide 10
Switch statement
❖ Switch statement is a type of selection control mechanism
used to allow the value of a variable or expression to
change the control flow of program execution
❖ A switch statement allows a variable to be tested for
equality against a list of values
▪ Each value is called a case, and the variable being switched
is checked for each switch case
❖ Switch statement acts as a substitute for if-else-if
statement that is used to test a list of cases

slide 11
Switch statement
❖ The switch statement executes according to the
following rules:
▪ When the value of the expression is matched against a
case value, the statements execute until either a break
statement is found or the end of the switch structure is
reached
▪ If the value of the expression does not match any of the
case values, the statements in following the default label
are executed
▪ A break statement causes an immediate exit from the
switch structure

slide 12
Switch statement
❖ Example
#include <stdio.h>
int main () {
char grade = 'B';

switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
printf(“Good\n" );
break; <Result>
case 'C' : Good
printf("Well done\n" ); Your grade is B
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}

printf("Your grade is %c\n", grade);


return 0;
}

slide 13
Switch statement
❖ Example
#include <stdio.h>

int main () {
char grade = 'B';

switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' : <Result>
printf("Well done\n" ); Well done
Your grade is B
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}

printf("Your grade is %c\n", grade);


return 0;
}

slide 14
Switch statement
❖ Example
#include <stdio.h>

int main () {
char grade = ‘S’;

switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' : <Result>
printf("Well done\n" ); Invalid grade
Your grade is S
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}

printf("Your grade is %c\n", grade);


return 0;
}

slide 15
Switch statement

Switch
Expression

case 1 Case 1 block

case 2 Case 2 block

case 3 Case 3 block

default
Default block

slide 16
Switch statement
❖ A rule of switch statement
switch(number)
{
case x: //Variables cannot be used
printf(“…");
break;
case (x+2): //An expression containing variables cannot be used
printf(“…");
break;
case 0.001: //floating-point cannot be used
printf(“…");
break;
case “001”: //character string cannot be used
printf(“…");
break;
}

slide 17
Switch and if-else if statements

int main(void) switch(number)


{ {
int number; case 0:
scanf("%d", &number); printf(“nothing\n");
break;
case 1:
if( number == 0 ) printf(“one\n");
printf(“nothing\n"); break;
else if( number == 1 ) case 2:
printf(“one\n"); printf(“two\n");
else if( number == 2 ) break;
printf(“two\n"); default:
else printf(“many\n");
printf(“many\n"); break;
} }

slide 18
Assertion
❖ Assertions are statements used to test assumptions
made by programmer
▪ For example, we may use assertion to check if pointer
returned by malloc() is NULL or not

assert(expression);

▪ If expression evaluates to 0 (false), then the expression,


source code filename, and line number are sent to the
standard error, and then abort() function is called

slide 19
Assertion
❖ Example of Assertion

#include <stdio.h>
#include <assert.h>

int main()
{
int x = 7;

/* Some big code in between and let's say x


is accidentally changed to 9 */
x = 9;

// Programmer assumes x to be 7 in rest of the code


assert(x==7);

return 0;
}

slide 20

You might also like