PROGRAMMING IN C AND DATA STRUCTURES
MODULE II: BRANCHING & LOOPING
STATEMENT
• A statement is also called instruction.
• As the name indicates, instruction is used to instruct computer to perform a particular task like
→ adding two numbers
→ reading data from keyboard or
• For ex:
sum=a+b;
scanf("%d ", &n);
• In C, the semicolon(;) is a statement terminator.
Compound Statement
• The sequence of statement enclosed within a pair of braces { and } is called a compound statement.
• For ex:
{
a=l*b;
printf("area =%d", a);
}
CONTROL STRUCTURES
• A program is nothing but the execution of sequence of one or more instructions.
• Quite often, it is desirable to change the order of execution of statements based on certain conditions
or
• This involves a kind of decision making to see whether a particular condition has occurred or not and
direct the computer to execute certain statements accordingly.
• Based on application, it is necessary / essential
i) To alter the flow of a program
ii) Test the logical conditions
iii) Control the flow of execution as per the selection these conditions can be placed in the
program using decision-making statements.
C SUPPORTS MAINLY THREE TYPES OF CONTROL STATEMENTS
1) Decision making statements
i) if statement
ii) if else statement
iii) nested if statement
iv) else if ladder
v) switch statement
2) Loop control statements
i) while loop
ii) for loop
iii) do-while loop
3) Unconditional control statements
i) break statement
ii) continue statement
20
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
BASIC CONCEPT OF DECISION STATEMENTS
• Decision making is critical to computer programming.
• There will be many situations when you will be given 2 or more options and you will have to select an
option based on the given conditions.
• For ex, we want to print a remark about a student based on secured marks and following is the
situation:
1. Assume given marks are x for a student
2. If given marks are more than 95 then
3. Student is brilliant
4. If given marks are less than 30 then
5. Student is poor
6. If given marks are less than 95 and more than 30 then
7. Student is average
• Now, question is how to write programming code to handle such situation. C language provides
conditional i.e., decision making statements which work based on the following flow diagram:
• There are 5 types of decision statements:
i) if statement
ii) if else statement
iii) nested if statement
iv) else if ladder
v) switch statement
21
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
THE if STATEMENT
• This is basically a “one-way” decision statement.
• This is used when we have only one alternative.
• The syntax is shown below:
if(expression)
{
statement1;
}
• Firstly, the expression is evaluated to true or false.
If the expression is evaluated to true, then statement1 is executed.
If the expression is evaluated to false, then statement1 is skipped.
• The flow diagram is shown below:
• Example: Program to illustrate the use of if statement.
#include<stdio.h>
void main()
{
int n;
printf(“Enter any non zero integer: \n”) ;
scanf(“%d”, &n)
if(n>0)
printf(“Number is positive number ”);
if(n<0)
printf(“Number is negative number ”);
}
Output:
Enter any non zero integer:
7
Number is positive number
22
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
THE if else STATEMENT
• This is basically a “two-way” decision statement.
• This is used when we must choose between two alternatives.
• The syntax is shown below:
if(expression)
{
statement1;
}
else
{
statement2;
}
• Firstly, the expression is evaluated to true or false.
If the expression is evaluated to true, then statement1 is executed.
If the expression is evaluated to false, then statement2 is executed.
• The flow diagram is shown below:
• Example: Program to illustrate the use of if else statement.
#include<stdio.h>
void main()
{
int n;
printf(“Enter any non-zero integer: \n”) ;
scanf(“%d”, &n)
if(n>0)
printf(“Number is positive number”);
else
printf(“Number is negative number”);
}
Output:
Enter any non-zero integer:
7
Number is positive number
23
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
THE nested if STATEMENT
• An if-else statement within another if-else statement is called nested if statement.
• This is used when an action has to be performed based on many decisions. Hence, it is called as
multi-way decision statement.
• The syntax is shown below:
if(expr1)
{
if(expr2)
statement1
else
statement2
}
else
{
if(expr3)
statement3
else
statement4
}
• Here, firstly expr1 is evaluated to true or false.
If the expr1 is evaluated to true, then expr2 is evaluated to true or false.
If the expr2 is evaluated to true, then statement1 is executed.
If the expr2 is evaluated to false, then statement2 is executed.
If the expr1 is evaluated to false, then expr3 is evaluated to true or false.
If the expr3 is evaluated to true, then statement3 is executed.
If the expr3 is evaluated to false, then statement4 is executed.
• The flow diagram is shown below:
24
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
• Example: Program to select and print the largest of the 3 numbers using nested “if-else” statements.
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter Three Values: \n”);
scanf(“%d %d %d ”, &a, &b, &c);
printf(“Largest Value is: ”) ;
if(a>b)
{
if(a>c)
printf(“ %d ”, a);
else
printf(“ %d ”, c);
}
else
{
if(b>c)
printf(“ %d”, b);
else
printf(“ %d”, c);
}
}
Output:
Enter Three Values:
786
Largest Value is: 8
25
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
THE else if LADDER STATEMENT
• This is basically a “multi-way” decision statement.
• This is used when we must choose among many alternatives.
• The syntax is shown below:
if(expression1)
statement1;
else if(expression2)
statement2;
else if(expression3)
statement3
else if(expression4)
statement4
else
default statement5
• The expressions are evaluated in order (i.e. top to bottom).
• If an expression is evaluated to true, then
→ statement associated with the expression is executed &
→ control comes out of the entire else if ladder
• For ex, if exprression1 is evaluated to true, then statement1 is executed.
If all the expressions are evaluated to false, the last statement4(default case) is executed.
Example: Program to illustrate the use of else if ladder statement.
void main ( )
{
int n;
printf(“Enter any integer:”) ;
scanf(“%d”, &n)
if(n>0)
printf ("Number is Positive");
else if(n< 0)
printf ("Number is Negative");
else if(n== 0)
printf ("Number is Zero");
else
printf ("Invalid input");
}
Output:
Enter any integer: 7
Number is Positive
26
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
THE switch STATEMENT
• This is basically a “multi-way” decision statement.
• This is used when we must choose among many alternatives.
• The syntax & flow diagram is shown below:
• Here, choice can be either any integer value or a character.
• Based on this integer value, the control is transferred to a particular case-value where necessary
statements are executed.
• During executing, if break statement is encountered, then the control comes out of the switch block.
• If the value of the choice does not match with any of the case values (i.e. value1, value2, value3)
then control goes to default label.
• All case-values must be different.
• Example: Program to illustrate the use of switch statement.
void main()
{
char grade; // local variable definition
printf(“enter grade A to D: \n”);
scanf(“%c”,&grade);
switch(grade)
{
case 'A': printf("Excellent! \n ");
break;
case 'B': printf("Well done \n ");
break;
case 'C': printf("You passed \n ");
break;
case 'D': printf("Better try again\n ");
break;
default: printf("Invalid grade \n ");
return;
}
printf("Your grade is %c", grade);
return 0;
}
Output:
enter grade A to D:
B
Well done
Your grade is B
27
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
BASIC CONCEPT OF LOOP
• Let's consider a situation when you want to write a message “Welcome to C language” five times.
Here is a simple C program to do the same:
#include<stdio.h>
void main()
{
printf( " Welcome to C language \n");
printf( " Welcome to C language \n");
printf( " Welcome to C language \n");
printf( " Welcome to C language \n");
printf( " Welcome to C language \n");
}
Output:
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language
• When the program is executed, it produces the above result.
• It was simple, but again let's consider another situation when you want to write the same message
thousand times, what you will do in such situation?
• Are we going to write printf() statement thousand times? No, not at all.
• C language provides a concept called loop, which helps in executing one or more statements up to
desired number of times.
• Loops are used to execute one or more statements repeatedly.
• The flow diagram is shown below:
• There are 3 types of loops in C programming:
1) while loop
2) for loop
3) do while loop
28
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
THE while LOOP
• A while loop statement can be used to execute a set of statements repeatedly as long as a given
condition is true.
• The syntax is shown below:
while(expression)
{
statement1;
statement2;
}
• Firstly, the expression is evaluated to true or false.
• If the expression is evaluated to false, the control comes out of the loop without executing the body
of the loop.
• If the expression is evaluated to true, the body of the loop (i.e. statement1) is executed.
• After executing the body of the loop, control goes back to the beginning of the while statement and
expression is again evaluated to true or false. This cycle continues until expression becomes false.
• The flow diagram is shown below:
Example: Program to display a message 5 times using while statement.
#include<stdio.h>
void main()
{
int i=1;
while(i<=5)
{
printf(“Welcome to C language \n”);
i=i+1;
}
}
Output:
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language
29
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
THE for LOOP
• A for loop statement can be used to execute s set of statements repeatedly as long as a given
condition is true.
• The syntax is shown below:
for(expr1;expr2;expr3)
{
statement1;
}
• Here, expr1 contains initialization statement
expr2 contains limit test expression
expr3 contains updating expression
• Firstly, expr1 is evaluated. It is executed only once.
• Then, expr2 is evaluated to true or false.
• If expr2 is evaluated to false, the control comes out of the loop without executing the body of the
loop.
• If expr2 is evaluated to true, the body of the loop (i.e. statement1) is executed.
• After executing the body of the loop, expr3 is evaluated.
• Then expr2 is again evaluated to true or false. This cycle continues until expression becomes false.
• The flow diagram is shown below:
Example: Program to display a message 5 times using for statement.
#include<stdio.h>
void main()
{
int i;
for(i=1;i<=5;i++)
{
printf(“Welcome to C language \n”);
}
}
Output:
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language
30
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
THE do while STATEMENT
• When we do not know exactly how many times a set of statements have to be repeated, do-while
statement can be used.
• The syntax is shown below:
do
{
statement1;
}while(expression);
• Firstly, the body of the loop is executed. i.e. the body of the loop is executed at least once.
• Then, the expression is evaluated to true or false.
• If the expression is evaluated to true, the body of the loop (i.e. statement1) is executed
• After executing the body of the loop, the expression is again evaluated to true or false. This cycle
continues until expression becomes false.
• The flow diagram is shown below:
Example: Program to display a message 5 times using do while statement.
#include<stdio.h>
void main()
{
int i=1;
do
{
printf(“Welcome to C language \n”);
i=i+1;
}while(i<=5);
}
Output:
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language
31
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
THE break STATEMENT
• The break statement is jump statement which can be used in switch statement and loops.
• The break statement works as shown below:
1) If break is executed in a switch block, the control comes out of the switch block and the
statement following the switch block will be executed.
2) If break is executed in a loop, the control comes out of the loop and the statement following
the loop will be executed.
• The syntax is shown below:
Example: Write the program given in switch statement.
32
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
THE continue STATEMENT
• During execution of a loop, it may be necessary to skip a part of the loop based on some condition.
In such cases, we use continue statement.
• The continue statement is used only in the loop to terminate the current iteration.
• The syntax is shown below:
Example: Program to read and add only positive numbers using continue statement.
#include<stdio.h>
void main()
{
int i=1, num, sum =0;
for(i=0; i < 5; i ++)
{
printf(“ Enter an integer:”);
scanf( “%d”, &num);
if(num < 0)
{
printf(“you have entered a negative number \n”);
continue ; // skip the remaining part of loop
}
sum=sum+num;
}
printf(“The sum of the Positive Integers Entered = %d”, sum);
}
Output:
Enter an integer: 10
Enter an integer:-15
You have entered a negative number
Enter an integer: 15
Enter an integer: -100
You have entered a negative number
Enter an integer: 30
The sum of the positive integers entered = 55
33
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]
PROGRAMMING IN C AND DATA STRUCTURES
THE goto AND labels
• goto statement can be used to branch unconditionally from one point to another in the program.
• The goto requires a label in order to identify the place where the branch is to be made.
• A label is any valid variable name and must be followed by a colon( : ).
• The label is placed immediately before the statement where the control is to be transferred.
• The syntax is shown below:
• Example: Program to detect the entered number as to whether it is even or odd. Use goto statement.
#include<stdio.h>
void main()
{
int x;
printf(“Enter a Number: \n”);
scanf(“%d”, &x);
if(x % 2 = = 0)
goto even;
else
goto odd;
even: printf(“%d is Even Number”);
return;
odd: printf(“ %d is Odd Number”);
}
Output:
Enter a Number:
5
5 is Odd Number.
34
For Solved Question Papers of UGC-NET/GATE/SET/PGCET in Computer Science, visit [Link]