0% found this document useful (0 votes)
68 views

C++ Chapter 4 (2)

fsfdsssd

Uploaded by

shiferachala778
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)
68 views

C++ Chapter 4 (2)

fsfdsssd

Uploaded by

shiferachala778
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/ 11

Introduction to computer & programming

Chapter four
Control Structure
Depending upon requirements of a problem, it is often required to alter the normal
sequence of execution to in the program. This means that we may desire to selectively
or repetitively execute a program segment.
Control structure

Repetitive statement Breaking


Conditional statement (Loop statement) control statement

If statement switch-case for while do-while


Statement loop loop loop Break Continue
Statement Statement
Nested if else
If if else go to statement
Before studying above structure we will study simple and block statement.
Statement is an expression like: c=0 or x- - or cout<<x. Becomes a statement when it is
followed by a semicolon as in
C=0;
x- -;
cout<<x;
In C++ semicolon is a statement terminator. The above statements are also known as
simple statements.
Block- are also known as compound statement. Braces { and } are used to group
declaration and statements together into a compound statement, or block, so that they are
syntactically equivalent to a single statement. There is no semicolon after the right brace
that ends a block.
e.g. { x1=y + z;
x2=y * z;
x3=y – z;
x4= y/z;
}
Conditional statement – are mainly used for making decision. Depending on the
value of an expression decision can be made.
1. if statement – is the simplest of the decision statement. The syntax of the if
statement is as follow:
if (expression)
body of if;
Expression is evaluated first if this is true then body of if is executed. If the
expression is false then the body of if is skipped and execution continues with the
next statement. Body of if is simple statement or block depending on the
programmer requirement. i.e. if body of if is simple statement then that look like:
if (expression)
1
Introduction to computer & programming
Statement ;
Or
if (expression)
{ statement ; }
If the body of if is compound or block then that look like:
if (expression)
{ statement1;
Statement2;
.
.
Statement n; }
If expression is true then all the statements within the block are executed. In case
expression is false all the statements within the block are skipped. If structure
execute like follow:

expression false

true
Body of if

E.g. write a program which reads two numbers and then print greater one.
If (a>b) cout<<”greater is: “<<a;
If (b>a) cout<<”greater is: “<<b;
For example, the following code fragment prints x is 100 only if the value stored in
the x variable is indeed 100:

1 if (x == 100)
2 cout << "x is 100";

If we want more than a single statement to be executed in case that the condition is true
we can specify a block using braces { }:

1 if (x == 100)
2{
3 cout << "x is ";
4 cout << x;
5}

2
Introduction to computer & programming

2. if else statement – the expression is evaluated first if that is true then the if
body is executed otherwise else body is executed. the syntax of if else statement is
as follow:
if (expression)
body of if
else
body of else
Body of if and body of else may be simple or block. If body of if and else is compound
then the structure looks like:
if (expression)
{ statement1;
Statement2;
.
.
Statement n; }
else
{ statement 1;
Statement2;
.
.
Statement n;
}

If else structure executed like follow:

express false
ion

true
Body of if Body of else

3
Introduction to computer & programming
E.g. write a program which reads a number and then prints the message for that number
(that is even or odd).
If (n%2= =0)
cout<< “the number is even”;
else
cout<<”the number is odd”;
The following example also shows its use telling if the value currently stored in x is
positive, negative or none of them (i.e. zero):

1 if (x > 0)
2 cout << "x is positive";
3 else if (x < 0)
4 cout << "x is negative";
5 else
6 cout << "x is 0";

Remember that in case that we want more than a single statement to be executed, we
must group them in a block by enclosing them in braces { }.

3. Nested if – else statement- in if statement of if-else statement in place of body


we can use again if or if-else this concept is known as nested structure.
e.g. write a program which reads three numbers and then find the largest one

#include<iostream.h>
#include<conio.h>
void main ( )
{ float a,b,c;
cout<<”Enter three number: “;
cin>>a>>b>>c;
if (a>b)
{
if (a>c)
Cout<< “largest value : “<<a<<endl;
else
Cout<<”largest value: “<<c<<endl;
}
else
{
if (b>c)
Cout<<”largest value : “<<b<<endl;
else
Cout<<”largest value :”<<c<<endl:
}

4
Introduction to computer & programming

4. Switch Statement
The switch or switch-case statement is an alternate of nested if-else structure. This is a
multi-way decision maker that tests whether an expression matches one of the numbers of
values. The syntax of switch statement is as follow:
switch(expression)
{
case value 1:
Statement 1;
Statement 2; first
. case
. body
Statement n;
break;
case value 2:
statement 1;
statement 2; second
. case
. body
Statement n;
break;

case value n:
statement 1;
statement 2; nth
. case
. body
Statement n;
Break;
default:
statement 1;
this is optional we statement 2; default
can skip this also . body
.
Statement n;
}

This expression must be integer or character type. That means the value 1, value 2,…,
value n all are also integer or character type. In switch statement; switch, case, break and
default are reserve word. Switch first calculate the expression then match that expression
with value 1 if both are equal then first case body is executed and switch is terminated
because of break statement, which is written at last of every body. If we skip the break
statement then after executing first body switch is not terminated this match with next
value and so on. If expression is not equal to value 1, then expression checked with value
2 if equal execute second case body and switch is terminated.

5
Introduction to computer & programming
If expression is not equal to value 2 then expression checked with value 3 if there
is no match process continue to last value.
If expression is not match with any value from value 1 to value n then default
body is executed. The diagram for execution of switch statement is:

If expression true First case body


equal to value 1
executed

false

If expression true
equal to value 2 second case body
executed

false

If expression true nth case body


equal to value
executed
n

false

default body
executed

Loop Statement or Repetitive Statement


Loop cause a section of our program to be repeated a certain number of times. The
repetition continues up to when a condition is true. There are three kinds of loop in C++.
They are for loop, while loop and do while loop.
1. for loop
for loop is easiest loop, this consists three expressions. The syntax of for loop is as
follow:
for ( initialization expression; test expression; assignment)
body of loop;
6
Introduction to computer & programming
if in body of loop there is single statement then for loop look like:

for ( initialization expression; test expression; assignment)


statement;

If body of loop consists multiple statements that look like:

for ( initialization expression; test expression; assignment)


{ statement 1;
Statement 2;
Statement 3;
.
.
Statement n;
}

E.g. for (i=0; i<=0; i++)


Statement;
The for statement execute like following:
First the initialization expression executes. Then test expression is checked if it is false
loop terminate, if this is true body of loop is executed. After this assignment assign a new
value (that increment or decrement the variable) to variable. Then again test expression is
checked if false loop terminate otherwise body of loop again execute. This process repeat
until the test expression becomes false. The diagram for this is as follow:

Initialization
expression

Test false
expression

true
Body of loop
executed

Assignment

7
Introduction to computer & programming

2. while loop
The while loop allows programs to repeat a statement or series of statements, over and
over, as long as a certain test condition is true. The format is:

while(test condition)
{
block of code;
}

The test condition must be enclosed in parentheses. The block of code is called the
body of the loop and is enclosed in braces and indented for readability. (The braces are
not required if the body is composed of only ONE statement.) Semi-colons follow the
statements within the block only.

When a program encounters a while loop, the test condition is evaluated first. If the
condition is TRUE, the program executes the body of the loop. The program then
returns to the test condition and reevaluates. If the condition is still TRUE, the body
executes again. This cycle of testing and execution continues until the test condition
evaluates to 0 or FALSE. If you want the loop to eventually terminate, something within

the body of the loop must affect the test condition. Otherwise, a disastrous INFINITE
LOOP is the result! The while loop is an entry-condition loop. If the test condition is
FALSE to begin with, the program never executes the body of the loop. The diagram of a
while loop is as follow:

expression false

true

Body of loop

The following program fragment prints and counts the characters of a string array:
apstring name="Corky";
i = 0; //begin with the first cell of array
while (i< name.length( )) // loop test condition
{
cout<< name[ i ] << endl; //print each character
i++; //increment counter by 1
}

8
Introduction to computer & programming
cout << "There are " << i << " characters.\n";
\*This last cout prints when the test condition is false and the loop terminates*\

3. do-while loop
In a while loop, the condition is evaluated at the beginning of the loop. If the condition is
false when the loop is entered, the loop body will not be executed at all. But in do-while
loop first the body of loop is executed then the condition is evaluated. In do-while loop
the body of loop always executes at least one time if the condition is false also.
The syntax of do-while loop is:
do
body of loop;
while (condition);

If body of do-while loop consists multiple statements then that look like:

do
{
Statement 1;
Statement 2;
.
The diagram of do-while loop execution
. is as follow:
Statement n;
}
while (condition);

Body of loop

expres false
sion

true

9
Introduction to computer & programming
void main()
{ int n, sum=0,i=1;
cin>>n;
do {
sum=sum + 1;
I = I + 2;
}
while (I <=n);
cout<<sum;
}

Breaking Control Statements

C++ provides three different type of breaking control statements. With the help of
breaking control statement the control structure power is improved.

1. Break statement

Break statement causes an exit from a loop just as it does from a switch statement. Break
terminates the loop in which that is written. We can use break in any loop. The break
statement terminates the execution of the nearest enclosing loop or conditional statement
in which it appears. Control passes to the statement that follows the terminated statement,
if any. break is used with the conditional switch statement and with the do, for, and
while loop statements. In a switch statement, break causes the program to execute the
next statement after the switch. Without a break statement, every statement from the
matched case label to the end of the switch, including the default, is executed.
In loops, break terminates execution of the nearest enclosing do, for, or while statement.
Control passes to the statement that follows the terminated statement, if any.
Within nested statements, the break statement terminates only the do, for, switch, or
while statement that immediately encloses it. You can use a return or goto statement to
transfer control from within more deeply nested structures.
e.g. for(i=0;i<=100;i++)
{ statement 1;
Statement 2;
break;
Statement 3;
}
Statement 4;
In the above example statement 3 is not executed because before that there is a break
statement.
2. Continue Statement

The continue statement is used for inverse operation of the break statement. The continue
statement transfers control to start of loop. The syntax of continue statement is similar to
break. Syntax of break break;

10
Introduction to computer & programming
Syntax of continue continue; both are reserve words.
E.g. while(condition)
{ statement 1;
Statement 2;

In the above example if a>b is true the control is transferred to start means again

if(a>
condition is checked and statement is executed. The arrow shows the flow of control.
3. go to statement
By go to statement we can transfer the control to some other part. Means by go to we can
change the execution sequence of program. The general syntax of go to is:

b)
goto label;
go to is reserve word. Label is a name given by the user or programmer but it must be a
valid identifier. E.g. goto start;
goto A23;

conti
but goto 23A; is invalid because 23A is not valid identifier.
Example1. statement 1;
statement 2;
start: statement 3;

nue; statement 4;
goto start;

example2. statement 1;
statement 2;
B: statement 3;
Statement 4;
if ( a > b ) goto B;

State
ment
3;
}
11

You might also like