Unit IV-Control and Iterative Structure
Unit IV-Control and Iterative Structure
Introduction:
Control statements enable us to specify the flow of program control; i.e., the
order in which the instructions in a program must be executed. They make it
possible to make decisions, to perform tasks repeatedly or to jump from one
section of code to another.
There are four types of control statements in C:
[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure
If statement:
The single if statement in C language is used to
execute the code if a condition is true. It is also
called one-way selection statement.
Syntax
if (condition)
Statements;
If the expression is evaluated to nonzero
(true) then if block statement(s) are executed.
If the expression is evaluated to zero (false)
then Control passes to the next statement
following it.
If-Else Statement:
The if-else statement in C language is used to execute the code if condition is true
or false. It is also called two-way selection
statement.
Syntax
if (expression)
{
True block of statements
}
else
{
False block of statements
}
[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure
The nested if...else statement is used when a program requires more than one test
expression. It is also called a multi-way selection statement. When a series of the
decision are involved in a statement, we use if else statement in nested form.
Syntax
if( expression )
{
if( expression1 )
{
statement-block1;
}
else
{
statement-block 2;
}
}
else
{
statement-block 3;
}
If..else If ladder
The if-else-if statement is used to execute one code from multiple conditions. It is
also called multipath decision statement. It is a chain of if..else statements in which
each if statement is associated with else if statement and last would be an else
statement.
Syntax
if(condition1)
{
//statements
}
[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure
else if(condition2)
{
//statements
}
else if(condition3)
{
//statements
}
else
{
//statements
}
Example:
#include<stdio.h>
void main( )
{
int a;
printf("enter a number");
scanf("%d",&a);
if( a%5==0 && a%8==0)
{
printf("divisible by both 5 and 8");
}
else if( a%8==0 )
{
printf("divisible by 8");
}
else if(a%5==0)
{
printf("divisible by 5");
}
else
{
printf("divisible by none");
Prof. Farzana Azeem Sheikh Page 4
[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure
}
}
[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure
[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure
Example:
int a = 1;
switch (a) {
case 1:
printf("a is 1");
break;
case 2:
printf ("a is 2");
break;
default:
printf("a is neither 1 nor 2");
break;
}
Example:
#include <stdio.h>
main()
{
int a;
printf("Enter Your choice");
scanf("%d",&a);
switch (a) {
case 1 ...6:
printf("%d in range 1 to 6\n", a);
break;
case 15 ... 20:
printf("%d in range 15 to 20\n", a);
Prof. Farzana Azeem Sheikh Page 7
[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure
break;
default:
printf("%d not in range\n", a);
break;
}
}
Practice Programs on Switch Statement
1. Write a program to implement basic calculator operations.
2. Write a program to read a number from 0-9 and display it in word.
3. Write a program to accept a character and check if it vowel or not.
4. Write a Menu Driven program to display the price of the selected item based
on no. of items. (Price per item is given in bracket)
B=BURGER (100Rs.)
F=FRENCH FRY (50Rs.)
P=PIZZA (300Rs.)
S=SANDWICHES (150 Rs.)
5. Write a menu driven program to read time in hrs and display Greeting
Message accordingly.
Example: hrs < 12
greet = 'Good Morning';
hrs >= 12 && hrs <17
greet = 'Good Afternoon';
hrs >= 17 && hrs <= 24
greet = 'Good Evening';
[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure
#include <stdio.h>
void main()
{
int a=2;
switch(a)
{
printf("Message\n");
default:
printf("Default\n");
case 2:
printf("Case-2\n");
case 3:
printf("Case-3\n");
}
printf("Exit from switch\n");
}
Case-2
Case-3
Exit from switch
Program 2:
#include <stdio.h>
int main()
[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure
{
int x;
float y=7.0;
switch(x=y+1)
{
case 8: printf("It's Eight."); break;
default: printf("Oops No choice here!!!");
}
}
It's Eight
Program 3
#include<stdio.h>
void main()
{
int movie = 1;
switch (movie << (2 + movie))
{
default:
printf(" Traffic");
case 4:
printf(" Sultan");
case 5:
printf(" Dangal");
case 8:
printf(" Bahubali");
}
}
Bahubali
[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure
Program 4
#include<stdio.h>
#define L 10
void main()
{
auto a = 10;
switch (a, a*2)
{
case L:
printf("ABC");
break;
case L*2:
printf("XYZ");
break;
case L*3:
printf("PQR");
break;
default:
printf("MNO");
case L*4:
[email protected]
B.C.A. (Science) Department, AISC
Unit IV- Control and Iterative Structure
printf("www");
break;
}
}
XYZ
Iteration statements:
[email protected]
B.C.A. (Science) Department, AISC