Chapter-5 Control Instruction
Chapter-5 Control Instruction
Chapter-5
Control Instruction
What is control?
Program is a set of instructions. We know that each instruction of the program is
executed by processor. Processor executes instructions one by one. What ever we write in
our program will be executed in the same order as they appear in the program. So we can
say processor execute instructions in a sequential manner. At a particular instant
processor is executing some line of code, we say control of processor is on that line.
Processors control moves from one line to another. We say this movement of processor
control goes in sequence.
Types of control instruction
l Sequence control instruction
l Decision control instruction
l Iterative control instruction
l Switch case control instruction
l goto instruction
Sequence control instruction
Processor control moves in a sequential manner. We have to do nothing to implement
sequence control instruction. This is just a concept that your program always run in a
sequence.
Decision control instruction
Decision control instruction is also known as selection control instruction. As the name
implies the job is selection control instruction is to select a set of code for execution on
the basis of some condition.
We can implement decision control instruction in three ways:
l if
l if-else
l conditional operator (Ternary Operator)
Syntax of if
main()
{
..
..
if(some condition)
{
Statement1;
Statement2;
}
if is a keyword which let compiler to identify decision control instruction. Immediately
after if some condition is there. This condition is any valid expression in C. If the result
of expression is non-zero it is considered as TRUE otherwise FALSE.
Immediately after this condition there is a block of code. Since this block is immediately
after if, it is known as if block. Whatever we write in if block will be execute only when
condition is TRUE.
When condition is false control skip if block and execute statements written after if
block.
Example:
main()
{
int marks;
printf(Enter marks );
scanf(%d,&marks);
if(marks>=33)
{
printf(You are PASS);
}
if(marks<33)
{
printf(You are FAIL);
}
}
Sample Output:
Enter marks 45
You are PASS
Sample Output:
Enter marks 23
You are FAIL
In this program output depends on the value given by user. Variable marks hold the value
entered by user. We have used two if statements. In the first if statement we use the
condition marks>=33, thus if the marks are greater than or equal to 33 condition becomes
TRUE, so if block executed, otherwise if block is skipped.
Whatever may the result of first if condition, control has to reach second if statement.
If marks are less than 33 condition will be TRUE and execute if block otherwise if block
is skipped.
}
This is similar to if but the else block is new add on. If the condition of if is TRUE if
block will be executed and if the condition of if is FALSE else block will be executed.
It is important to mention that only one from the two blocks (if block and else block) can
be executed as the condition may have only two results, TRUE or FALSE
You can use if statement without else block but else must have paired with if.
Else block should appear immediately after if block otherwise an error occurred during
compilation.
Example:
main()
{
int marks;
printf(Enter marks );
scanf(%d,&marks);
if(marks>=33)
{
printf(You are PASS);
}
else
{
printf(You are FAIL);
}
}
Selective assignment
main()
{
int x,y,max;