0% found this document useful (0 votes)
27 views33 pages

Lecture3

Control structures are used to control program execution flow and include sequences, selections, and repetitions. Sequences execute statements in order, selections use if/else conditional statements to choose between blocks of code, and repetitions use loops to repeatedly execute blocks. Common control structures include if/elseif/else conditional statements and while, do-while, and for loops. Nested control structures allow placing structures inside other structures to create more complex program behaviors.

Uploaded by

Maxwell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views33 pages

Lecture3

Control structures are used to control program execution flow and include sequences, selections, and repetitions. Sequences execute statements in order, selections use if/else conditional statements to choose between blocks of code, and repetitions use loops to repeatedly execute blocks. Common control structures include if/elseif/else conditional statements and while, do-while, and for loops. Nested control structures allow placing structures inside other structures to create more complex program behaviors.

Uploaded by

Maxwell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

CONTROL STRUCTURES

 Control structures are used to control a


program execution flow – the order in which
statements in a program are executed
 There are three types of control structures
◦ Sequence – This is the default structure.
Statements in a function are executed in a
top-down fashion
◦ Selection/Decision – two or more options
exists and a choice has to be made.
◦ Repetition/Iteration/loop – program
statements executed two or more times
 In both selection and loops, a condition
determines how statements are executed

Condition
Conditionals operators use two kinds of special operators:
relational and logical. These are used to determine whether
some condition is true or false. The relational operators are
used to test a relation between two expressions:
The logical operators are often used to combine
relational expressions into more complicated Boolean
expressions:
Examples using logical operators
(assume x = 6 and y = 2):

!(x > 2) → false


(x > y) && (y > 0) → true
(x < y) && (y > 0) → false
(x < y) || (y > 0) → true
Implemented using the following statements
a) The if statement:
b) The switch statement
 The if conditional has the form:
 if(condition)
 {
 Statement1
 statement2…
 }
The condition is some expression whose value is being
tested. If the condition resolves to a value of true, then the
statements are executed before the program continues on.
Otherwise, the statements are ignored.
The if-else form is used to decide between two sequences
of statements referred to as blocks:
if(condition)
{
statementA1
statementA2
}
else
{
statementB1
statementB2
}
If the condition is met, the block corresponding to the if is
executed. Otherwise, the block corresponding to the else
is executed. Because the condition is either satisfied or
not, one of the blocks in an if-else must execute.
The else if is used to decide between two or more blocks
based on multiple conditions:
if(condition1)
{
statementA1
statementA2…
}
else if(condition2)
{
statementB1
statementB2…
}
…..
If condition1 is met, the block corresponding to the
if is executed.
If not, then only if condition2 is met is the block
corresponding to the else if executed.
There may be more than one else if, each with its
own condition. Once a block whose condition was
met is executed, any else ifs after it are ignored.
Therefore, in an if-else-if structure, either one or
no block is executed.
An else may be added to the end of an if-else-if. If
none of the previous conditions are met, the else
block is executed.
#include <iostream>
using namespace std;
int main()
{
int x = 6;
int y = 2;
if(x > y)
cout << “x is greater than y\n”;
else if (y > x)
cout << “y is greater than x\n”;
else
cout << “x and y are equal\n”;
return 0;
}
if (gender = 'm')
#include <iostream> {
using namespace std; cout<<"He is a MAN";
int main(){ }
char gender; else if (gender = 'f')
cout<< "Enter Gender {
m/f :"; cout<<"She is a
cin>> gender; WOMAN";
}

else cout<<" Invalid


Gender";

return 0;
}
#include <iostream> if (y>x)
{
using namespace std; if (y>z)
int main(){ {
int x,y,z,l; l=y;
}
cout<< "Enter THREE
Numbers at random"; }
cout<< "Enter X"; else if (z>x)
cin>> x; {
l=z;
cout<< "Enter Y"; }
cin>> y;
cout<< "Enter Z "; cout<< "The largest is "<< l;
return 0;
cin>> z;
}
l=x;
 This is a multiple-branching statement
where, based on a condition, the control is
transferred to one of the many possible
points;
 The switch-case is another conditional structure
that may or may not execute certain statements.
 The switch evaluates expression and, if expression
is equal to constant1, then the statements beneath
case constant 1: are executed until a break is
encountered.
 If expression is not equal to constant1, then it is
compared to constant2. If these are equal, then the
statements beneath case constant 2: are executed
until a break is encountered.
 If not, then the same process repeats for each of the
constants, in turn. If none of the constants match,
then the statements beneath default: are executed.
switch(expression)
{
case constant1:
statementA1
statementA2
...
break;
case contant2:
statementB1
statementB2
...
break;
...
default:
statementZ1
statementZ2
...
}
#include <iostream>
using namespace std;
int main()
{
int x = 6;
switch(x)
{
case 1:
cout << “x is 1\n”;
break;
case 2:
case 3:
cout << "x is 2 or 3";
break;
default:
cout << "x is not 1, 2, or 3";
}
return 0;
}
#include <iostream>
using namespace std;
int main(){
char gender;
cout<< "Enter Gender m/f :";
cin>> gender;
switch(gender)
{
case 'm': cout<<"He is a MAN";
break;
case 'f': cout<<"She is a WOMAN";
break;
default: cout<<" Invalid Gender";
}
return 0;
}
 Used where statements in a program
be require to be executed several
times
 C++ has three kinds of loops:
 While
 do-while
 for
 The while statement:
Syn:

While(condition)
{
Statements;
}
As long as condition holds, the block of
statements will be repeatedly executed.
#include <iostream>
using namespace std;
int main()
{
int x = 0;
while(x < 10)
{
x = x + 1;
cout << “x is “ << x << “\n”;
}
return 0;
}
The do-while statement:
The do-while loop is a variation that
guarantees the block of statements will be
executed at least once:

Syn:

Do
{
Statements ;
}
while(condition);
The block of statements is executed and then,
if the condition holds, the program returns to
the top of the block.
Curly braces are always required.
Also note the semicolon after the while
condition.
The for loop:

Syn:

for(expression1;expression2;expression3)
{
Statements;
Statements;
}
The for loop is designed to allow a counter
variable that is initialized at the beginning of
the loop and incremented (or decremented) on
each iteration of the loop.
#include <iostream>
using namespace std;
int main()
{
for(int x = 0; x < 10; x = x + 1)
{
cout << x << “\n”;
}
return 0;
}
This program will print out the values 0 through 9,
each on its own line.
If the counter variable is already defined, there is no
need to define a new one in the initialization portion
of the for loop.
#include <iostream>
using namespace std;
int main()
{
int n,c=1;
cout<< "Enter number of times to display the Message :";
cin>>n;
while (c<=n)
{
cout<< "HELLO" << "\n";
c++;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n,c=1;
cout<< "Enter number of times to display the Message :";
cin>>n;
do
{
cout<< "HELLO" << "\n";
c++;
}while (c<=n);
return 0;
}
 #include <iostream>
 using namespace std;
 int main()
 {
 int n,c;
 cout<< "Enter number of times to display the Message :";
 cin>>n;
 for (c=1;c<=n;c++)
 {
 cout<< "HELLO" << "\n";
 }
 return 0;
 }
#include <iostream>
using namespace std;
int main()
{
int n,fact=1,c;
cout<< "Enter number N to compute Factorial :";
cin>>n;
for (c=1;c<=n;c++)
{
fact = fact*c;
}
cout<< "The Factorial of "<<n<< "is "<<fact;
return 0;
}
 It is possible to place ifs inside of ifs and
loops inside of loops by simply placing these
structures inside the statement blocks. This
allows for more complicated program
behavior.
 The inner loop is executed first, after which
control is taken to the outer loop until
program terminates
#include <iostream>
using namespace std;
int main()
{
for(int x = 0; x < 4; x = x + 1)
{
for(int y = 0; y < 4; y = y + 1)
{
cout << y;
}
cout << “\n”;

}
return 0;
}

This program will print four lines of 0123.


END.
WAIRAGU G.R.

You might also like