Chapter 3
Chapter 3
Conditional Statements
1
Chapter outcomes
• By the end of this chapter, the students will
• know how to create decision making statements using if ..
else statements
• know how to write decision making statements using switch
.. case statement
• be able to solve complex problems using the concepts
learned
2
Decision Control statement
• In decision control statements, a group of statements are
executed when the condition is true.
If (condition) {
Statement/s to be executed if
condition is true;
}
• This form has the advantage of brevity and is appropriate for simple
conditionals.
•
Examples with conditional operator
Example 1:
To assign the maximum of a and b to z: if (a>b)
z = (a>b) ? a : b; z = a;
which is the same as: else
Example 2: z=b;
9
Nested if
• The if...else statement can be used in nested form when
serious decisions are involved.
• This can be very useful if you want to test various types of
scenarios and situations.
• If the test condition is true, it will execute the code before else
block but, if it is false, the control of the program jumps to the
else block and check test condition 1 and the process
continues.
• If all the test expression are false then, the last statement is
executed.
10
• The ANSI standard specifies that 15 levels of nesting may be
continued.
Algorithm example
Algorithm Comparison
Var
n1,n2 : integer
Begin
1-write(“enter two integers to check”)
1- read(n1,n2)
2- if (n1=n2)
write (n1, “=”, n2)
else
if (n1>n2)
write (n1, “>”, n2)
else
write (n1, “<”, n2) 11
End
Equivalent in C
12
Notes
• If any action consists of more than one statement, it is enclosed within a
pair of braces.
if ( codition ) { statement1 statement2 }
• Opening and Closing braces are not required if there is only one
Statement after if.
• Opening and Closing braces are not required if there is only one
Statement after else.
• Block followed after if and before else is called “if block”
• We can write any number of statements inside block if condition is true
• if there is no statement after else then else can be dropped
• More than one Conditions can be Combined inside if
• Nested Control Statements are allowed
• "==" is a relational operator and "=" is an assignment operator.
• ! Operator Reveres the Value of Expression
13
• Logical && operator indicates “both Conditions must be TRUE”
• Logical || operator indicated “Either Conditions must be TRUE”
switch .. case statements
• If a programmer has to choose one among many alternatives,
if...else can be used but, this makes programming logic complex.
• This type of problem can be handled in C programming using
switch...case statement.
• Syntax:
switch (expression)
{
Note use of colon!
case constant1:
Instruction/s to be executed if expression equals to constant1;
break;
case constant2:
Instruction/s to be executed if expression equals to constant2;
break;
….
default: 14
Instruction/s to be executed if expression doesn't match to any cases;
}
switch .. case statements
Flowchart: :
15
Example 1:
Algorithm Appreciation
Var
grade : character
Begin
1. write( "Enter your current letter grade")
2. read(grade)
3. switch (grade)
{
case ('a') : case ('A') :
write ("Good Job!") ;
case ('b') : case ('B') :
write ("Pretty good.”) ;
case ('c') : case ('C') :
write ("Better get to work.") ;
case ('d') : case ('D') :
write ("You are in trouble.") ;
default :
write("You are failing!!") ; 16
}
End
Equivalent in C
#include <stdio.h>
int main ( )
{ •Input: ‘A’
char grade ;
printf ("Enter your current letter grade\n") ;
grade = getchar ( ) ;
switch (grade) • Output :
{
case ('a') : case ('A') :
Good Job!
printf ("Good Job!\n") ;
Pretty good.
case ('b') : case ('B') : Better get to work.
printf ("Pretty good.\n") ; You are in trouble.
case ('c') : case ('C') : You are failing!
printf ("Better get to work.\n") ;
case ('d') : case ('D') : Note: The input entered
printf ("You are in trouble.\n") ; matches the first case but
default : every action in the structure is
printf ("You are failing!!\n") ; executed. 17
} /* End of switch-case structure */
} /* End of main program
switch .. case statements : Notes
18
Use of break;
• The problems with the previous program can be
corrected by use of the break statement.
• It can be used in either a conditional structure or a
repetition structure to break out of (that is, to exit from)
the structure.
• The syntax is:
break ;
• The following program illustrates an example with the
addition of the break statements.
19
Example 1 using break statement
#include <stdio.h>
main ( )
{ char grade ;
printf ("Enter your current letter grade\n") ;
grade = getchar ( ) ; • Input: ‘A’
switch (grade)
{ • Output : Good Job!
case ('a') : case ('A') :
printf ("Good Job!\n") ;
break;
Begin
1. value ← 3
2. switch (value)
{
case 1 : write(" Value is 1”)
break
case 2 : write(" Value is 2”)
break
case 3 : write(" Value is 3”)
break
case 4 : write(" Value is 4”)
break
default
write(" Value is other than 1, 2, 3, 4”)
} 21
End
Equivalent in C
With switch ,, case statement With if .. else statement
22
int id = 3 ;
switch(id)
{
case 1:
printf("C Programming Language");
break;
case 2:
printf("C++ Programming Language");
break;
case 2:
printf("Web Technology");
break;
default :
printf("No student found");
break;
} 24
Rule 2 : Case Labels must end with Colon
case 1 :
printf("C Programming Language");
break;
Rule 3 : Case labels must be constants / constant
expression
case 1+1:
case 'A':
case 67:
=> These are allowed examples of switch case labels , however variables
are not allowed in switch case
case var : 25
case num1 :
case n1+n2 :
Rule 4 : Case label must be of integral Type (Integer,
Character)
case 'A':
case 'a':
case 10.12:
case 7.5:
26
Rule 5 : Switch case should have at most one default
label
switch(roll)
{
case 1:
printf("C Programming Language");
break;
case 2:
printf("C++ Programming Language");
break;
case 3:
printf("Web Technology");
break;
default :
printf("Default Version 1");
break;
default :
printf("Default Version 2"); 27
break;
}
Rule 6 : Default label is Optional
switch(roll)
{
case 1 :
printf("C Programming Language");
break;
case 2 :
printf("C++ Programming Language");
break;
case 3 :
printf("Web Technology");
break;
}
28
default statement is optional. It can be neglected.
Rule 7 : Default can be placed anywhere in the switch
switch(roll)
{
case 1 :
printf("C Programming Language");
break;
default:
printf("No Student Found");
break;
case 2 :
printf("C++ Programming Language");
break;
case 3 :
printf("Web Technology");
break; 29
}
Rule 8 : Break Statement takes control out of the switch
Rule 9 : Two or more cases may share one break statement
switch(alpha)
{
case 'a':
case 'A':
printf("Alphabet A");
break;
case 'b':
case 'B':
printf("Alphabet B");
break;
}
30
Rule 10 : Nesting ( switch within switch ) is allowed
switch(alpha)
{case 'a':
case 'A':
printf("Alphabet A");
break;
case 'b':
case 'B':
switch(beta)
{ …
}
break;
} 31
Rule 11 : Relational Operators are not allowed in
Switch Statement
switch(num)
{
case >15:
printf("Number > 15");
break;
case =15:
printf("Number = 15");
break;
case <15:
printf("Number < 15");
break; 32
}
Rule 12 : Const Variable is allowed in switch Case
Statement
switch(num)
{
case var:
printf("Number = 2");
break;
}
33
Exercise :
• Write an algorithm called MONTH, which displays the name of the month
corresponding to a month number entered by the user
Algorithm MONTH
Var
m:integer
Begin
1. write(" Enter a month number")
2. read(m)
3. switch (m)
{
Case 1 : write ( "January’’)
Case 2 : write ( " February ‘’)
…………..
Case 12 : write ( " December’’)
Default
write (‘’error ! ! ! ! ‘’) 34
}
End