Chapter Four Class 2015
Chapter Four Class 2015
Control Statements
Flow control in a program is the sequence of statements that are executed in a program depending on
the outcome of a logical condition. Control flow statements in software program are categorized into
selection statements, iteration statements and jump statements.
Selection Statements (Conditional Statements)
There are two types of conditional statements in C++, which are “if statement” and “switch
statement”.
The if Statement
The statements execution dependent upon a condition being satisfied. The executed statement will be
decided whether to be execute or not based on a condition which will be tested either for TRUE or
FALSE result. There are different forms of the “If” statement is: simple if ,if- - -else and if- elseif
statement.
The simple if statement
The simple if statement will decide only one part of the program to be executed if the condition is
satisfied or ignored if the condition fails.
The Syntax is:
if (expression)
statements;
first the “if expression” will be evaluated and if the outcome is non zero (which means TRUE), then the
“statements” is executed. Otherwise, nothing happens and execution resumes to the line immediately
after the “if” block. Most of the time “expression” will have relational expressions testing whether
something is equal, greater, less, or different from something else. Thus any expression, whose final
result is either zero or none zero can be used in ”expression”.
//This is a simple if statement;
//This is a simple if statement; #include<iostream.h>
#include<iostream.h>
#include<iostream.h> using namespace std;
using namespace std;
using namespace std; int main ()
int main ()
int main () {
{
{ int x;
int x=20;
int age; cout<<"enter the number"<<endl;
cin>>x; if(x<20)
if(age>18) cout<<"This is a simple if statement";
if(x)
cout<<" you are an adult "; cout<<"you are an adult"; return 0;
return 0; return 0; }
} } 1
//Multiple statements dependent on the same condition -- // Simple if statement ,check the number is the largest
-{___} #include <iostream.h>
// simple if statement check the number is the largest using namespace std;
&sum of the number int main ()
#include <iostream.h> {
using namespace std; int a,b;
int main () cout<<"Enter the two number ";
{ cin>>a>>b;
int a,b,sum; if(a>b)
cout<<"a is the largest"<<endl;
cout<<"Enter the two number "; return 0;
cin>>a>>b; }
if(a>b)
{
sum=a+b;
a*=4;
} // Simple if statement check the number is the largest
cout<<"a is the largest"<<endl; &sum of the number
cout<<"sum="<<sum <<endl; #include<iostream.h>
cout<<"a="<<a; using namespace std;
return 0; int main ()
} {
int c=10;
int a=5;
int d=++c+a;
if(d>c)
a++;
cout<<"a ="<<a;
return 0;
}
4
The Switch Statement
Another C++ selection statement that implements a selection control flow is the switch statement
(multiple-choice statement). The switch statement provides a way of choosing between a set of
alternatives based on the value of an expression.
The switch statement has four components: The General Syntax might be:
▪ Switch switch(constant_expression)
▪ Case {
▪ Default case constant1:
▪ Break statements;
.
.
case constant n:
statements;
default:
statements;
}
When the variable being switched on is equal to a case, the statements following that case will execute
until a break statement is reached. When a break statement is reached, the switch terminates, and the
flow of control jumps to the next line following the switch statement. A switch statement can have an
optional default case, which must appear at the end of the switch. The default case can be used for
performing a task when none of the cases is true. No break is needed in the default case. Now let us see
the effect of including a break statement in the switch statement.
//Program that demonstrate use of switch statement #include<iostream.h>
#include <iostream> int main()
using namespace std; {
int main() char grade;
{ cout<<"Enter Grade";
int classyear; cin>>grade;
cout<<"Please enter your class year in integer:"; switch(grade){
cin>> classyear; case 'A': cout<<"Ecellent";
switch (classyear){ break;
case 1: cout<<"You are 1st year.\n"; case 'B': cout<<"Very Good";
break; break;
case 2: cout<<"You are 2nd year.\n";
break; case 'C': cout<<"Good";
case 3: cout<<"You are 3rd year.\n"; break;
break; case 'D': cout<<"Fair";
case 4: cout<<"You are 4th year.\n"; break;
break; case 'F': cout<<"Fail";
case 5: cout<<"You are 5th year.\n"; break;
break; default: cout<<"Invalid Grade value ";
default: cout<<"An error class year!\n"; }
} }
}
5
Repetition Statements
Repetition statements control a block of code to be executed repeatedly for a fixed number of times or
until a certain condition fails.
➢ There are three C++ repetition statements:
1) The For Statement or loop
2) The While statement or loop
3) The do…while statement or loop
The for statement / loop
The “for” statement (also called loop) is used to repeatedly execute a block of instructions until a
specific condition fails.
The General Syntax is:
for(expression1 ; expression2 ; expression3)
statements;
The for loop has three expressions:
Expression1: is one or more statements that will be executed only once and before the looping starts.
Expression2: is the part that decides whether to proceed with executing the instructions in the loop
or to stop. Expression2 will be evaluated each time before the loop continues. The output of
expression2 should be either non zero (to proceed with the loop) or zero (to stop the loop) to represent
true and false output respectively.
Expression3: is one or more statements that will be executed after each iteration.
Thus, first expression1 is evaluated and then each time the loop is executed, expression2 is evaluated.
If the outcome of expression2 is non zero then statements is executed and expression3 is evaluated.
Otherwise, the loop is terminated. In most programs, the “for loop” will be used for such expressions
where expression1 is initialization, expression2 is condition and expression3 is either increment or
decrement.
The general format can be expressed as follows for the sake of clarity:
for(initialization ; condition ; increase/decrease)
statement;
Steps of execution of the for loop:
1. Initialization is executed. (will be executed only once)
2. Condition is checked, if it is true the loop continues, otherwise the loop finishes and statement
is skipped.
3. Statement is executed.
4. Finally, whatever is specified in the increase or decrease field is executed and the loop gets
back to step two.
//Program that demonstrate for loop Guess the output of the following code:
int main (){ int main()
int i; {
for (i=0; i<=10; i++) for(int i=10;i>0;i--)
{ {
cout<<i<<” ”;} cout<<i<<“,”;
cout<<endl; }
return 0; } cout<< “FIRE!”;
return 0; }
6
//Program that demonstrate nested for loop
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main (){ int main (){
int i,j; int i,j;
for(i=0;i<=10;i++) { for(i=5;i>=1;i--) { //for(i=1;i<=5;i++)try it
for (j=0;j<=i;j++) for (j=i;j>=1;j--) //for(j=i;j<=5;j++)
{ {
cout<<j;} int i,j; cout<<j;}
cout<<endl; for(i=1,j=2;i<=4;i++,j+=2) { cout<<endl;
cout<<i<<" "<<j<<endl;
} }
}
return 0; return 0;
} }
7
Do…while loop
The do statement (also called the do while loop) is similar to the while statement, except that its body is
executed first and then the loop condition is examined. In do…while loop, we are sure that the body of
the loop will be executed at least once. Then the condition will be tested.
The general syntax is:
do
{
statement;
}
while(expression); // condition
First statement is executed and then expression is evaluated. If the outcome of the expression is nonzero,
then the whole process is repeated. Otherwise the loop is terminated. Do while lopp
#include <iostream.h>
using namespace std; #include <iostream.h>
int main() { using namespace std;
int n; int main() {
cout<<"enter n"; int age;
do { cout<<"enter age";
cin >> n; do {
cout << n * n << '\n'; cin >> age;
} cout << ++age << '\n';
while (n != 0); }
return 0; while (age<=10);
} return 0;
}
The continue and break statements
for(int n=10;n>0;n--)
{
if(n = = 5)
continue; //causes a jump to n
cout<<n<< “,”;
}
8
The break statement
A break statement may appear inside a loop (while, do, or for) or a switch statement. It causes a jump
out of these constructs, and hence terminates them. Like the continue statement, a break statement only
applies to the “loop” or “switch” immediately enclosing it. It is an error to use the break statement outside
a loop or a switch statemen
// break loop example
#include <iostream.h> }
using namespace std; #include<iostream.h>
int main () using namespace std;
{ int main()
{
for (int n=10; n>0; n--)
for (int age=10; age>0;age--) {
{
cout<<age;
cout << n << ", "; if(age==5){
if (n==3) cout<<" waw execution are abotred";
{
cout << "countdown aborted!"; break; }
break; } }
} return 0; }
}
return 0;