Chapter 7 Control Structures
Chapter 7 Control Structures
Control structures
Generally C program statement is executed in a order in which they appear in
the program. But sometimes we use decision making condition for execution
only a part of program, called control statement.
Control statements are used to control the flow of the program. They
specify the order in which the various statements should be executed.
Control statement defines how the control is transferred from one part to the
other part of the program. There are several control statement like if...else,
switch, while, do....while, for loop, break, continue, goto etc.
a. Simple if statement
b. if else statement
c. nested-if statement
d. else-if ladder
e. switch statement
a. Simple if statement:
The if statement is used to express conditional expressions. If the given
condition is true then it will execute the statements otherwise skip the
statements.
The statement is executed only when condition is true. If the condition is false
then compiler skip the lines within the if block and execution continues with the
next statements.
Note: If the if statement body is consists of several statement then better to use
pair of curly braces. There should not be (;) at the end of closind brackets after
the condition.
The simple structure of ‘if’ statement is
{
int n;
scanf(“%d”,&n);
If (n>10)
printf(“ number is grater”);
}
Output:Enter a number:12
Number is greater
b. if-else statement
The if-else statement in the C programming language allows you to execute a
block of code if a certain condition is true, and another block of code if the
condition is false. It provides a way to make decisions and control the flow of
the program based on different conditions.
if(<exp>)
{
Statement-1;
Statement -2;
…………..
……………
Statement- n;
}
else
{
Statement1;
Statement 2;
…………..
……………
Statement- n;
}
Example:
void main()
{
int n;
else
printf(“odd number”);
}
Output: enter a number:121
odd number
c. Nested if-else statements (If statement within another if statement)
When the outer if condition is true, the outer if’s if block will be executed.
When the outer if block is false, the outer if’s else block is executed.
Example:
#include <stdio.h>
int main()
{
int a=4, b=6, c=9;
if (a > b)
{
if (a > c)
{
printf("A is greatest%d", a);
}
else
{
printf("c is the greatest %d", c);
}
}
else
{
if (b > c)
{
printf("b is the greatest %d", b);
}
else
{
printf("c is the greatest %d", c);
}
}
Getch()
}
d. else-if ladder
The else-if ladder is a Multi-way decision making statement which contains two
or more else-if from which any one block is executed.
int main()
{
int day;
printf("Enter day number: ");
scanf("%d", &day);
switch(day)
{
case 1: printf("SUNDAY.");
break;
case 2: printf("MONDAY.");
break;
case 3: printf("TUESDAY.");
break;
case 4: printf("WEDNESDAY.");
break;
case 5: printf("THURSDAY.");
break;
case 6: printf("FRIDAY.");
break;
case 7: printf("SATURDAY.");
break;
default: printf("INVALID DAY.");
break;
}
getch();
}
a) While
The test condition may be any expression .when we want to do something a
fixed no of times but not known about the number of iteration, in a program
then while loop is used.
Here first condition is checked if, it is true body of the loop is executed else, If
condition is false control will be come out of loop.
The condition is called as entry-controlled loop or Pre-tested loop because the
condition is tested in the beginning of the while loop.
Example:
Example:-
/* wap to print 5 times welcome to C” */
#include<stdio.h>
void main()
{
int p=1;
While(p<=5)
{
printf(“Welcome to C\n”);
p=p+1;
}
}
b) do-while
do while loop statement is also used for looping. The body of this loop may
contain single statement or block of statement. The syntax for writing this
statement is:
Syntax:-
do
{
Statement;
}
while (condition);
Here firstly statement inside body is executed then condition is checked. If the
condition is true again body of loop is executed and this process continue until
the condition becomes false. In do-while loop semicolon is placed at the end of
while.
Do-while loop is called as exit-controlled loop or post-tested loop because the
condition is tested after executing the body of the loop.
Example:-
#include<stdio.h>
void main()
{
int X=1
do
{
printf(“%d”,X);
X=X+1;
}while(X<=5);
}
Output: 1 2 3 4 5
There is minor difference between while and do while loop, while loop test the
condition before executing any of the statement of loop. Whereas do while loop
test condition after having executed the statement at least one within the loop.
If initial condition is false while loop would not executed it’s statement on other
hand do while loop executed it’s statement at least once even If condition fails
for first time. It means do while loop always executes at least once.
Note:
Do while loop used rarely when we want to execute a loop at least once.
c) for loop
In a program, for loop is generally used when number of iteration are known in
advance. So it is known as Fixed execution loop. The body of the loop can be
single statement or multiple statements.
For loop is called pre-tested loop because it tests the condition and if condition
is true, then only it executes the statements within the loop.
Syntax:
for(exp1;exp2;exp3)
{
Statement;
}
Or
for(initialized counter; test counter; update counter)
{
Statement;
}
Here exp1 is an initialization expression, exp2 is test expression or condition
and exp3 is an update expression. Expression 1 is executed only once when loop
started and used to initialize the loop variables. Condition expression generally
uses relational and logical operators. And the updation part executed only when
after body of the loop is executed.
Example:-
void main()
{
int i;
for(i=1;i<10;i++)
{
Printf(“ %d ”, i);
}
}
Output:-1 2 3 4 5 6 7 8 9
Format - I
for( ; exp2; exp3 )
Statements;
In this format the initialization expression (i.e., exp1) is omitted. The initial
value of the variable can be assigned outside of the for loop.
Example 1
int i = 1;
for( ; i<=10; i++ )
printf (“%d \n”, i);
Format - II
for( ; exp2 ; )
Statements;
In this format the initialization and increment or decrement expression (i.e
expression-1 and expression-3) are omitted. The exp-3 can be given at the
statement part.
Example 2
int i = 1;
for( ; i<=10; )
{
printf (“%d \n”,i);
i++;
}
Format - III
for( ; ; )
Statements;
If the terminating condition is not specified then it enters infinite loop. We need
to press break or return to end the loop.
Note: The semicolon at the end of the statement indicates that the body of the
loop is empty.
Nesting of loop
When a loop written inside the body of another loop then, it is known as nesting
of loop. Any type of loop can be nested in any type such as while, do while, for.
For example nesting of for loop can be represented as :
Example:
#include <stdio.h>
void main()
{
int i,j;
for(i=0;i<2;i++)
for(j=0;j<5; j++)
printf(“%d %d”, i, j);
}
Output: i=0
j=0 1 2 3 4
i=1
j=0 1 2 3 4
38
Jumps in loops
For effective handling of the loop structures, C allows the following types of
control break statements.
a. Break Statement b. Continue Statement
a) break statement
Sometimes it becomes necessary to come out of the loop even before loop
condition becomes false then break statement is used. Break statement is used
inside loop and switch statements. It cause immediate exit from that loop in
which
it appears and it is generally written with condition. It is written with the
keyword as break. When break statement is encountered loop is terminated and
control is transferred to the statement, immediately after loop or situation where
we want to jump out of the loop instantly without waiting to get back to
conditional state.
When break is encountered inside any loop, control automatically passes to the
first statement after the loop. This break statement is usually associated with if
statement.
Example :
void main()
{
int j=0;
for(;j<6;j++)
if(j==4)
break;
}
Output:
0123
The continue statement can be used only within the loop. If it is used without
the loop then there will be a compiler error.
The continue statement is used to bypass the remainder of the current pass
through a loop. The loop does not terminate when a continue statement is
encountered. Rather, the remaining loop statements are skipped and the
proceeds directly to the next pass through the loop. The “continue” that can be
included with in a while a do-while and a for loop statement.
General form :
continue;
The continue statement is used for the inverse operation of the break statement.
Example
while (x<=100)
{
if (x <= 0)
{
printf (“zero or negative value found \n”);
continue;
}
}
The above program segment will process only the positive whenever a
zero or negative value is encountered, the message will be displayed and it
continue the same loop as long as the given condition is satisfied.
Break;
1. Break is a key word used to terminate the loop or exit from the block. The
control jumps to next statement after the loop or block
2. Break statements can be used with for, while, do-while, and switch statement.
When break is used in nested loops, then only the innermost loop is terminated.
3. Syntax:{ statement1; statement2; statement3; break;}
4. Example :Switch ( choice){ Case ‘y’: printf(“yes”); break; Case ‘n’:
printf(“NO”); break;}
5. When the case matches with the choice entered, the corresponding case block
gets executed. When ‘break’ statement is executed, the control jumps out of the
switch statement.
Continue
1. Continue is a keyword used for containing the next iteration of the
loop
2. This statement when occurs in a loop does not terminate it rather skips
the statements after this continue statement and the control goes for
next iteration. ‘Continue’ can be used with for, while and do- while loop.
3. Syntax:
{ statement1;
continue;
statement2;
statement3;
break;
}
Difference between break and continue statement is :
The difference between break and continue is, when the break encountered loop
is terminated and it transfer to the next statement and when continue is
encounter control come back to the beginning position.
Infinite loop:
The loop which is never finished is known as infinite loop. It means the looping
condition is always true, so that the loop never terminates.
Example:-
void main()
{
int n;
for(n=2; n<=9; n++)
{
if(n==4)
continue;
printf(“%d”, n);
}
}
Printf(“out of loop”);
}
Output: 2 3 5 6 7 8 9 out of loop
c) Unconditional Branching (Go To Statement)
goto statement
Syntax
Where label is an identifier used to label the target statement to which the
control would be transferred the target statement will appear as:
goto<label>;
label :
statements;
Each label must be unique.
Example:
Example 1
#include <stdio.h>
main();
{
int a,b;
printf (“Enter the two numbers”);
scanf (“%d %d”,&a,&b);
if (a>b)
goto big;
else
goto small;
big :printf (“big value is %d”,a);
goto stop;
small :printf (“small value is %d”,b);
goto stop;
stop;
}