Module2
Module 2
Decision control and looping statements
C supports three types of control statements.
1. Conditional statements
2. Unconditional statements
3. Looping statements.
Conditional statements:
Conditional statements in C programming are used to make decisions based on the condition.
C language provides the following five types of conditional statements.
simple if
if else
nested if/if – else – if
cascaded if
switch
simple if :
It is a one way selection statement which executes the statements based on certain condition.
Syntax :
if(condition/expression)
{
Statement block 1;
}
Statement X;
Flowchart :
FALSE
Condition/
expression
TRUE
Statement block 1
Statement X
The if structure may include one statement or n statements enclosed within curly brackets.
First the condition/expression is evaluated. If it is true, the statement block 1 will be executed,
otherwise these block will be skipped and the execution will jump to statement X.
1 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
Example program: write a program to determine whether a person is eligible to vote or not.
#include<stdio.h>
main()
{
int age;
printf(“enter the age of a person”);
scanf(“%d”,&age);
if(age>=18)
printf(“eligible to vote”);
}
if-else statement :
It is a two way selection statement which executes either true block statements or false block
statements based on the given condition.
Syntax :
if(condition/expression)
{
Statement block 1;
}
else
{
Statement block 2;
}
Statement X;
Flowchart :
Condition/
expression
TRUE FALSE
Statement block 1 Statement block 2
Statement X
2 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
In the above syntax, expression is evaluated first. If it is true statement block 1 will be
executed, otherwise statement block 2 will be executed.
Example program : write a C program to find whether a person is eligible to vote or not.
#include<stdio.h>
main()
{
int age;
printf(“enter the age of a person”);
scanf(“%d”,&age);
if(age>=18)
printf(“eligible to vote”);
else
printf(“not eligible to vote”);
}
nested if:
If/if else statements within another if/ if else statement is called as nested if statements.
Syntax :
if(condition1)
{
if(condition 2)
{
Statement block 1;
}
else
{
Statement block 2;
else
{
Statement block 3;
}
Statement Y;
Flowchart :
3 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
FALSE TRUE
condition
1
Statement block 3 condition
TRUE 2 FALSE
Statement block 1 Statement block 2
Example program : write a C program to test whether a given number is positive or negative
or equal to zero.
#include<stdio.h>
main()
{
int num;
printf(“enter any number”);
scanf(“%d”,&num);
if(num==0)
printf(“the number is equal to zero”);
else if(num>0)
printf(“number is positive”);
else
printf(“number is negative”);
}
Cascaded if else/else if ladder/if-else-if :
It is a multiway statement.
Cascaded if statement makes it possible to evaluate several conditions in a row.
This type of statements has several if code blocks placed below each other, with optional code
at the end.
4 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
Syntax :
if(condition 1)
Statement block 1;
else if(condition 2)
Statement block 2;
else if(condition 3)
Statement block 3;
else
Statement X;
Flowchart :
TRUE
condition Statement 1
1
FALSE
TRUE
condition Statement 2
2
FALSE
TRUE
condition Statement 3
3
FALSE
Statement X
Example program : write a C program to display suitable grade of the student based on his
percentage.
Grade Percentage
FCD Per >=90
FC 60 to 90
SC 35 to 60
FAIL Per <35
5 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
#include<stdio.h>
main()
{
float percentage;
printf(“enter the percentage”);
scanf(“%f”,&percentage);
if(percentage>=90)
printf(“grade is FCD”);
elseif((percentage>=60)&&(percentage<90))
printf(“grade is FC”);
elseif((percentage>=35)&&(percentage<60))
printf(“grade is SC”);
else
printf(“grade is FAIL”);
}
Switch statement :
It is another multi way decision statement. It helps the programmer to select from many
alternatives. It is mainly used by the programmer to provide menu options so that he can
select one option from multiple options.
Syntax :
switch(expression)
{
case value 1:
Statement block1;
break;
case value 2:
Statement block2;
break;
case value 3:
Statement block3;
break;
………………………………….
case value n:
Statement blockn;
break;
case value n:
Statement block n;
break;
6 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
Switch(expression)
Statement 1;
break;
Statement 1;
Statement 2;
break;
break;
Statement 3;
break;
Statement n;
break;
Statement X;
break;
Example program : simple claculator
#include<stdio.h>
main()
{
float a,b;
char op;
printf(“enter the values of a and operator and b”);
scanf(“%f%c%f”,&a,&op,&b);
switch(op)
{
case ‘+’ : printf(“sum is %f”,a+b);
break;
case ‘-’ : printf(“difference of two numbers is %f”,a-b);
break;
case ‘*’ : printf(“multiplication of two numbers is %f”,a*b);
7 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
break;
case ‘%’ : printf(“mod is %f”,a/b);
break;
case ‘/’ : if(b==0)
printf(“division by zero error”);
else
printf(“division of twon numbers is %f”,a/b);
break;
default : printf(“invalid operator”);
break;
}
}
Rules for switch statement:
1. The switch value must be an integer type or character type.
2. Case labels must be unique.
3. Case labels must end with colon.
4. Two case labels may have the same set of actions associated with it.
5. The break and default statements are optional.
6. There can be only one default label in a switch statement.
7. C permits nested switch statements ,i.e, a switch statement within another switch statement.
8 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
Iterative statements :
Iterative statements are used to repeat the execution of a list of statements, depending on the
value of condition.
Iterative statements are also known as looping statements.
C supports three types of iterative statements.
1. While loop
2. Do-while loop
3. For loop
While loop
It is one of entry control loop statement.
It repeats the given true block statements repeatedly till the given condition is true. Whenever
the condition becomes false the loop terminates.
It is pretest loop since the condition is placed at the beginning of the loop. If the condition
evaluates to false for the first time, then the statements enclosed in loop are never executed.
Syntax :
Initialization;
while(condition)
{
Statement block;
Update counter;
}
Flowchart:
initialization
condition exit
false
Update counter
true
Statement block
9 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
Example program: C program to print from 0 to 4
#include<stdio.h>
main()
{
int i=0;
while(i<5)
{
printf(“%d”,i);
i++;
}
}
Output : 0,1,2,3,4
do-while loop :
It is exit control statement.
It repeats the given true block statements repeatedly till the given condition is true. Whenever
the condition becomes false the loop terminates.
It is post test loop, i.e. test condition is tested at the end of the loop. Here the body of the loop
gets executed at least once even if the condition is false.
Syntax :
Initialization;
do
{
Statement block;
Update counter;
}
while(condition);
Flowchart :
10 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
Statement block
Update the condition
TRUE
Condition
FALSE
Statement
exit X
Example program: C program to print from 0 to 4
#include<stdio.h>
main()
{
int i=0;
do
{
printf(“%d”,i);
i++;
}
while(i<5);
}
Output : 0,1,2,3,4
Difference between while and do-while loop:
while do-while
It is entry control statement. It is exit control statement.
It repeats the given true block statements It repeats the given true block statements
repeatedly till the given condition is true. repeatedly till the given condition is true.
Whenever the condition becomes false the loop Whenever the condition becomes false the loop
terminates. terminates.
It is pre test loop. It is post test loop.
If the condition evaluates to false for the first time, Here the body of the loop gets executed at least
then the statements enclosed in loop are never once even if the condition is false.
11 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
executed.
Syntax : Syntax :
while(condition)
do
{
{
Statement block;
Statement block;
}
}
Statement X;
while(condition);
Statement X;
Flowchart : Flowchart :
Statement block
Update the condition
Condition Update the condition
FALSE
Statement block TRUE
Condition
Statement X
FALSE
Statement X
Example program: Example program:
#include<stdio.h> #include<stdio.h>
main() main()
{ {
int i=0; int i=0;
while(i<5) do
{ {
printf(“%d”,i); printf(“%d”,i);
i++; i++;
} }
} while(i<5);
}
For loop:
It repeats the given true block statements repeatedly till the given condition is true. Whenever
the condition becomes false the loop terminates.
12 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
For loop is known as determinate or definite loop because the programmer knows exactly
how many times the loop will repeat.
For loop consists of three parts such as initialization, condition, increment/decrement counter.
Working :
The loop variable is initialized only once. With every iteration of the loop, the value of
the loop variable is updated and the condition is checked. If the condition is true,
statement block of the loop is executed, else they are skipped and execution jumps to the
immediate statement following the for loop body/
Syntax :
for(initialization;condition;update counter)
{
Statement block;
}
Statement X;
Flowchart:
for(initialization;condition;update counter)
TRUE FALSE
Statement block
Statement X
Programming example: C program to print from 0 to 4
#include<stdio.h>
main()
{
int i;
for(i=0;i<5;i++)
{
13 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
printf(“%d”,i);
}
}
Output : 0,1,2,3,4
Unconditional statements/loop control statements:
These are the statements which transfer the control or flow of execution,
unconditionally to another block of statements.
The three unconditional statements are break, goto and continue staements.
Break statement:
It stops the current and succeeding iteration. i.e it transfers the execution control out of the
loop. It breaks the entire loop execution.
Programming example:
#include<stdio.h>
main()
{
int i;
for(i=0;i<5;i++)
{
printf(“%d”,i);
if(i==2)
break;
}
}
Output: 0,1
Continue statement:
It stops the current iteration and continues the next iteration. i.e it transfers the execution
control to the next iteration of the same loop. It stops the current iteration of that loop.
Programming example:
#include<stdio.h>
main()
{
int i;
for(i=0;i<5;i++)
{
printf(“%d”,i);
14 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
if(i==2)
continue;
}
}
Output: 0,1,3,4
Goto statement:
It provides an unconditional jump from goto to the labelled statement. Here, label is an
identifier and it can be set(placed) anywhere in the program above or below the goto
statement.
Programming example:
#include<stdio.h>
main()
{
printf(“KVGCE\t”);
goto label1;
printf(“is in\t”);
label1:printf(“sullia\t”);
}
Output: KVGCE sullia
Note: If the label is above the goto, then it is called backward jump. If it is below goto, then it is
called forward jump.
Type conversion and type casting:
Type conversion:
In type conversion, a data type is automatically converted into another data type by a
compiler at the compiler time.
It is also called widening conversion.
It is also called implicit conversion.
In this type conversion, there is no need for a casting operator.
The destination data type can’t be smaller than source data type
Example:
int x=30;
float y;
y=x; // y==30.000000.
15 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
Type casting:
In type conversion, one data type is converted into another data type by a y a programmer
using casting operator.
It is also called explicit conversion.
In this type casting, casting operator is needed in order to cast a data type to another data
type.
Type casting is also called narrowing conversion because in this, the destination data type
may be smaller than the source data type.
Example:
float salary;
int sal;
sal=(int)salary;
16 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia
Module2
17 Mr. Jagadeesh M.,ECE Dept., KVGCE Sullia