UNIT-II
Decision Control and Loop Control
⚫ Repeating important sections of the program.
⚫ Selecting between optional section of a
program
Decision
Making
Statement
Decision Decision
Making and Making and
Branching Looping
SWITCH Conditional GOTO The WHILE The do-while The FOR
IF Statement Operator
Statement Statement Loop Loop Loop
Statement
• DECISION MAKING AND BRANCHING
In programming the order of execution of instructions
may have to be changed depending on certain conditions.
This involves a kind of decision making to see
whether a particular condition has occurred or not and
then direct the computer to execute certain instructions
accordingly.
• DECISION MAKING AND LOOPING
Execution of the statement or set of statement
repeatedly is known as LOOPING.
This may be executed a specified number of times and
this depends on the satisfaction of a test condition.
A program loop is made up of two parts one part is known
as body of the loop and the other is known as control
condition.
Depending on the control condition statement , the
⚫ The IF statement is the powerful decision making
statement and is used to control the flow of execution of
the statements.
⚫ When decision making in a program they are simply the
result of computation in which the final result is either
TRUE or FALSE.
⚫ The value zero (0) is considered to be FALSE in program.
Any positive or negative value is considered to be
TRUE.
Levels of complexity for IF:
1.Simple IF statement
2.IF…..ELSE statement
3. NESTED IF…..ELSE statement
4. ELSE…..IF ladder
It is used to control the flow of execution of the
statements and also to test logically whether the
condition is true or false.
Syntax:
if (condition)
{
statement ;
}
If the condition is true then the statement following
the “if” is executed if it is false then the statement is
skipped.
Test TRUE
Condition
Executable X - Statement
( Flow chart of Simple if
statement )
The if…..else statement is an extension of the simple if
statement.
Syntax:
if (condition)
{
statement 1; (if the condition is TRUE this statement will be
} executed)
else
{
statement 2; (if the condition is FALSE this statement will be executed)
}
It is used to execute some statements when the
condition is true and execute some other statements
when the condition is false depending on the logical
test.
FALS Test TRUE
E Condition
Executable Y - Statement Executable X - Statement
( Flow chart of if…..else statement )
⚫ When a series of if…..else statements are occurred in a program, we can
write an entire if…..else statement in another if…..else statement called
NESTING.
Syntax:
If (condition 1)
{
if (condition 2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
if (condition 3)
statement 3;
else
statement 4;
( Flow chart of nested if……else
statement )
FALS Tes TRUE
E Condition-1
t
FALSE Test TRUE
Condition-1
Statement-2 Statement-3
Test
Condition-3
FALS TRUE
E
Statement-3 Statement-4
When a series of decisions are involved we have to use more than one
if…..else statement called as multiple if’s. Multiple if…..else
statements are much faster than a series of if…..else statements, since
their structure when any of the condition is satisfied.
Syntax:
if (condition 1)
statement 1;
else if (condition 2)
statement 2;
else if (condition 3)
stat
ement
3;
e
lse
if
(co
( Flow chart of else…..if ladder )
FALS TRUE
Test Statement 1
E
Condition 1
FALS Test TRUE
E Statement 2
Condition 2
FALS Test TRUE
Condition Statement 2
E
3
FALS
Test TRUE
E
Condition Statement n
n
Statement X
⚫ The control statement which allows us to make decision
from the number of choices is called switch or switch case
statement.
⚫ It is a multi way decision statement, it test the given
variable or expression against a list of case values.
switch (expression)
{
case constant 1:
block 1
break;
case constant 2:
block 2
break;
default:
default block
break;
}
Statement –x;
(Selection process of the SWITCH Statemen
Switch
expression
Expression = value -1
Block-1
Expression = value -2
Block-2
(no match) default default
block
Statement-x
⚫ This operator is a combination of ? And : , and
takes three operands.
⚫ General form:
conditional expression ? exp-1:exp-2;
⚫ This concept says that if this expression is true
then whatever we are writing after this question
mark statement, it will be executed.
And if this expression is false then whatever
we are writing after this colon statement, it will be
executed.
⚫ A WHILE loop has one control expression,
and executes as long as that expression is
true.
Syntax:
While (condition)
{
statement (s);
increment or decrement loop
counter
}
⚫ A WHILE loop is an entry controlled
loop.
Start
Initialize
Test FALS
E
Condition
TRUE
Stop
Body of Loop
Increment or Decrement
Flow chart of WHILE
⚫ The body of the loop may not be executed if the condition is
not satisfied in while loop.
⚫ Since the test is done at the end of the loop, the statement in
the braces will always be executed at least once.
⚫ The statements in the braces are executed repeatedly as long
as the expression in the parentheses is true.
Syntax:
initialize loop counter;
Do
{
statement
(s);
increment
or
decrement
loop counter
}
While (condition);
Start Flow chart of
do-while
LOOP
Initialize
Body of Loop
Increment or Decrement
TRUE Test
Condition
FALS
E
⚫ The for loop is another repetitive control structure, and
is used to execute set of instruction repeatedly until the
condition becomes false.
⚫ To set up an initial condition and then modify some value
to perform each succeeding loop as long as some condition
is true.
Syntax:
for(expr1; expr2; expr3)
{
body of the loop;
}
⚫ The three expressions:
expr1 – sets up the initial condition,
expr2 – tests whether another trip through the loop
should be taken,
expr3 – increments or updates thing after each trip.
Start
Initialize; test condition; Increment / Decrement
Body of Loop
Stop
Flow chart of for
C break statement
The break is a keyword in C which is used to bring the program
control out of the loop. The break statement is used inside loops or
switch statement. The break statement breaks the loop one by one,
i.e., in the case of nested loops, it breaks the inner loop first and
then proceeds to outer loops.
Syntax:
//loop or switch case
break;
Example
#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
}
Continue Statement
The continue statement in C language is used to bring the program control to
the beginning of the loop. The continue statement skips some lines of code
inside the loop and continues with the next iteration. It is mainly used for a
condition so that we can skip some code for a particular condition.
Syntax:
//loop statements
continue;
//some lines of the code which is to be skipped
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;//initializing a local variable
//starting a loop from 1 to 10
for(i=1;i<=10;i++)
{
if(i==5)
{//if value of i is equal to 5, it will continue the loop
continue;
}
printf("%d \n",i);
}//end of for loop
getch();
}