0% found this document useful (0 votes)
48 views

Chapter 7 Control Structures

The document discusses different types of control structures in C programming. It describes decision making statements like if, if-else, nested if-else, else-if ladder and switch statements which allow conditional execution of code. It also covers loop control statements like while, do-while and for loops that execute a block of code repeatedly as long as a condition is true. Specific examples are provided for each statement to illustrate their syntax and usage.

Uploaded by

syxdmatheen.9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Chapter 7 Control Structures

The document discusses different types of control structures in C programming. It describes decision making statements like if, if-else, nested if-else, else-if ladder and switch statements which allow conditional execution of code. It also covers loop control statements like while, do-while and for loops that execute a block of code repeatedly as long as a condition is true. Specific examples are provided for each statement to illustrate their syntax and usage.

Uploaded by

syxdmatheen.9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 7

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.

There are two types of control statements in C-

1. Decision making control statements:

2. Loop control statements:

These statements are used to execute a group of statements repeatedly.

1. Decision making control statements:

In many situations it is necessary to use some conditions to make a decision.


Based on decision we can execute only a list of statements. The condition is
normally comparison between expressions.

The different control statements available in C are:

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

i. if (< condtional expressione>)


statement-1;
(or)
ii. if (< condtional expressione>)
{
statement-1;
statement-2;
statement-3;
……………
Statement-N;
}
void main()

{
int n;

printf (“ enter a number:”);

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;

printf (“enter a number:”);

sacnf (“%d”, &n);


If (n%2==0)
printf (“even number”);

else

printf(“odd number”);

}
Output: enter a number:121

odd number
c. Nested if-else statements (If statement within another if statement)

Including numerous if-else statements inside an if and else statement is


called nesting.

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.

When a logical expression is encountered whose value is true the


corresponding statements will be executed and the remainder of the nested else
if statement will be bypassed. Thus control will be transferred out of the entire
nest once a true condition is encountered.
The final else clause will apply if none of the exp is true.
Example:
#include<stdio.h>
int main()
{
int day;
printf("Enter day number: ");
scanf("%d", &day);
if(day==1)
{
printf("SUNDAY.");
}
else if(day==2)
{
printf("MONDAY.");
}
else if(day==3)
{
printf("TUESDAY.");
}
else if(day==4)
{
printf("WEDNESDAY.");
}
else if(day==5)
{
printf("THURSDAY.");
}
else if(day==6)
{
printf("FRIDAY.");
}
else if(day==7)
{
printf("SATURDAY.");
}
else
{
printf("INVALID DAY.");
}
getch();
}
e. Switch statement
A switch statement is used to choose a statement (for a group of statement)
among several alternatives. The switch statements is useful when a variable is to
be compared with different constants and in case it is equal to a constant a set of
statements are to be executed.
Where constant1, constanat2 — — — are either integer constants or
character constants. When the switch statement is executed the exp is evaluated
and control is transferred directly to the group of statement whose case label
value matches the value of the exp. If none of the case label values matches to
the value of the exp then the default part statements will be executed.
If none of the case labels matches to the value of the exp and the default
group is not present then no action will be taken by the switch statement and
control will be transferred out of the switch statement.

Rules that must be remembered while using switch statement:

1. Expression and constant must be of type either integer or character.


2. All the cases must be distinct.
3. The block of statement under default will be executed when none of the
cases match the value of expression.
4. The break statement causes exit from switch statement. If it is not
present, after executing the case which match the value of the expression,
the following other cases are executed.
Example:
#include<stdio.h>

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();
}

2. Loop Control structures

Loop statements are used to execute the statements repeatedly as long as an


expression is true. When the expression becomes false then the control
transferred out of the loop. There are three kinds of loops in C.
a) while b) do-while c) for

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

Difference between While and do-while loop

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

Continue statement (key word continue)


Continue statement is used for continuing next iteration of loop after skipping
some statement of loop. When it encountered control automatically passes
through the beginning of the loop. It is usually associated with the if statement.
It is useful when we want to continue the program without executing any part of
the program.

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

The go to statement is used to alter the program execution sequence by


transferring the control to some other part of the program.

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;
}

You might also like