CONTROL INSTRUCTIONS
• In real world, several activities are sequenced, or repeated based on some decisions.
• Constructing control instructions can program such activities.
Types of control Instruction
1. Sequential Control Instruction
2. Selection Control Instruction
3. Loop Control Instruction
4. Case Control Instruction
SEQUENTIAL CONTROL INSTRUCTION
• Here instructions are executed sequential manner
• That is the same order in which they appear in the program.
• By default the instructions in a program are executed sequentially.
Example:
#include<iostream.h> void main()
{
int a,b;
cout<<”Enter the value of a&b”; cin>>a>>b;
int x=a+b;
cout<<”Sum of a & b is”<<x;
}
RUN
Enter the value of a& b 10 5
Sum of a & b is 15
In the above program instructions are executed one after another, in which they appear in the program.
SELECTION CONTROL INSTRUCTION
• Many times, we want a set of instructions to be executed in one situation, and an entirely different set of instruction to be executed in another situation.
• This kind of situation is dealt in C++ by constructing selection control instructions. The following Statements are supporting to construct selection control instructions: 1. simple if statement
2. if-else statement
3. Nested if- else statement
4. Else-if statement
1. Simple if statement(one way decision stmt):
It is a powerful decision making statement, which is used to control the sequence of the execution of statements.
Syntax:
If(test expression)
{
statement Block;
}
statement-x;
Execution Procedure:
The statement Block may be a single statement or a group of statements. If the test expression is true, the statement block is 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 statement x are executed in sequence.
Example:
if( category ==”sports”)
{
marks = marks + bouns_marks;
}
cout<<marks;
The program tests the type of category of the student. If the student belongs to the SPORTS category, then additional bouns_marks are added to his marks befor they are printed. For others, bouns_marks are not added.
2. The if-else statement ( two way decision stmt)
It performs some action even when the test expression fails.
Syntax:
if( test expression)
{
true block statements;
}
else
{
false block statements;
{
statement x;
Execution Procedure:
If the test expression is true, then the true block statement, immediately following the statement are executed; otherwise, the false block statements are executed. In either case, either true or false block will be executed, not both. In both the cases, the control is transferred subsequently to statement x.
Example:
#include<iostream.h> void main()
{
int age;
cout<<”Enter your age”; cin>>age; if((age>12)&&(age<20))
{
cout<<”you are a teen aged person”:
}
else
{
cout<<”You are not a teen aged person”;
}
cout<<”Program terminated”;
}
Run1
Enter your age 16
You are a teen aged person Program Terminated Run 2
Enter your age 23
You are not teen aged person Program Terminated
3. Nesting of if else statements(Multi way decision stmt)
When a series of decisions involved, we may have to use more than on if – else statement is nested form as follows:
if(test condition 1)
{
if( test condition 2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
statement 3;
}
Statement x;
Execution Procedure:
If the condition 1 is false, the statement 3 will be executed; otherwise it continues to perform the second test. If the condition 2 is true, the statement –1 will bee evaluated; otherwise statement 2 will be executed and then the control, is transferred to statement x.
Example: Program to find largest of 3 nos
#Include<iostream.h> void main()
{
int a,b,c;
cout<<” three nos”; cin>>a>>b>>c; If(a>b)
{
if(a>c)
{
cout<<”a is greatest”;
}
else
{
cout<<”c is greatest”;
}
}
else
{
if(b>c)
{
cout<<”b is greatest”;
}
else
{
cout<<”c is greatest”;
}
}
Run:
Enter 3 nos: 24 56 34 b is greatest
4. else – if statement
There is another way of putting ifs together when multipath decisions are involved. A multipath decision is a chain of ifs in which the statement associated with each else is an if.
It takes the following general form: If( test condition1)
{
statement1;
}
else if (test condition2)
{
statement 2;
}
else if(test condition3)
{
statement 3;
}
else
{
statement 4;
}
statement x;
LOOP CONTROL INSTRUCTIONS
• Loop cause a section of code to be executed repeatedly until a termination condition is met.
• A program loop therefore consist of two segments, one known as the body of the loop and the other known as the control statement.
• Control statement tests certain conditions and then directs the repeated execution of the statements contained in the body of the loop.
The following Statements are supporting to construct loop control instructions:
1. while statement
2. do - while statement
3. for statement
1. while statement(Entry controlled Loop)
While is used when the number of iterations to be performed is not known in
advance.
Syntax:
While(test condition)
{
body of the loop;
}
Statement x;
Execution Procedure:
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.
Example: Display 1----N numbers
#include<iostream.h> void main()
{
int n;
cout<<”how many integers to be displayed”; cin>>n;
int I=1; while(I<=n)
{
cout<<I<<endl;
I++;
}
cout<<”Program Terminated”;
}
Run
How many integers to be displayed: 5 1 2 3 4 5
Program Terminated
2.do-while statement (Exit controlled loop stmt)
• Some times, it is desirable to execute the body of a while loop only once,, even if the test expression evaluates to false during the first iteration.
• This requires testing of termination expression at the end of the loop rather than the beginning as in the while loops.
• So, the do- while loop is called bottom tested loop.
• The loop is executed as long as the test condition remains true.
Syntax: do {
body of the loop;
} while(test condition);
statement x;
Execution Procedure:
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. I 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.
3. for loop statement
For loop is useful while executing a statement a fixed number of times. For statement is the compact way to express a loop.
Syntax
for(initialization; condition; increment/decrement)
{
Body of the loop;
}
statement x;
Execution Procedure:
The Initialization part is executed only once. Next the test condition is evaluated. If the test evaluates to false, then the next statement after the for loop is executed. If the test expression evaluates to true, then body of the loop is executed. After executing the body of the loop, the increment/ decrement part is executed. The test is evaluated again and the whole process is repeated as long as the test expression evaluates to true.
Example: display numbers 1…..n using for loop
#include<iostream.h> void main()
{
int n;
cout<<”how many integers to be displayed”; cin>>n;
int I; for(I=1;I<=n;I++)
{
cout<<I<<endl;
}
cout<<”Program Terminated”;
}
Run
How many integers to be displayed: 5 1 2 3 4 5
Program Terminated