Chapter 4
Chapter 4
3/18/2022 2
Introduction
Flow control in a program is sequential, from one statement to the next,
but may be diverted to other paths by branch statements.
Jumping statements are used to divert the execution path to another part
of the program.
3
Conditional Statements
Selection statements are used for specifying alternate paths of
execution, depending on the outcome of a logical condition
if statement.
Nested if –else statement.
If-else statement.
Switch statement.
else-if statement.
3/18/2022 4
if statement
The if statement is used to express conditional expressions.
If the given condition is true then it will execute the statements otherwise skip
the statements.
If the expression is false the statements are skipped and execution continues with
6
if - else statement
It is a two way branching statement.
It is similar to if statement but the only difference is if the condition is
false then a different block of statements are executed which is inside else
statement.
Syntax of if-else statement
if (condition)
{
statements;
... ... ...
}
else
{
statements;
... ... ...
}
7
Cont…
#include<iostream.h>
int main () {
int n;
cout<<"Enter a Number: " ;
cin>>n;
if (n%2==0)
{
cout<<n<<" is Even!"; }
Else
{
cout<<n<<" is Odd!"; }
return 0;
}
8
else-if statements
It used when there are two or more conditions that needs to be
checked to decide which block of statement is to be executed.
Syntax:
if (exp1)
Statement1;
else if (exp>)
Statement2;
else if (exp3)
Statement3;
else
Statement4;
When a logical expression is encountered whose value is true the
corresponding statements will be executed and the remainder of the nested
else if statement will be bypassed.
3/18/2022 9
Cont...
Thus control will be transferred out of the entire nest
once a true condition is encountered.
3/18/2022 10
Cont. …
Syntax of else-if statement
if (condition 1) {
statements;
... ... ...
}
else if (condition 2) {
statements;
}
... ... ... ... ... ...
else if (condition n) {
statements;
... ... ...
}
else {
statements;
}
11
cont…
//else -if equivalent cout<<"x is 2";
#include<iostream.h> }
int main() else if(x ==3)
{ {
int x; cout<<"x is 3";
cout<<"Enter x:"; }
cin>>x; else
if (x == 1) {
{ cout << "value of x
cout << "x is 1"; unknown";
} }
else if(x ==2) return 0;
{ }
3/18/2022 12
Nested if-else statement
It is possible to nest if-else statements, one within another.
There are several different form that nested if-else statements can take.
The most general form of two-layer nesting is
3/18/2022 14
Con’d...
Syntax of nested if statement statements;
}
if (condition 1) statements;
{ }
statements; ... ... ...
if (sub condition 1) ... ... ...
{ else
statements; {
} statements;
statements; if (sub condition n)
} {
else if (condition 2) statements;
{ }
statements; statements;
if (sub condition 2) }
{
15
Cont...
3/18/2022 16
Cont...
//Demo to check even no. and divisible by 5
#include <iostream.h> else
int main() { {
int n; if (n%5 == 0)
cout<<"Enter a number: "; {
cin>>n; cout<<"Number is not even but
if (n%2 == 0) divisible by 5";
{ }
if (n%5 == 0)
else
{
{
cout<<"Number is even and divisible by 5";
cout<<"Number is not even and
}
not divisible by 5";
else {
cout<<"Number is even but not divisible by 5";
}
} }
} return 0;
} 17
Switch statement
The switch statement provides a way of choosing between a set of
alternatives, based on the value of an expression.
The general form of the switch statement is:
switch (expression) {
case Constant1:
statements;
break;
case Constant2:
statements;
break;
default:
statements;
break; }
18
Cont..
First expression (called the switch tag) is evaluated, and the
outcome is compared to each of the numeric constants (called case
labels), in the order they appear, until a match is found.
3/18/2022 19
Cond...
Switch(expression) {
case Constant1:
Statement 1;
Break;
Case Constant2:
Statement 2;
Break;
...
Case value n:
Statement n:
Break;
Default:
Statement n
}
3/18/2022 20
Cont...
Rules for Switch Statements
Values for 'case' must be integer, Boolean or character
constants.
3/18/2022 21
Cont...
//Switch Case example
#include<iostream.h> case 2:
int main() cout<<"x is 2";
{ break;
int x; default:
cout<<"Enter x:"; cout<<"value of x
cin>>x; unknown";
switch (x) { }
case 1: return 0;
cout<<"x is 1"; }
break;
3/18/2022 22
Cont...
//Switch Case example case 4:
#include<iostream.h> cout<<"the day is Wednesday ";
int main() { break;
int day; case 5:
cout<<"Enter the day number:"; cout<<"the day is Thursday";
cout<<endl; break;
cin>>day; case 6:
switch (day) cout<<"the day is Friday";
{ break;
case 1: case 7:
cout<<"the day is Sunday"; cout<<"the day is Saturday";
break; break;
case 2: default:
cout<<"the day is Monday"; cout<<"unknown value";
break; }
case 3: return 0;
cout<<"the day is Tuesday "; }
break;
3/18/2022 23
Looping Statements
Looping is a way of repeating a series of instructions several
times to execute a block of statements.
statement;
expression1;
while (expression2)
{
statement;
expression3;
}
3/18/2022 28
Cont…
The general form of the for statement is:
for(expression1;expression2; expression3)
statement;
3/18/2022 29
Cont…
The most common use of for loops is for situations where a variable is
sum += i;
3/18/2022 30
Cont …
Contrary to what may appear, the scope for i is not the body of the loop, but
the loop itself. Scope-wise, the above is equivalent to:
int i;
sum += i;
Any of the three expressions in a for loop may be empty. For example,
removing the first and the third expression gives us something identical to
a while loop:
statement1;
statment2;
3/18/2022 31
Cont …
Removing all the expressions gives us an infinite loop. This
loop's condition is assumed to be always true.
3/18/2022 33
..
Cont
For loops with multiple loop variables are not unusual.
In such cases, the comma operator is used to separate their expressions:
for (i = 0, j = 0; i + j < n; ++i, ++j)
something;
Because loops are statements, they can appear inside other loops.
3/18/2022 35
B. While Statement
The while statement (also called while loop) provides a way of
while (expression)
statement;
3/18/2022 37
Contd..
//While loop demo
#include<iostream.h>
int main()
{
int i=0;
int sum=0;
while(i<=10)
{
sum+=i;
i++;
}
cout<<"\n\t The Sum is "<<sum;
return 0;
}
3/18/2022 38
C. do…while Statement
The do statement is similar to the while statement, except that its body is
executed first and then the loop condition is examined.
The general form of the do statement is:
do
statement;
while (expression);
First statement is executed and then expression is evaluated. If the outcome
of the latter is nonzero then the whole process is repeated. Otherwise, the
loop is terminated.
It is useful for situations where we need the loop body to be executed at
least once, regardless of the loop condition.
3/18/2022 39
Cont…
#include<iostream>
int main() {
int n;
do {
cout<<"Enter the number "<<endl;
cin >> n;
cout << n * n << “\n”;
}
while (n != 0);
cout<<"End of the loop excuted!!!";
return 0;
}
3/18/2022 41
Cont …
#include<iostream.h>
int main() {
int counter=0; while(counter<n);
int sum=0; cout<<"the sum of the
int n;
number is"<<" "<<sum;
cout<<"enter the required number"<<" ";
cin>>n; cout<<"end of the do
do while loop";
{ return 0;
sum=sum + counter;
}
counter=counter+1;
}
3/18/2022 42
D. Nested Looping Statements
3/18/2022 43
…
Cont
#include<iostream>
int main()
{
int i;
int j; Output
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
55555
{ 4444
cout<<i; 333
} 22
cout<<endl; 1
}
return 0;
}
3/18/2022 44
Loop Summary
do...while loop: Like a while statement, except that it tests the condition
at the end of the loop body
nested loops: a way of using one or more loop inside any another while,
for or do…while loop.
3/18/2022 45
Cont …
#include <iostream.h>
void main ()
int x, answer;
do
if (answer == 2*x + 30 - x)
else
3/18/2022 46
Jumping Statements
Jump statements are used to divert the execution path to another part
of the program.
Jump statements cause an unconditional jump to another statement elsewhere in
the code.
The jump statements are the goto statement, the continue statement,
the break statement, and the return statement.
3/18/2022 47
A. Break Statement
Using break we can leave a loop even if the condition for its end is not fulfilled.
It can be used to end an infinite loop, or to force it to end before its natural end.
3/18/2022 49
B. Continue Statement
The continue statement terminates the current iteration of a loop and
instead jumps to the next iteration.
For example, we are going to skip the number 5 in our countdown:
#include <iostream>
int main ()
{
for (int n=10; n>0; n--) {
if (n==5) continue; 10, 9, 8, 7, 6, 4, 3, 2, 1,FIRE!
cout << n << ", ";
}
cout << "FIRE!\n";
return 0; }
50
C. goto statement
goto allows to make an absolute jump to another point in the program.
You should use this feature with caution since its execution causes an
unconditional jump ignoring any type of nesting limitations.
3/18/2022 51
//goto example
…
Cont
#include<iostream.h>
void main() {
cout << "\nStatement 1.";
cout << "\nStatement 2."; Output
cout << "\nStatement 3."; Statement 1.
Statement 2.
goto last; Statement 3. End of
cout << "\nStatement 4."; Program.