chapter 04 Control statements
chapter 04 Control statements
[CoEng1112]
Chapter Four:
Control Statements
Introduction
• Control statements allow decision making
within programs.
• Control statements come in several
different forms:
– Conditional statements: to optionally execute
C ++ statements
– Loops: executed statements repeatedly until
the exit condition is satisfied.
– Branch: jump to a certain statement and
continue execution.
2
4.1 Conditional Structure: If and else
3
1. Simple if statement
• General form:
Entry
if (test expression)
Test False
{ expression
?
statement-block;
True
}
Statement-block
statement-x;
Statement-x
General form Flowchart of simple if statement 4
Cont’d
• The ‘statement-block’ may be a single
statement or a group of statements.
• If the test expression is true, the
statement-block is executed. If it is false,
statement-block is ignored (not executed)
and the program continues on the next
instruction after the conditional structure.
5
\* To display number is Sample output 1:
positive.*/ Enter a number:3
#include<iostream.h> number is positive
int main()
{
int num;
cout<<“Enter a number:”; Sample Output 2:
cin>>num; Enter a number:-3
if (num>0)
cout<<” number is positive ”;
return 0;
}
6
Cont’d
• For a block of instructions, we use curly
brackets { }:
• Example: if (x == 100)
{
cout << "x is ";
cout << x;
}
• But for a single, we can ignore {}.
• Example: if (x == 100)
cout << "x is 100“; 7
2. The if – else statement
• Form:
Entry
if (condition)
{ True Conditio False
n
statement block 1; ?
}
else statement statement
block 1 block 2
{
statement block 2;
}
statement-x; statement-x
8
Cont’d
• For example:
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
9
3. Nested if statement
• when a series of decisions are involved,
use more than one if…else statement.
• Example:
if (x>0)
cout<<”The given number is +ve”<<endl;
else if (x<0)
cout<<”The given number is negative”<<endl;
else
cout<<”The given number is zero”;
10
Exercise
• Write a program that find whether the
given number is even or odd.
11
Solution
// To find whether given number is even or odd.
#include<iostream.h>
void main()
{
int no;
cout<<”enter a number”;
cin>>no;
if ( no %2 == 0 )
cout<<no<<”is an even number”;
else
cout<<no<<”is an odd number”;
12
}
4.2 Repetitive Structures or
loops
• Loops have as objective to repeat a
statement a certain number of times while
a condition is fulfilled.
– The while loop
– The do-while loop
– The for loop
13
The while loop
• Format:
while(expression)
statements;
While
(expression) statements
? True
False
Out of loop
14
Example:
• Find the sum of even numbers between 1
to n
#include <iostream.h>
void main()
{
int i, n, sum=0;
cout<<"enter the end number:";
cin>>n;
15
Example(cont’d)
i=2;
while (i <= n)
{
sum = sum + i;
i = i + 2;
}
cout<<"sum= "<<sum;
}
16
The do-while loop
do
• Format:
do
statements
statements;
while (condition);
Condition
?
True
False
17
Out of loop
Example
sum = 0;
i = 1;
do
{
sum += i;
i += 2;
} while (i <= 100);
cout<<“Sum of odd integers up to 100 is”<<sum;
• Output:
Sum of odd integers up to 100 is 2500 18
The for loop
• Its format is:
for (initialization; condition; increase/decrease)
statement;
• Following steps are involved in the code:
False Condition
?
Out of loop
True
statement
increase/decrease
20
Examples
• Example 1:
sum = 0;
for (i=0 ; i <= 100 ; i++)
sum += i;
• Example 2: Write a program to list a
number and its square in two columns for
integers 1 up to 10.
21
#include<iostream.h>
void main()
{
for(int i=1;i<=10;i++)
{
cout<<“The number is:”<<i;
int square=i*i;
cout<<“ and its square is:”<<square;
cout<<endl;
}
} 22
4.3 Bifurcation of control and
jumps.
23
The break instruction
• The format of the break statement is
simply break;
• causes an immediate exit even if the
condition for its end is not fulfilled.
• It can be used to end an infinite loop, or to
force it to end before its natural end.
24
Examples
i=1;
while (1)
{
cout<<i<<" ";
if (i==10)
break;
i=i+1;
}
Output: 1 2 3 4 5 6 7 8 9 10 25
// break loop example 10, 9, 8, 7, 6, 5, 4, 3,
#include <iostream.h> countdown aborted!
int main ()
{
int n;
for (n=10; n>0; n--) {
cout << n << ", ";
if (n==3)
{
cout << "countdown
aborted!";
break;
}
}
return 0; 26
}
The continue instruction
• Format: continue;
• The continue statement tells the compiler,
“Skip the following statements and
continue with the next iteration’’.
• In while and do loops, continue causes
the control to go directly to the test-
condition and then to continue the iteration
process. In the case of for loop, the
increment section of the loop is executed
before the test-condition is evaluated. 27
while (---------)
{
---------
---------
if (condition)
continue;
---------
---------
}
--------
28
// to add only positive numbers
#include<iostream.h>
void main()
{
int i, n, sum=0;
cout<<"enter any 10 numbers\n";
for(i=1;i<=10;i++)
{
cin>>n;
if (n<0)
continue;
sum += n;
}
cout << " sum of +ve numbers = "<<sum; 29
}
// break loop example 10, 9, 8, 7, 6, 4, 3, 2, 1, End!
#include <iostream.h>
int main ()
{
for (int n=10; n>0; n--)
{
if (n==5)
continue;
cout << n << ", ";
}
cout << “End!";
return 0;
}
30
The goto instruction.
• allows making an absolute jump to another
point in the program.
• The destination point is identified by a
label, which is then used as an argument
for the goto instruction.
• A label is made of a valid identifier
followed by a colon (:).
31
// goto loop example 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, End!
#include <iostream.h>
int main ()
{
int n=10;
loop:
cout << n << ", ";
n--;
if (n>0)
goto loop;
cout << “End!";
return 0;
}
32
The exit function
• a function defined in cstdlib (stdlib.h)
library.
• The purpose of exit is to terminate the
running program with an specific exit code.
• Its prototype is:
void exit (int exit code);
• if exit code= 0 means that the program
finished normally.
33
// exit function example 10, 9, 8, 7,
#include <iostream.h>
int main ()
{
int n=10;
while(n>0)
{
cout << n << ", ";
n--;
if (n==6)
exit(0);
}
cout << “End!";
return 0;
} 34
4.4 The selective Structure:
switch
• General form:
switch (expression)
{
case constant1:
block of instructions 1;
break;
case constant2:
block of instructions 2;
break;
…
default:
default block of instructions;
} 35
Cont’d
• Inclusion of break at the end of each block
is necessary because if, for example, we
did not include it after block of instructions
1 the program would not jump to the end
of the switch selective block (}) and
continue to execute the rest of the blocks
of instructions until the first appearance of
break.
36
Example 1:
switch (x) {
case 1:
cout << "x is 1";
break;
case 2:
cout << "x is 2";
break;
default:
cout << "value of x unknown";
} 37
Example 2:
switch (x) {
case 1:
case 2:
case 3:
cout << "x is 1, 2 or 3";
break;
default:
cout << "x is not 1, 2 nor 3";
38
}