Chapter4 Control Flow Structure - Cs
Chapter4 Control Flow Structure - Cs
Fundamentals of Programming
true
• if(Expre.) • Conditional Statement
false
//Example,If Statement
#include<iostream>
#include<stdlib>
Using namespace std;
Int main()
{
Int x, y;
cout<<“Enter the first number”<<endl;
cin>>x;
cout <<“Enter the second number”<<endl;
cin>>y;
If (x==y)
{
cout<<“the two numbers are equal”<<endl;
exit(0);
}
cout<<“The two numbers are not equal”<<endl;
return 0;
}
The If else statement
• Another form of the “if” is the “if …else” statement. The “if else if”
statement allows us to specify two alternative statements:
One which will be executed if a condition is satisfied and
Another which will be executed if the condition is not satisfied.
• The General Syntax is:
If (expression)
statement1;
else
statement2;
First “expression” is evaluated and if the outcome is none zero (true),
then “statements1” will be executed. Otherwise, which means the
“expression” is false “statements2” will be executed
If else statement..
• If (expression)
Statement;
Else
Statement;
If else statement..
If body of if and else is compound then the structure looks like:
If (expression)
E.g.:
{ if(balance > 0)
statement1; {
statement2; interest = balance *
.Statement n; } creditRate;
else balance += interest;
}
{
else
Statement1; {
Statement2; interest = balance *
Statement n; debitRate;
} balance += interest; }
//write the if else program code below and see the effects
#include<iostream>
#include<conio.h>
Using namespace std;
int main(
{
int testScore;
cout<<”\nEnter your test score:”;
cin>>testScore;
if(testScore<70)
cout<<”\n You did not not pass:”; //then block
else
cout<<”\n You did pass”; //else block
getch();
return 0;
}
If else structure execute like follow:
expression is evaluated first if that is true then the
if body is executed otherwise else body is executed.
following diagram show its execution
true
• if(Expre.) • Conditional Statement
false
• else Statement
The “if else if” statement
• The third form of the “if “statement is the “if…else if” statement.
• The “if else if” statement allows us to specify more than two
alternative statements each will be executed based on testing
one or more conditions.
• The General Syntax is:
if (expression1)
statements1;
else if(expression2)
statements2; .. .
else if(expressionN)
statementsN;
else
statements
The “if else if” statement...
• First the “expression1” is evaluated if the outcome is non zero
(true), then “statements1” will be executed. Otherwise, which
means the “expression1” then “expression2” will be
evaluated:
• if the out put of expression2 is True then “statements2” will
be executed otherwise the next “expression” in the else if
statement will be executed and so on.
• If all the expressions in the “if” and “else if “statements are
false then “statements” under else will be executed.
• The “if…else” and “if…else if” statements are said to be
exclusive selection as if one of the condition (expression) is
satisfied only instructions in its block will be executed and the
rest will be ignored.
Nested if else statement
• In if else statement in place of body we can also
use again if or if else. This concept is known as
nested structure.
– If(a>b) {
• If(a>c)
cout<<“a is the larger value”<<endl;
• Else
cout<<“c is the lager value”<<endl; }
– Else
• If(b>c)
Cout<<“b is largest”<<endl;
• Else
Cout<<“c is lagest”;
The Switch Statement
• Another C++ 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:
Switch
Case
Default
Break
The Switch Statement..
Syntax:
• Where Default and Break are switch (<selector expression>)
{
Optional case <label1> : <sequence
of statements>;
• The General Syntax might be: break;
switch(expression) case <label2> : <sequence
of statements>;
{ break;
case <labeln> : <sequence
case constant1: of statements>;
break;
statements; default <sequence ofstatements>;
case constant n: }
.. .
statements;
default:
statements;
The Switch Statement..
Meaning:
• Evaluate selector expression.
• The selector expression can only be: a bool, an
integer, an enum constant, or a char.
• Match case label.
• Execute sequence of statements of matching label.
• If break encountered, go to end of the switch
statement.
• Otherwise continue execution
switch Statement: Example 1
switch(int(score)/10){
case 10:
case 9: cout << "Grade = A" << endl;
case 8: cout << "Grade = B" << endl;
case 7: cout << "Grade = C" << endl;
case 6: cout << "Grade = D" << endl;
default:cout << "Grade = F" << endl;
}
switch Statement: Example 2
#include <iostream>
using namespace std;
int main()
{ char answer;
cout << "Is comp102 an easy course? (y/n): ";
cin >> answer;
switch (answer){
case 'Y':
case 'y': cout << "I think so too!" << endl;
break;
case 'N':
case 'n': cout << "Are you kidding?" << endl;
break;
default:
cout << "Is that a yes or no?" << endl; }
return 0; }
switch Statement with Multiple Labels:
Example 3
switch (watts) {
case 25 : lifespan = 2500;
break;
case 40 :
case 60 : lifespan = 1000;
break;
case 75 : lifespan = 750;
break;
default :
lifespan = 0;
} // end switch
Points to Remember
• The expression followed by each case label
must be a constant expression.
• No two case labels may have the same value.
• Two case labels may be associated with the
same statements.
• The default label is not required.
• There can be only one default label, and it
is usually last.
C) Repetition statements (Looping ) or iteration
Looping or iterative statement are used when a need arise
to execute a single statement a number of time. For loop
cause a section of our program to be repeated a certain
number of time.
Initialisation
Increment/decrement
Condition body
test true of loop executed
false
//example1
//Write the following program and see the output
#include<iostream>
using namespase std;
int main()
{
for(int i=0;i<10;i++)
cout<<“Hello world”<<endl;
return 0;
}
//example2
//Write the following program and see the output
#include<iostream>
using namespace std;
int main()
{
for(int i=0;i<10;i++)
cout<<i<<endl;
return 0;
}
Note: We can write the following type for loop
For(;;)infinite loop
example1
#include<iostream>
using namespace std;
int main()
{
for(int i=11;i>10;i++)
cout<<i<<endl;
}
From the above for loop type, the statement executes infinite
time. Because there is no condition checked within the for loop
that means we can say that is for loop all the executions are
optional we can skip any one or more if we need, but semicolon is
necessary.
Nested for loop
In C++ any loop can be placed inside other lop one for
loop can be placed inside other loop that is known as
nested for loop.
In the nested loop the inner loop is executed up to end
for a single interaction of outer loop.
//example1
//Write the following program and see the output
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=6;i++) {
for(int j=1;j<=i; j++)
cout<<j<<endl; }
return 0; }
While loop
For loop is best if we know fixed number of repetition. But
if we don’t know how many times you want to do
something before you start the loop then the while loop is
used.
General Syntax:
while (condition)
body of while loop
Execution of while loop:
First the condition is evaluated if that is non-zero the body
of loop executed otherwise loop terminates.
After execution of body again condition is checked if it is
false the loop terminated, otherwise the body of loop is
executes. The body of loop is executed again and again up
to when the condition is true.
//exampl1
//Write the following program and see the output
#include<iostream>
using namespace std;
int main()
{
int i;
while(i<=9)
cout<<“i=“<<i;
return 0;}
//Writ a program which reads integer number from
the key board until you don’t give Zero number.
#include<iostream>
using namespace std;
int main()
{
int n=4; // make sure that n is non 0
cout<<“Enter 0 for termination otherwise any number”<<endl;
cout<<endl;
cin>>n;
while(n!=0)
The do ---while statement (loop)
Its functionality is exactly the same as the while loop
except that condition in the do-while is evaluated after
the execution of statement instead of before, granting at
least one execution of statement even if condition is
never fulfilled.
General Syntax
do
statements;
while (expression);
The do ---while loop is similar with a while loop, except
that its body is executed first and then the loop
condition is checked.
// For example, the following program echoes any number you enter until
you enter 0.