Chapter 3 Control Statment
Chapter 3 Control Statment
Programming
1
Chapter 3
Control Statement
2 Conditional Statements
The if Statement
It is sometimes desirable to make the execution of a statement
dependent upon a condition being satisfied.
The different forms of the ‘If” statement will be used to decide
whether to execute part of the program based on a condition
which will be tested either for TRUE or FALSE result.
The different forms of the “If” statement are:
The simple if statement
The If--- else statement
The if---else if---else statement
Nested if---else statement
3 Simple if Statements
General form or syntax: #include <iostream.h>
if (expression1) else
{ {
if (expression2) if (expression3)
statementsN; statementsR;
else else
statementsM; statementsT;
} }
8 Cont’d…
} while (continue-condition);
Example for do While
{ true
int age; Continue
cout<<"enter age"; condition?
do {
cin >> age;
cout << ++age << '\n'; false
}
while (age<=10); Next
return 0; Statement
}
14 3. For Loops
for(initialization, condition, increment/decrement)
{
statement;
}
initialization sets a loop control variable to an initial value.
condition is an expression that is tested each time the loop repeats.
The increment/decrement is an expression that determines how the loop control
variable is incremented each time the loop repeats.
Initial-Action
false
Action-After- Continuation
Each-Iteration condition?
true
Statement(s)
(loop-body)
Next
Statement
15 Example 1
int i;
for (i = 0; i < 10; i++) {
cout<<"Welcome to Programming! \n”;
} Output
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
Welcome to Programming!
16 Example 2
#include<iostream>
using namespace std;
int main()
{
for(int i=10;i>0;i--)
{ Output
cout<<i<<",";
10,9,8,7,6,5,4,3,2,1,FIRE!
}
cout<<"FIRE!";
return 0;
}
17 Example 3
#include<iostream>
using namespace std;
int main()
{
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; i++)
sum += i*i;
cout<< "The sum of the first " << n << " squares is "<< sum <<
endl;
return 0;
}
18 continue
The continue statement can be used in loops and has the opposite effect to break, that is,
the next loop is begun immediately.
In the case of a while or do-while loop the program jumps to the test expression, whereas
a for loop is reinitialized
Example:
#include<iostream>
using namespace std;
int main()
{
for( int i = 0; i < 50; i++ )
{
if( i == 30)
continue;
cout<<" The Value of i is:"<<i<<endl;
}
return 0;
}
19 Break Statement
The break statement exits from a switch or loop immediately.
You can use the break keyword to jump to the first statement that follows
the switch or loop.
Example: #include<iostream>
using namespace std;
int main()
{
for( int i = 0; i < 50; i++ )
{
if( i == 10)
break;
cout<<" The Value of i is:"<<i<<endl;
}
return 0;
}
20 Exercise
1. Write the program that executes the following program using for loop.
*********
*******
*****
***
*
2. Write the program that executes the following output on the screen
The square of 1 is = 1
The square of 2 is = 4
The square of 3 is = 9
The square of 4 is = 16
The square of 5 is = 25
The square of 6 is = 36
21 Thank You ...
?
System Analysis and Design