Chapter 3 Computer Programmingodp
Chapter 3 Computer Programmingodp
[ECEg-1052]
Chapter Three:
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 expression False
{ ?
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.*/ Sample output 1:
#include <iostream> Enter a number: 3
else
{ statement block statement block
statement block 2; 1 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";
Exercise
Write a program that find whether the given number is even or odd. 11
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";
}
12
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
13
The while loop
Format:
while(expression)
statements;
While
(expression) statements
? True
False
Out of loop
14
Cont’d...
Example: i=2;
/* Find the sum of even while (i <= n){
numbers between 1 to n */ sum = sum + i;
i = i + 2;
#include <iostream>
}
using namespace std;
cout<<"sum= "<<sum;
int main(){ }
int i, n, sum=0;
cout<<"enter the end number:";
cin>>n;
15
The do-while loop
do
Format:
do
statements; statements
while (condition);
Condition
?
True
False
Out of loop
16
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
17
The for loop
Its format is:
for (initialization; condition; increase/decrease)
statement;
Following steps are involved in the code:
18
Cont’d...
initialization
False Condition
?
Out of loop
True
statement
increase/decrease
19
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.
20
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;
}
} 21
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.
22
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
23
Cont’d...
25
Cont’d...
while (---------)
{
---------
---------
if (condition)
continue;
---------
---------
}
--------
26
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;
}
27
Cont’d...
// example Output:
#include <iostream> 10, 9, 8, 7, 6, 4, 3, 2, 1, End!
using namespace std;
int main (){
for (int n=10; n>0; n--) {
if (n==5)
continue;
cout << n << ", ";
}
cout << "End!";
return 0;
} 28
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 (:).
29
Cont’d...
// goto loop example Output:
#include <iostream> 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, End!
using namespace std;
int main (){
int n=10;
loop:
cout << n << ", ";
n--;
if (n>0)
goto loop;
cout << "End!";
return 0;
}
30
The exit function
A function defined in cstdlib(stdlib.h) library.
The purpose of exit is to terminate the running program with an specific
exit code.
Its prototype is:
void exit (int exit code);
if exit code= 0 means that the program finished normally.
31
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!";
return 0; Output:
} 10, 9, 8, 7, 32
The selective Structure: switch
General form: Inclusion of break at the end of each
switch (expression) block is necessary because if, for
{ example, we did not include it after
case constant1: block of instructions 1 the program
block of instructions 1; would not jump to the end of the
break; switch selective block (}) and
case constant2: continue to execute the rest of the
block of instructions 2; blocks of instructions until the first
break; appearance of break.
…
default:
default block of instructions;
} 33
Cont’d...
Example 1: Example 2:
switch (x) { switch (x) {
case 1: case 1:
cout << "x is 1"; case 2:
break;
case 3:
case 2:
cout << "x is 1, 2 or 3";
cout << "x is 2";
break; break;
default: default:
cout << "value of x unknown"; cout << "x is not 1, 2 nor 3";
} } 34
Thank You !