Lecture 6 GÇô Decision Making and Branching
Lecture 6 GÇô Decision Making and Branching
2
Decision Making and Branching
• Sequential statements
• Branching
• Decision-making statements:
– if statement
– switch statement
– Conditional operator statement (?)
• Looping / Repetition 3
Decision Making with if Statement
• The if statement is a powerful decision making
statement.
• It is basically a two-way decision statement
• It takes the following form:
if (test expression)
• Simple if statement
• if...else statement
• Nested if...else statement
• else if ladder
5
Simple if Statement
• The general form of a simple if statement is:
if (test expression)
{
statement-block;
}
statement-x;
6
Simple if Statement
Test True
Expression
Statements
False
Statement-x
7
Simple if Statement
• The statement-block may be a single statement or a
group of statements.
8
Example with Simple if Statement
• A program to check whether the value of a is greater
than b.
#include<iostream>
using namespace std;
int main()
{
int a = 150;
int b = 100;
if(a>b)
{
cout<<"Value of A is greater than B";
}
cout<<"\nThis is the end of the program";
}
9
The if…else Statement
• The if...else statement is an extension of the simple if
statement.
• The general form is:
if (test expression)
{
True-block statement(s);
}
else
{
False-block statement(s);
}
statement-x;
10
The if…else Statement
Test True
False
Expression
Statements Statements
Statement-x
11
The if…else Statement
• If the test expression is true, then the true-
block of statement(s), immediately following
the if statements are executed; otherwise the
false-block of statement(s) are executed.
12
Example: if-else Statement
• A program to find the greater number from two
numbers
#include<iostream>
using namespace std;
int main()
{
int a, b;
cout<<"Enter a value for A ";
cin>>a;
cout<<"Enter a value for B ";
cin>>b;
if(a>b)
cout<<"Value of A is greater than B";
else
cout<<"Value of B is greater than A";
} 13
Nesting of if…else Statements
if (test condition1)
{
if (test condition 2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
if(condition 3)
{
statement-3;
}
else
{
statement-4
}
}
statement-x;
14
Nesting of if…else Statements
• If the condition-1 is true, it will check the condition-2
and if this condition is also true, it will execute the
statement1.
• If the condition-2 is false, statement 2 will be
executed.
• If condition-1 is false it will check the condition-3
and if the result of the condition is true, statement 3
will be executed, otherwise statement 4 will be
executed.
• statement-x will be executed after executing any
statement of the if structure. .
15
Nesting of if…else Statements
Statement-x
16
Example: Nested if Statements
int main()
{
int a, b, c;
cout<<"Enter the values of a, b and c ";
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
{
cout<<"Value of A is greater than B or C";
}
else
{
cout<<"Value of C is greater than A or B";
}
}
else
{
if(b>c)
{
cout<<"Value of B is greater than A or C";
}
else
{
cout<<"Value of C is greater than A or B";
}
}
}
17
The else if Ladder
• The general format:
if (condition 1)
{
statement 1 ;
}
else if (condition 2)
{
statement 2;
}
else if (condition n)
{
statement n;
}
else
{
default statement;
}
statement x;
18
The else if Ladder
• This if structure is known as the else if ladder.
• The conditions are evaluated from the top to
downwards.
True False
CD-1
Statement 1
True False
CD-2
Statement 2
True False
CD-n
Statement -x
20
An Example of else if ladder
int main(){ else if(marks>64)
float marks; {
cout<<"Enter the marks"; cout<<"Credit";
cin>>marks; }
if(marks>84) else if(marks>49)
{ {
cout<<“HD"; cout<<"Pass";
} }
else if(marks>74) else
{ {
cout<<"Distinction"; cout<<"Fail";
} }
21
The Switch Statement
• Multiple conditions with if statement will increase the
complexity of the program
Expression = value 1
Block-1
Expression = value-n
Block-n
Statement-x
24
The Switch Statement
• The expression used in the switch statement should
result an integer value.
25
The Switch Statement
• On execution of the keyword switch, the value of the
expression is successively compared against the
values value1, value2, value-n.
• If the value of the expression for a particular case
matches with a case value, then the block of
statements that follow the case are executed.
• The break statement at the end of each block signal
the end of a particular case and causes an exit from
the switch statement.
• The default is an optional case. When present, it will
be executed if the expression does not match any of
the case values.
26
The Switch Statement
• If default statement is not present no action takes
place if all matches fails and the control goes to the
statement-x.
• The break statement causes an immediate exit from
the switch statement.
• As the default case is placed at the end of the case
statement it is not necessary to include a break
statement there.
• It is advisable to use the break statement in the
default block.
27
An Example of Switch Statement
int main() case 5:
{ cout<<"Wednesday";
int day; break;
cout<<"Enter a number case 6:
between 1 to 7 - "; cout<<"Thursday";
cin>>day; break;
switch(day) case 7:
{ cout<<"Friday";
case 1: break;
cout<<"Saturday"; default:
break; cout<<"Please enter
case 2: proper value";
cout<<"Sunday"; break;
break; }
case 3: }
cout<<"Monday";
break;
case 4:
cout<<"Tuesday";
break;
28
Conditional Operator and if: Example
#include<iostream> #include<iostream>
using namespace std;
using namespace std; int main()
int main() {
{ int n = 48, m = 65;
int x;
int n = 48, m = 65; if(n<m)
int x; x = n + 2;
x = (n<m)? n + 2 : m * 4; else
x = m * 4;
cout<<"Value of x = "<<x; cout<<"Value of x = "<<x;
} }
29
Output?
#include<iostream>
using namespace std;
int main(){
int a=100;
if(a>10)
cout<<‘A’;
else if(a>20)
cout<<‘B’;
else if(a>30)
cout<<‘C’;
} A
30
Output?
#include<iostream>
using namespace std;
int main(){
int x=-1,y=-1;
if(++x=++y)
cout<<‘A’;
else
cout<<‘B’;
} B
31
Output?
#include <iostream>
using namespace std;
int main(){
int a=5,b=10;
if(++a||++b)
cout<<a<<endl<<b;
else
cout<<'WRONG';
}6, 10
32
Output?
int main()
{
int x;
cout<<"Enter a value ";
cin>>x;
switch(x)
{
case 1:
cout<<“ONE";
case 2:
cout<<“TWO";
case 3:
cout<<“THREE";
default:
cout<<“WRONG";
break;
}
33
THANK YOU
34