Chapter4-Conditional Statements
Chapter4-Conditional Statements
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
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
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);
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" );
}
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" );
}
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" );
}
slide 15
Switch statement
Switch
Expression
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
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);
slide 19
Assertion
❖ Example of Assertion
#include <stdio.h>
#include <assert.h>
int main()
{
int x = 7;
return 0;
}
slide 20