Module2 POP
Module2 POP
Module 2
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 Mrs.Bhavya P S.,CSE 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 X
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 Mrs.Bhavya P S.,CSE Dept., KVGCE Sullia
Module2
FALSE TRUE
condition
1
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.
This type of statements has several if code blocks placed below each other, with optional code
at the end.
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
#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;
Switch(expression)
Statement 1;
break;
Statement 1;
Statement 2;
break;
break;
Statement 3;
break;
Statement n;
break;
Statement X;
break;
#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 Mrs.Bhavya P S.,CSE 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;
}
}
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
Initialization;
do
{
Statement block;
Update counter;
}
while(condition);
Flowchart :
Statement block
TRUE
Condition
FALSE
Statement
exit X
executed.
Syntax : Syntax :
while(condition)
do
{
{
Statement block;
Statement block;
}
}
Statement X;
while(condition);
Statement X;
Flowchart : Flowchart :
Statement block
FALSE
Statement block TRUE
Condition
Statement X
FALSE
Statement X
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 Mrs.Bhavya P S.,CSE 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
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 Mrs.Bhavya P S.,CSE 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.
float y;
y=x; // y==30.000000.
15 Mrs.Bhavya P S.,CSE 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;