Chapter 4
Chapter 4
[ECEg-1052]
Chapter Four:
Control Statements
3
4.2. Conditional Structure: if and else
It is used to execute an instruction or block of
instructions only if a condition is fulfilled.
if can be used in different forms depending upon
nature of conditional test.
1. if
2. if…else
3. Nested – if
4
Simple if statement
General form: Entry
if (test expression)
Test False
{ expression
?
statement-block;
True
} Statement-block
statement-x;
Statement-x
5
Cont’d...
The ‘statement-block’ may be a single statement or a
group of statements.
If the test expression is true, the statement-block is
executed.
If it is false, statement-block is ignored (not executed)
and the program continues on the next instruction
after the conditional structure.
6
Cont’d...
\* To display number is positive.*/
#include <iostream> Sample output 1:
using namespace std; Enter a number: 3
int main(){ number is positive
int num; Sample Output 2:
Enter a number:-3
cout<<"Enter a number:";
cin>>num;
if (num>0)
cout<<" number is positive ";
return 0;
} 7
Cont’d...
For a block of instructions, we use curly brackets { }:
Example:
if (x == 100){
cout << "x is ";
cout << x;
}
But for a single, we can ignore {}.
Example:
if (x == 100)
cout << "x is 100";
8
The if – else statement
Form:
Entry
if (condition)
{ True False
Condition
statement block 1; ?
}
else statement statement
block 1 block 2
{
statement block 2;
}
statement-x; statement-x
9
Cont’d...
For example:
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
10
Nested if statement
When a series of decisions are involved, use more
than one if…else statement.
Example:
if (x>0)
cout<<"The given number is +ve"<<endl;
else if (x<0)
cout<<"The given number is negative"<<endl;
else
cout<<"The given number is zero";
11
Cont’d...
Exercise
1.Write a program that find whether the given number
is even or odd.
12
Cont’d...
Solution
// To find whether given number is even or odd.
#include<iostream>
using namespace std;
int main(){
int no;
cout<<"enter a number";
cin>>no;
if ( no %2 == 0 )
cout<<no<<"is an even number";
else
cout<<no<<"is an odd number";
}
13
4.2 Repetitive Structures or loops
Loops have as objective to repeat a statement a
certain number of times while a condition is
fulfilled.
The while loop
The do-while loop
The for loop
14
The while loop
Format:
while(expression)
statements;
While
(expression) statements
? True
False
Out of loop
15
Cont’d...
Example:
/* Find the sum of even i=2;
numbers between 1 to n */ while (i <= n){
#include <iostream> sum = sum + i;
i = i + 2;
using namespace std;
}
int main(){
cout<<"sum= "<<sum;
int i, n, sum=0; }
cout<<"enter the end number:";
cin>>n;
16
The do-while loop
do
Format:
do
statements; statements
while (condition);
Condition
?
True
False
Out of loop
17
Cont’d...
Example
sum = 0;
i = 1;
do{
sum += i;
i += 2;
} while (i <= 100);
cout<<“Sum of odd integers up to 100 is”<<sum;
Output:
Sum of odd integers up to 100 is 2500
18
The for loop
Its format is:
for (initialization; condition; increase/decrease)
statement;
Following steps are involved in the code:
19
Cont’d...
initialization
False Condition
?
Out of loop
True
statement
increase/decrease
20
Cont’d...
Example 1:
sum = 0;
for (i=0 ; i <= 100 ; i++)
sum += i;
Example 2: Write a program to list a number and its square in two
columns for integers 1 up to 10.
21
Cont’d...
#include<iostream>
using namespace std;
int main(){
for(int i=1;i<=10;i++) {
cout<<"The number is:"<<i;
int square=i*i;
cout<<" and its square is:"<<square;
cout<<endl;
}
}
22
4.3 Bifurcation of control and jumps.
The break instruction
The format of the break statement is simply
break;
Causes an immediate exit 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.
23
Cont’d...
Examples
i=1;
while (1){
cout<<i<<" ";
if (i==10)
break;
i=i+1;
}
Output: 1 2 3 4 5 6 7 8 9 10
24
Cont’d...
26
Cont’d...
while (---------)
{
---------
---------
if (condition)
continue;
---------
---------
}
--------
27
Cont’d...
// to add only positive numbers
#include<iostream>
using namespace std;
int main(){
int i, n, sum=0;
cout<<"enter any 10 numbers\n";
for(i=1;i<=10;i++) {
cin>>n;
if (n==7)
continue;
sum += n;
}
cout << " sum of +ve numbers = "<<sum;
}
28
Cont’d...
// example
#include <iostream> Output:
using namespace std; 10, 9, 8, 7, 6, 4, 3, 2, 1, End!
int main (){
for (int n=10; n>0; n--) {
if (n==5)
continue;
cout << n << ", ";
}
cout << "End!";
return 0;
} 29
The goto instruction
Allows making an absolute jump to another point in
the program.
The destination point is identified by a label, which is
then used as an argument for the goto instruction.
A label is made of a valid identifier followed by a
colon (:).
30
Cont’d...
32
Cont’d...
// exit function example
#include <iostream>
#include <cstdlib>
using namespace std;
int main (){
int n=10;
while(n>0) {
cout << n << ", ";
n--;
if (n==6)
exit(0);
}
cout <<"End!"; Output:
return 0; 10, 9, 8, 7,
} 33
The selective Structure: switch
General form:
switch (expression)
{
case constant1:
block of instructions 1;
break;
case constant2:
block of instructions 2;
break;
…
default:
default block of instructions;
}
34
Cont’d...
Inclusion of break at the end of each block is necessary
because if, for example, we did not include it after block of
instructions 1 the program would not jump to the end of the
switch selective block (}) and continue to execute the rest of
the blocks of instructions until the first appearance of break.
35
Cont’d...
Example 1:
switch (x) {
case 1:
cout << "x is 1";
break;
case 2:
cout << "x is 2";
break;
default:
cout << "value of x unknown";
} 36
Cont’d...
Example 2:
switch (x) {
case 1:
case 2:
case 3:
cout << "x is 1, 2 or 3";
break;
default:
cout << "x is not 1, 2 nor 3";
}
37
Thank You !
38