0% found this document useful (0 votes)
14 views34 pages

Lecture 6 GÇô Decision Making and Branching

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views34 pages

Lecture 6 GÇô Decision Making and Branching

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Lecture 6

Decision Making and Branching


Objectives
• Explain simple if statement
• Explain if …else statement with flowcharts
• Discuss nested if …else statement with
flowcharts
• Clarify else…if ladder with example
• Explain the switch statement with example
• Compare conditional operator with if
statement

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)

• It evaluates the expression first and then,


depending on the result of the expression, it
transfers the control to a particular statement(s).

• This point of program has two paths to follow,


one for the true condition and the other for the
false condition.
4
Different Forms of if Statements
• The if statement may be implemented in
different forms depending on the complexity
of the conditions to be tested.

• 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.

• If the test expression is true, the statement-block will


be executed

• If the test expression is false the statement-x will be


executed.

• When the condition is true both the statement-block


and the statement-x are executed in sequence.

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.

• In either case, either true-block or false-block


will be executed, but not both.

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

False Test True


condition 1

False Test True False Test True


condition 3 condition 2

Statement 4 Statement 3 Statement 2 Statement 1

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.

• As soon as a true condition is found, the statement


associated with it is executed and the control is
transferred to the statement-x, skipping the rest of the
statements.

• When all the conditions become false, then the final


else containing the default statement will be executed.
19
The else if Ladder

True False
CD-1

Statement 1
True False
CD-2

Statement 2
True False
CD-n

Statement-n Default Statement

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

• If there are multiple tests to be conducted, use switch


statement
• This will increase the clarity of the program

• The switch statement tests the value of a given


variable (or expression) against a list of case values

• If a match is found, it will execute that block of


statements.
22
The Switch Statement
switch (expression)
{
case value1:
block1
break;
case value2:
block2
break;
case value-n:
block-n
break;
default:
default-block
break;
}
statement-x;
23
The Switch Statement
Expression

Expression = value 1
Block-1

Expression = value 2 Block-2

Expression = value-n
Block-n

Expression = default value


Default block

Statement-x
24
The Switch Statement
• The expression used in the switch statement should
result an integer value.

• value1, value2, .... are constants or constant


expressions and are known as case labels.

• Each of these values should be unique within a switch


statement. block1, block2,.. are statement lists and
may contain zero or more statements.

• There is no need to put braces around these blocks.


Note that case labels end with a colon.

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

You might also like