0% found this document useful (0 votes)
4 views9 pages

CH 4 Control ST CJR

Uploaded by

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

CH 4 Control ST CJR

Uploaded by

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

1/9

Control Statements
Control statements in C++: Control statements allow decision making within programs.
Control statements come in several different forms to provide flexibility in the
controlling of program execution. One form is to optionally execute C ++ statements.
Another form is to form a loop, where all the statements in a loop are executed repeatedly
until the exit condition is satisfied. Another form is to branch to a certain statement and
continue execution.
4.1. Decision making with if statement: The if statement is a powerful decision making
statement and is used to control the flow of execution of statements. It is basically a two-
way decision statement and is used in conjunction with an expression.
if can be used in different forms depending upon nature of conditional test.
1. If 2. If…Else 3. Nested – If.
4.11. Simple if statement:
The general form of a simple if statement is
if (test expression)
{
statement-block;
}
statement-x;

The ‘statement block’ may be a single statement or a group of statements. If the test
expression is true, the statement-block will be executed; otherwise the statement-block
will be skipped and the execution will jump to the statement-x. Remember, when the
condition is true both the statement-block and the statement-x are executed in sequence.
This is illustrated in the following figure.
Entry
Eg., 1) if (x>y)
cout<<” x is greater than y”;
2) if )(marks>250) && (marks <300))
Test False cout << “Second Division”;
expression
? Program 1) To state the given no is +ve.
\\ To display number is positive.
#include<iostream.h>
True
main()
{
Statement-block int num;
cout<<” enter a number\n”;
cin>>num;
Statement-x if (num>0)
cout<<” number is positive ”;
Fig: Flowchart of simple if statement }

1/9
4.1.2. The if – else statement:
The if…else statement is an extension of the simple if statement. The general
form is
if (test expression)
{
True block statement(s);
}
else
{
False block statement(s);
}
statement-x;

If the test expression is true, then the true-block statement(s), immediately following
the if statement are executed; otherwise, the false-block statement(s) are executed. In
either case, either true-block or false-block will be executed, not both. This is illustrated
in the following Fig. In both cases, the control is transferred subsequently to statement-x.

Entry Eg., 1) if (total>=250)


cout<<” result Pass”;
else
cout<<” result Fail”;
True Test False Program: To find whether the given
expressio number is even or odd.
n // To find whether given number is
? even or odd.
#include<iostream.h>
main()
True-block False-block {
statements statements int no;
cout<<”enter a number”;
cin>>no;
if ( no %2 == 0 )
Statement-x
cout<<no<<”is an even number”;
else
Fig: Flowchart of if…else statement cout<<no<<”is an odd number”;
}

4.1.3. Nested if statement: When a series of decisions are involved, we may have to use more
than one if…else statement in nested form as follows:
if (test condition 1) Example:
{ // To find whether the given number is +ve or –ve or
if (test condition 2) zero.
{ #include<iostream.h>
statement-1; void main()
} {
else int n;
{ cout<<”Enter any number :”;
statement-2; cin>>n;
} if (n>0)
} cout<<”The given number is +ve”<<endl;
else else if (n<0)
{ cout<<”The given number is negative”<<endl;
statememt-3; else
} cout<<”The given number is zero”;
statement-x; }

If the condition-1 is false, the statement-3 will be executed; otherwise it continues to perform
the second test. If the condertion-2 is true, the statement-1 will be evaluated; otherwise the
statement-2 will be evaluated and then the control is transferred to the statement-x.
Example: Write a C++ Program to calculate tax to be paid by an employee based on
following conditions:
(1) income up to 20,000 no tax.
(2) 20,000 < income <= 30, 000 tax = 20% for next 10,000.
(3) 30,000 < income <= 50,000 tax = 30% for next 20,000.
(4) 50,000 < income <=1, 00,000 tax = 40% for next 50,000.
(5) income > 1,00,000 tax = 50% for remaining part.

#include <iostream.h>
main ()
{
float income, tax;
cout<<”enter the total income of the employee\n”;
cin>>income;
if(income<=20000)
tax = 0.0;
else if (income <=30000)
tax = (income – 20000) * 0.20;
else if (income <= 50000)
tax=(income-30000) * 0.30 + 2000;
else if (income <= 1000000)
tax = (income –50000) * 0.40 +8000;
else
tax = (income –100000) *.50 +28000;
cout<<"tax = "<<tax;
}
4.2. The switch statement:
This is a multiple branching control statement. This is useful for decision making when
more than one case is involved.
The general form of switch statement is as follows:

switch (expression)
{
When case label 1: statement sequence ; switch is executed value of expression is
break; compared against label 1, label 2, label 3
etc. If case label 2: statement sequence ; a case is found whose value matches the
break; expression, then sequence of statements
that case label 3: statement sequence ; follow the case is executed.
The break; break at the end of each block indicates
end of : particular case & causes exit from the
switch : statement, transferring control to outside
the default: statement sequence ; switch statement.
The break; default case is optional case. When
} present, it will be executed if the value of
the expression does not match with any of the
case values.
Example: See at the end of this handout.
4.3. The while statement:
The simplest of all the looping structures in C++ is the while statement.
The basic format of the while statement is
while (test condition)
{
body of the loop
}
The while is an entry-controlled loop statement. The test-condition is
evaluated and if the condition is true, then the body of the loop is executed. After
execution of the body, the test-condition is once again evaluated and if it is true the
body is executed once again. This process of repeated execution of the body continues
until the test-condition finally becomes false and the control is transferred out of the
loop.
The body of the loop may have one or more statements. The braces are
needed only if the body contains two or more statements.
The flowchart of while loop is as follows:

Yes
while Statements
(Con
d)

No

out of loop
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;
i=2;
while (i <= n)
{
sum = sum + i;
i = i + 2;
}
cout<<"sum= "<<sum; }
4.4. The do-while statement: The basic form of the do-while statement is as
follows:

do
{
body of the loop
}
while (test-condition);

On reaching the do statement, the program proceeds to evaluate the body


of the loop first. At the end of the loop, the test-condition in the while statement is
evaluated. If the condition is true, the program continues to evaluate the body of the
loop once again. This process continues as long as the condition is true. When the
condition becomes false, the loop will be terminated and the control goes to the
statement that appears immediately after the while statement.
Since the test-condition is evaluated at the bottom of the loop, the
do...while provides an exit-controlled loop and therefore the body of the loop is
always executed at least once.
The flowchart of while loop is as follows:
do
Example:
sum = 0;
Statements i = 1;
do
{
True sum += i;
Cond. i += 2;
? } while (i >= 100);

False

Out of loop
4.5. for loop: - This is used when we know the number of times the instructions are
to be executed. Syntax : for (exp.1; exp.2; exp.3)
{
Sequence of statements;
}
Here exp.1 – initial value
exp.2 – Testing condition,
exp.3 - step value (increment /decrement)

(exp. 1)

No
exp.
2;
Yes
Out of for loop
Statements;

exp.3;

eg. - 1. sum = 0;
for (i=0; i <= 100 ; i++) no semicolon
sum += i;
Following steps are involved in the code:

Initialization condition checking body of the loop

increment/ decrement again condition checking.

2. fact = 1;
for (i = 1; i < n ; i++)
fact *= i ;

Program: – To display all odd numbers & their sum up to 50.


#include<iostream.h>
void main()
{
int i , sum = 0;
cout<<”list of odd numbers up to 50:\n”;
for (i=1 ; i <= 50 ; i=i+2)
{
Cout<<i<<” ”;
sum += i;
}
cout<<” \n Sum is: ”<< sum;
}
4.6. break statement: The break statement causes an immediate exit from the do,
for, switch, and while statement in which it appears, the program continues executing
with the next program statement following the do, for, switch, and while statement
block..
The format of the break statement is simply
break;
The behavior of the break statement is showed below.,

while (---------) Example:


{ i=1;
--------- while (1)
--------- {
if (condition) cout<<i<<" ";
break; if (i==10)
--------- break;
--------- i++;
} }
-------- Output: 1 2 3 4 5 6 7 8 9 10

4.7. continue statement: The continue statement tells the compiler, “skip
the
following statements and continue with the next iteration.”
The format of the continue statement is simply

continue;

The use of the continue statement in loops is illustrated in the following figure. 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.

while (---------)
{
---------
---------
if (condition)
continue;
---------
---------
}
-------- Program to accept 10 numbers and
to add only
the positive neglecting the negative numbers.
// to add only +ve numbers
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n,sum=0;
cout<<"enter any 10 numbers\n";
for(i=0;i<=10;i++)
{
cin>>n;
if (n<0)
continue;

sum += n;
}
cout << " sum of +ve numbers = "<<sum;
getch();
}

//Read sides of triangle and find right angle or not.


#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
char ch;
cout<<"enter three sides ";
cin>>a>>b>>c;
if(a>b && a>c)
ch='a';
else
if(b>a && b>c)
ch='b';
else
ch='c';
switch(ch)
{
case 'a':
if((a*a)==(b*b+c*c))
cout<<"a right angle triangle ";
else
cout<<"not";
break;
case 'b':
if((b*b)==(a*a+c*c))
cout<<"b right angle triangle ";
else
cout<<"not";
break;
case 'c':
if((c*c)==(b*b+a*a))
cout<<"c right angle triangle ";
else
cout<<"c is not a right angle triangle";
break;
}
}

//program to print a pyramid


#include<iostream.h>
#include<conio.h>
void main()
{
int n,i,j;
cout<<"enter n ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
cout<<"* ";
cout<<"\n";
}
}
/* program to print a pyramid
output:
enter n 5
*
**
***
****
* * * * * */
// Program to find the first n break;
prime numbers. }
#include<iostream.h> if (sum==0)
#include<conio.h> {
void main() count++;
{ cout<<k<<" ";
int n,j,sum=0,count=0; }
unsigned long int k; if(count==n)
cout<<"enter n "; break;
cin>>n; }
for(k=2;k<=4294967295;k++) getch();
{ }/* enter n 9
sum=0; 2 3 5 7 11 13 17 19 23 */
for(j=2;j<=k/2;j++)
if ((k%j)==0)
{
sum++;

You might also like