Unit-2-Control structures
Unit-2-Control structures
Prepared by :
Prof. Khushbu Chauhan
Computer Engg. Dept.
MPSTME, NMIMS
Outlines
• Introduction
• Conditional Branching
• Looping
• Nested looping
Control Structure
• A statement that is used to control the flow of execution in a program is
called control structure.
• Sequence
• Decision
• Looping
Need of decision making
• if number is odd
• {
• /* code */
• }
• It evaluates condition or logical expression first and based on its result (either
true or false), the control is transferred to particular statement.
• If result is true then it takes one path else it takes another path.
Decision Making Statements in C++
• Decision Making Statements are
if(condition)
{
// Body of the if
// true part
}
WAP to print Odd or Even Number
#include<iostream>
using namespace std;
int main()
{
int a;
cout <<"Enter Number:";
cin >> a;
if(a%2 == 0)
{
cout
<<"Even Number";
}
if(a%2 != 0)
{
cout <<"Odd Number";
}
return 0;
}
if...else
• if…else is two branch decision making statement
• If condition is true then true part will be executed else false part will be executed
• else is keyword
Syntax
if(condition)
{
// true part
}
else
{
// false part
}
if…else…if ladder
• if…else…if ladder is multi branch decision making statement.
• If first if condition is true then remaining if conditions will not be evaluated.
• If first if condition is false then second if condition will be evaluated and if it is
true then remaining if conditions will not be evaluated.
Syntax
if(condition-1)
statement-1;
else if(condition-2)
statement-2;
else
statement-3;
Flowchart
WAP to print Zero, Positive or Negative Number
#include<iostream>
using namespace std;
int main()
{
int a;
cout <<"Enter Number:";
cin >> a;
if(a > 0)
cout <<"Positive Number";
else if(a==0)
cout <<"Zero";
else
cout <<"Negative Number";
return 0;
}
Nested if
• If condition-1 is true then condition-2 is evaluated. If it is true then statement-1 will
be executed.
• If condition-1 is false then statement-3 will be executed.
if(condition-1)
{
if(condition-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
Nested if flowchart
Find the largest among three different numbers entered by the user.
int main(){
int a, b, c;
cout
<<"Enter Three Numbers:";
cin >> a >> b >> c;
if(a>b)
{
if(a>c)
cout <<“ num is max“ <<
a;
else
cout <<“ num is max“ <<
c;
}
else
{
if(b>c)
cout <<"max is “ << b;
else
cout <<"max is“ << c;
}
return 0;
switch...case
• The switch statement allows to execute one code block among many alternatives.
• It works similar to if...else..if ladder.
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
• The expression is evaluated once and compared with the
values of each case.
• If there is a match, the corresponding statements after the
matching case are executed.
• If there is no match, the default statements are executed.
• If we do not use break, all statements after the matching
label are executed.
• The default clause inside the switch statement is
optional.
WAP that asks day number and prints day name using switch…case
int main(){ case 7:
int day; cout<<"Saturday";
cout<<"Enter day number(1-7):"; break;
cin>>day; default:
switch(day) cout<<"Wrong input";
{ break;
case 1: }
cout<<"Sunday"; return 0;
break; }
case 2:
cout<<"Monday";
break;
case 3:
cout<<"Tuesday";
break;
case 4:
cout<<"Wednesday";
break;
case 5:
cout<<"Thursday";
break;
case 6:
cout<<"Friday";
break;
Looping
What is loop?
• Loop is used to execute the block of code several times according to the condition
given in the loop. It means it executes the same code multiple times.
Looping or Iterative Statements in C/C++
• Looping Statements are
Entry Controlled Loop: while, for
Exit Controlled Loop: do…while
Virtual Loop: goto
While Loop
• while is an entry controlled loop
• Statements inside the body of while are repeatedly executed till the condition is
true
• while is keyword
Syntax
while(condition)
{
// Body of the while
// true part
}
if v/s while
WAP to print 1 to n(while loop)
#include<iostream>
using namespace std;
int main()
{
int i=1,n;
cout<<"Enter n:";
cin>>n;
while(i<=n)
{
cout<< i << "";
++I;
}
return 0;
}
WAP to print Odd numbers between 1 to n(while loop)
#include<iostream>
using namespace std;
int main()
{
int i=1, n;
cout<<“Enter the value of n”;
cin>>n;
while(i <= n)
{
if(i%2!=0)
cout << i << “”;
i=i+1;
}
return 0;
}
Trace table
Line i n condition Process Output
1 Linking header file
2 Calling void main()
3
4 1 Declarations
5 Enter value of
n
6 5 Scanning value of n 5
7 i <= n, TRUE While loop condition
8
9 i%2 != 0 TRUE If condition
10 Printing value of i 1
11 2 Incrementing value of i by 1
12 Jump to line:7
7 i <= n, TRUE While loop condition
8
9 i%2 != 0 FALSE If condition, skip line:10
11 3 Incrementing value of i by 1
12 Jump to line:7
7 i <= n, TRUE While loop condition
8
9 i%2 != 0 TRUE If condition
10 Printing value of i 3
11 4 Incrementing value of i by 1
12 Jump to line:7
13 End of program
Syntax and Logic
How to build logic?
Step 1: Understand the problem statement
• e.g. Write a program to find factors of a number.
• Run following questions through mind
• What is the factor of a number?
– Factor is a number that divides another number evenly with no remainder.
– For example, 1,2,3,4,6,12 are factors of 12.
• How many variables needed? What should be their data types?(Inputs/Outputs)
– To get number from user we need variable n.
– Now we need to divide n with 1,2,3,...,n. For this we will declare a loop variable i initialized as 1.
– Both variables should be of integer data type.
• What control structure you require?
– First we need a loop to divide n by 1,2,3,…,n, loop will start from 1 and ends at n.
– Inside loop we need if structure to check n%i==0 (Number n is evenly divisible by i or not).
Step 2: Think for 1 or 2 examples
• Consider n=6, now take i=1
– 6%1==0, TRUE; So, 1 is factor of 6
– 6%2==0, TRUE; So, 2 is factor of 6
– 6%3==0, TRUE; So, 3 is factor of 6
– 6%4==2, FALSE; S0, 4 is not factor of 6
– 6%5==1, FALSE; S0, 5 is not factor of 6
– 6%6==0, TRUE; S0, 6 is factor of 6
• From this we can infer that loop variable i starts with 1 and incremented by one for next iteration
then ends at value n.
• Consider n=10, factors are 1,2,5,10
• Consider n=11, factor is 1,11
• From this we can infer that 1 and number itself are always factors of any number n.
Step 3: Draw flowchart/algorithm on paper or in mind
Step 4: Writing Pseudo-code
• Pseudo-code is an informal way to express the design of a computer program or an
algorithm.
• It does not require any strict programming language syntax.
Practice exercises
(1) WAP to find factors of a number(while loop)
(2) WAP to print reverse a number(while loop)
(3) WAP to check given number is perfect or not(while loop)
(4) WAP to check given number is prime or not(while loop)
for Loop
• for is an entry controlled loop
• Statements inside the body of for are repeatedly executed till the condition is true
• for is keyword for (initialization; condition; updateStatement)
{
// statements
}
• The initialization statement is executed only once.
• Then, the condition is evaluated. If the condition is false, the for loop is
terminated.
• If the condition is true, statements inside the body of for loop are executed, and the
update statement is updated.
• Again the condition is evaluated.
WAP to print numbers 1 to n (for loop)
#include<iostream>
using namespace std;
int main()
{
int i,n;
cout<<"Enter a number:";
cin >> n;
for(i=1;i<=n;i++)
{
cout << I << “”;
}
return 0;
}
Practice programs
(1) WAP to find factors of a number (for loop)
(2) WAP to print reverse a number(for loop)
(3) WAP to check given number is perfect or not(for loop)
(4) WAP to check given number is prime or not(for loop)
do while Loop
• do while is an exit controlled loop.
• Statements inside the body of do while are repeatedly executed till the
condition is true. do
• Do and while are keywords. {
// statement
}
while (condition);
• Loop body will be executed first, and then condition is checked.
• If the condition is true, the body of the loop is executed again and the condition is
evaluated.
• This process goes on until the condition becomes false.
• If the condition is false, the loop ends.
WAP to print Odd numbers between 1 to n(do while loop)
int main()
{
int i=1,n;
cout << "Enter a number:";
cin >> n;
do
{
if(i%2!=0)
{
cout << i << “”;
}
i=i+1;
}
while(i<=n);
}
goto Statement
• goto is an virtual loop
• The goto statement allows us to transfer control of the program to the specified
label.
• goto is keyword
goto label label:
; .
. .
. .
. goto label
label: ;
• The label is an identifier. When the goto statement is encountered, the control
of the program jumps to label: and starts executing the code.
WAP to print Odd numbers between 1 to n(goto)
int main()
{
int i=1,n;
cout<<"Enter a number:";
cin >> n;
odd:
if(i%2!=0)
{
cout << i << “”;
}
i=i+1;
if(i<=n)
{
goto odd;
}
}
Types of loops
Break Statement
• The break in C++ is a loop control statement used to terminate
the loop. As soon as this break statement is encountered
within a loop, the loop iterations end abruptly, and the control
will return immediately to the next line of code after the loop.
continue Statement
• C++ continue statement is a loop control statement that forces
the program control to execute the next iteration of the loop.
As a result, the code inside the loop following the continue
statement will be skipped and the next iteration of the loop
will begin.
WAP to use continue statement
Nested loop
• Nested loop means a loop statement inside another loop statement. That is why
nested loops are also called as “loop inside loop“.
• Syntax for Nested For loop:
• Syntax for Nested While loop: Syntax for Nested Do-While loop:
• There is no rule that a loop must be nested inside its own type. In fact, there can be
any type of loop nested inside any type and to any level.
WAP to display 7 days of 3 weeks
WAP to print pattern (nested loop)
*****
*****
*****
*****
*****
WAP to print pattern (nested loop)
int main() *
{ **
int i,j; ***
for(i=1;i<=5;i++) ****
{ *****
for(j=1; j<=i; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
WAP to print pattern (nested loop)
int main() *
{ **
int i,j,k; ***
for(i=1;i<=5;i++) ****
{ *****
for(k=5;k>i;k--)
{
cout << " ";
}
for(j=1;j<=i;j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
For practice
Discussion…
Thank You