CH 3
CH 3
Program is usually not limited to a linear sequence of instructions. During its process it
may bifurcate, repeat code or take decisions. For that purpose, C++ provides control
structures that serve to specify what has to be done to perform our program.
With the introduction of control sequences we are going to have to introduce a new
concept: the block of instructions. A block of instructions is a group of instructions
separated by semicolons (;) but grouped in a block delimited by curly bracket signs:
{ and }.
Most of the control structures that we will see in this section allow a generic statement
as a parameter, this refers to either a single instruction or a block of instructions, as we
want. If we want the statement to be a single instruction we do not need to enclose it
between curly-brackets ({}). If we want the statement to be more than a single instruction
we must enclose them between curly brackets ({}) forming a block of instructions.
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.
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.
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 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.
#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;
}
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.
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);
False
Out of loop
1.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;
2. fact = 1;
for (i = 1; i < n ; i++)
fact *= i ;
}
OUTPUT: 1 + 3 +5 +7 + 9 + 11……………..+49 , SUM =625
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.,
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
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.
sum += n;
}
cout << " sum of +ve numbers = "<<sum;
getch();
}
THE END!!!