COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Control Structures
Control Structures are just a way to specify flow of control in programs. Control flow
which is also stated as flow of control, determines what section of code is to run in program at a
given time. There are three types of flows, they are
1. Sequential control flow
2. Selection or Conditional control flow
3. Looping or repetition control flow
Sequential control flow:
The name suggests the sequential control structure is used to perform the
action one after another. Only one step is executed once. The logic is top to bottom
approach.
Example
Description: To find the sum of two numbers.
1. Start
2. Read the value of ‘a’
3. Read the value of ‘b’
4. Calculate sum=a+b
5. Print the sum of two number
6. Stop
Selection or Conditional control flow
Selection flow allows the program to make choice between two alternate paths
based on condition. It is also called as decision structure
Basic structure:
If( condition) TRUE then
{
Statement block 1;
Else
Statement block2;
}
The conditional control flow is explained with the example of finding greatest of two numbers.
pg. 2
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Repetition control flow
Repetition control flow means that one or more steps are performed repeatedly
until some condition is reached. This logic is used for producing loops in program logic
when one one more instructions may need to be executed several times or depending
on condition.
Basic Structure:
Repeat for(i = 0;I<=5;i++)
{ Statement block;
}
The classification of control statements are given below:
Control Statements
Conditional Unconditional
- goto
Selection (or)Decision making Iteration or looping - break
- continue
-simple-if - while
-if-else - do-while
-nested if - for
-if-else-if ladder
- switch case
pg. 3
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Conditional Branching Statements
Selection Statement or Decision making Statements
In selection or conditional branching, the program control is transferred from one point to
another based on the outcome of a certain condition.
simple-if Statement
if-else Statement
nested if Statement
if-else-if ladder Statement
switch case Statement
Simple-if Statement
This is the simplest form of decision control statement. In this form, a set of statements are
executed only if the condition given with if evaluates to true.
Its general syntax and flow chart is as under:-
if(condition)
{
Statements ;
}
Example:
/* program to check whether the given number is negative*/
#include<stdio.h>
#include<conio.h>
void main()
{
int num; clrscr();
printf(“enter the number”);
scanf(“%d”,&num); if(num<0)
{
printf(“the entered number is negative”);
}
getch();
}}
pg. 4
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
if- else statement:-
This is a bi-directional control statement. This statement is used to test a condition and
take one of the two possible actions. If the condition evaluates to true then one statement (or
block of statements) is executed otherwise other statement (or block of statements) is executed.
The general form and flow chart of if-else statement is as under:-
if(condition)
{
block of statements;
}
else
{
block of statements;
}
Example:
/*Program to find the biggest of two
numbers*/ #include<stdio.h>
#include<conio
.h> void main()
{
int
num1,num2 ;
clrscr();
printf(“enter the two numbers”);
scanf(“%d
%d”,&num1,&num2);
if(num1>num2)
{
printf(“the number %d is big”,num1);
}
else
{
printf(“the number %d is big”,num2);
}
getch();
pg. 5
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Nested if-else
If we have if-else statement within either the body of an if statement or the body of else
Statement or in the body of both if and else, then this is known as nesting of if else statement.
The general form of nested if-else statement is as follows:-
if (condition1)
{
if (condition2)
{
Statements block 1;
}
else
{
Statements block 2;
}
}
else {
Statements block 3;
}
pg. 6
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Example:
/*program to find the largest of three numbers*/
#include<stdio.h>
#include<conio.h>
main()
{
int a, b,c;
printf(“enter the three numbers”);
scanf(“%d%d%d”,&a,&b,&c);
if (a >= b)
{
if (a >= c)
printf("%d is the largest number.", a);
else
printf("%d is the largest number.", c);
}
else
{
if (b >= c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.", c);
}}
Else if ladder
This is a type of nesting in which there is an if-else statement in every else part except the last
else part. This type of nesting is called else if ladder.
The general syntax and flow chart of else if ladder is as follows:-
If(condition1)
{
Statement block 1;
}
else if(condition2)
{
Statement Block 2;
}
else if(condition3)
{
Statement block 3;
}
Else
{
default statement;
}
pg. 7
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Example:
A program to illustrate the else if ladder:-
/*program to find the grade of the student*/
#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
printf(“enter the percentage of the student”);
scanf(“%d”,&marks);
if(marks>=80)
printf(“your grade is a”);
else if(marks>=70)
printf(“your grade is b”);
else if(marks>=60)
printf(“your grade is c”);
else if(marks>=50)
printf(“your grade is d”);
else
printf(“Fail”);
getch();
}
pg. 8
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Switch case statement
Switch case statements are a substitute for long if statements and has more flexibility and a
clearer format than else-if ladder.
This statement is used to select one out of the several numbers of alternatives present in a block.
This selection statement successively tests the value of an expression against a list of integer
or character constants. When a match is found, the statements associated with that constant
are executed.
The general syntax of switch case statement is as follows:-
switch(expression)
{
case value1: statement1;
break;
case value 2: statement2;
break;
case value 3: statement3;
break;
………………………
………………………
case value N: statement N;
break;
default: default statement;
}
pg. 9
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Example:
/*program to print day of the week*/
#include<stdio.h>
#include<conio.h>
void main()
{
int day; clrscr();
printf(“enter the day number from 1 to7”);
scanf(“%d”,&day);
switch(day)
{
case 1: printf(“monday”);
break;
case 2: printf(“tuesday”);
break;
case 3: printf(“wednesday”);
break;
case
4:printf(“thursday”);
break;
case 5:printf(“friday”);
break;
case 6:printf(“saturday”);
break;
case 7:
printf(“sunday”);
break;
default:printf(“wrong input”);
}
getch();
pg. 10
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Looping or Iterative statements:
Looping means that one or more steps are performed repeatedly until some condition is
reached. This logic is used for producing loops in program logic when one or more instructions
may need to be executed several times or depending on condition.
There are three types of loops in C language:-
1. while loop
2. do while loop
3. for loop
while loop (or) Entry Control loop
The simplest of the looping structure in C is the while statement. The while loop provides a mechanism
to repeat one or more statements while a particular condition is true.
It is an entry controlled loop because the control condition is placed as the first line of the code. If the control
condition evaluates to false, then the statements enclosed in the loop are never executed.
Syntax:
while (condition)
{
Statement 1;
Statement 2;
increment/decrement;
}
Example:
#include<stdio.h>
main()
{
int i = 0;
while (i < 5)
{
printf("%d\n", i);
i++;
}
Do While loop
The do-while loop is an exit controlled loop because the test condition is evaluated at the
end of the loop. Now the test condition is evaluated at the end. This clearly means that the
body of the loop gets executed at least one time
pg. 11
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Syntax:-
initialization;
do
{
Body of the loop;
increment/decrement;
} while( condition );
Example:
#include<stdio.h>
main()
{
int i = 0;
do
{
printf("%d\n", i);
i++;
} while (i < 5);
}
For loop
The for loop in C is a control flow statement used for iterative execution of a block of code. It is
particularly useful when the number of iterations is known or can be determined before the loop begins.
Syntax:-
for(initialization; condition; increment/decrement/update)
{
Statement block;
}
pg. 12
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Example:
#include<stdio.h>
main()
{
int i;
for(i=0;i<=5;i++)
{
printf("%d\n", i);
i++;
}
}
Unconditional Statements (or) Jumping Statements:
Jump statements (or) unconditional statements are used to transfer the control from one
part of the program to another part.
Type of unconditional statements
goto
break
continue
goto statement:
It is an unconditional statement and it transfers the control to the another part of the
program. The goto statement is used as under:-
Syntax:
goto label;
...................
...................
...................
Label:
Statement;
....................
....................
pg. 13
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
Example:
#include <stdio.h>
int main() {
int n = 0;
// If the number is zero, jump to
// jump_here label
if (n == 0)
goto jump_here;
// This will be skipped
printf("You entered: %d\n", n);
jump_here:
printf("Exiting the program.\n");
return 0;
}
continue statement :
The continue statement is used inside the body of the loop statement. It is used when we want
to go to the next iteration of the loop after skipping some of the statements of the loop.
The continue statement is written as under:-
Syntax: continue;
Example:
Main()
{
int i;
for (i = 0; i < 10; i++)
{
if (i == 4)
{ continue; }
printf("%d\n", i);
}
break statement:
Break statement is used inside the loops or switch statement. This statement causes an
immediate exit from the loop or the switch case block in which it appears.
It can be written as
break;
Example:
/*Program to understand the use of break
statement*/
#include<stdio.h>
main()
{
int n;
for(n=1;n<=5;n++)
{
if(n==3)
{
printf(“I understand the use of
break \n”); break;
}
pg. 14
COMPUTER PROGRAMMING C CONTRO STRUCTURE SINCET
printf(“Number = %d \n”,n);
}
printf(“Out of for loop \n”);
}
Output:
Number = 1
Number = 2
I understand the use of break Out
of for loop
pg. 15