Beruflich Dokumente
Kultur Dokumente
PIC-17212
To change the flow of execution of the programs, certain control statements are used in C language.
Control statements are of two types:
1. Decision making statements:
if statement
if-else statement
switch-case statement
2. Loop constructs
for loop
while loop
do-while loop
statement-n;
}
If condition is true then the statements enclosed in { } will be executed. If condition is false then
these statement will not be executed. The flow chart of if statement is shown below:
2 Decision Making
PIC-17212
Example:
if(count<=10)
{
sum=sum+count;
count++;
}
if - else statement:
The syntax/general form of if - else statement is as follows:
if(condition)
statement;
else
statement;
If condition is true then the statement followed by if will be executed. If condition is false then the
statement followed by else will be executed.
if(condition)
{
statement-1;
statement-2;
statement-n;
}
else
{
statement-1;
statement-2;
statement-n;
}
2 Decision Making
PIC-17212
If condition is true then the statements followed by if will be executed. If condition is false then
the statements followed by else will be executed. The flow chart of if else is shown below:
Test
Expression
False
True
Body of if
Body of else
Example:
if(number%2==0)
{
printf(Number is even.);
}
else
{
printf(Number is even.);
}
2 Decision Making
PIC-17212
{
printf(Value of a=%d is greater.,a);
}
else
{
printf(Value of b=%d is greater.,b);
}
getch();
return 0;
}
2 Decision Making
PIC-17212
else if(condition-2)
{
statements-2;
}
else if(condition-3)
{
statements-3;
}
2 Decision Making
PIC-17212
{
printf(Your grade is: First class.);
}
else if(average >= 50 && average < 60)
{
printf(Your grade is: Second class.);
}
else if(average >= 40 && average < 50)
{
printf(Your grade is: Pass class.);
}
else
{
printf(Your grade is: Failed.);
}
getch();
return 0;
}
Nested if-else:
When if statement is used within another if statement, it is called nested if statement. The
general form of nested if-else is shown below:
if (condition-1)
{
if(codition-2)
{
statements-1;
}
else
{
statements-2;
}
}
else
{
statements-3;
}
nested if
In the above general form, if condition-1 is satisfied, second if statement will be executed. Here
condition-2 will be checked and depending upon the result of condition-2, execution will proceed.
2 Decision Making
PIC-17212
#include<conio.h>
int main( )
{
int a, b, c;
clrscr( );
printf(Enter three numbers a, b, c \n);
printf(Enter value of a: );
scanf(%d,&a);
printf(Enter value of b: );
scanf(%d,&b);
printf(Enter value of c: );
scanf(%d,&c);
if(a > b)
{
if(a > c)
{
printf(a=%d is largest.,a);
}
else
{
printf(c=%d is largest.,c);
}
}
else
{
if(b > c)
{
printf(b=%d is largest.,b);
}
else
{
printf(c=%d is largest.,c);
}
}
getch();
return 0;
}
switch statement:
In if statement, depending upon the result of condition, certain block of code is executed or
skipped. Codition can be either true or false. It means that there are only two options i.e. true and
false.If number of options are more, then switch statement can be used. The general form of
switch statement is as follows:
2 Decision Making
PIC-17212
switch(expression)
{
case value-1:
statements;
break;
case value-2:
statements;
break;
.
default:
statements;
}
In the above general form, the expression must solve to integer or character value, or, directly integer
or character variable can be used. the result of expression is matched with value-1, if it is true, then
the statements followed by value-1 will be executed. break statement in each case is used to take
the program execution out of switch block. if value-1 does not match with expression result, second
value i.e. value-2 is tried and so on. If no value matches expression result, then default block is
executed. The flowchart of switch block is shown below:
Program #5: To perform arithmetic operation on two numbers depending on users choice:
#include<stdio.h>
#include<conio.h>
int main( )
{
int a, b, c, choice;
float d;
clrscr( );
Computer Department, Jamia Polytechnic (0366)
2 Decision Making
PIC-17212
printf(Enter value of a: );
scanf(%d,&a);
printf(Enter value of b: );
scanf(%d,&b);
printf(========================================\n);
printf(1. Addition\n );
printf(2. Subtraction\n );
printf(3. Multiplication\n );
printf(4. Division\n );
printf(5. Exit\n );
printf(========================================\n);
printf(Enter your choice: );
scanf(%d,&choice);
switch(choice)
{
case 1:
c=a+b;
printf(a+b=%d,c);
break;
case 2:
c=a-b;
printf(a-b=%d,c);
break;
case 3:
c=a*b;
printf(a*b=%d,c);
break;
case 4:
if(b==0)
{
printf(Division is not possible.);
}
else
{
d=(float)a/b;
printf(a/b=%f,d);
}
break;
case 5:
return 0;
default:
printf(Invalid choice!);
}
getch();
return 0;
}
Computer Department, Jamia Polytechnic (0366)
2 Decision Making
PIC-17212
Loops:
When some block of code is to be executed repeatedly, loop can be used. In C language, there are
three loops:
1. while loop
2. do-while loop
3. for loop
while loop:
The general form of while loop is as follows:
while(condition)
{
statement-1;
statement-2;
::::::
statement-n;
}
In the above general form, if condition is true then the body of while loop is executed. After
executing body of while loop, the condition will be checked, if true, then again body of while will be
executed. This process is repeated as long as condition is true. when condition becomes false, the
loop is terminated. Flow chart of while loop is shown below:
10
2 Decision Making
PIC-17212
scanf(%d,&n);
sum=0;
i=1;
while(i<=n)
{
sum=sum+i;
i++;
}
printf(Value of sum=%d ,sum);
getch();
return 0;
}
Program #7: To find factorial of n :
#include<stdio.h>
#include<conio.h>
int main( )
{
int n, i, factorial;
clrscr( );
printf(Enter value of n: );
scanf(%d,&n);
factorial=1;
i=1;
while(i<=n)
{
factorial = factorial *i;
i++;
}
printf(Factorial=%d ,factorial);
getch();
return 0;
}
do-while loop:
The general form of do-while loop is as follows:
do
{
statement-1;
statement-2;
::::::
statement-n;
} while(condition);
11
2 Decision Making
PIC-17212
In the above general form, the body of loop is executed first and then codition is checked. If
condition is true, then body of loop is executed again. If condition is false, then the loop is
terminated. Flow chart of do-while loop is shown below:
The differencr between while loop and do-while loop is that in do-while loop, the body of loop is
executed at least once even if initially codition is false.
for loop:
The general form of for loop is as follows:
for(initialization; codition; update expression)
{
statement-1;
statement-2;
::::::
statement-n;
}
The execution of for loop is as follows:
1. When loop starts, initialization part is executed first.
2. After this, condition is checked. If condition is false, loop will be terminated. If condition is
true, body of for loop will be executed.
3. After executing body of for loop, update expression is executed. Update expression is
generally an increment or decrement expression, but this is not necessary.
4. Once again condition will be checked to be true or false. If condition is false, loop will be
terminated otherwise same procedure will be followed as described in step 2.
Flow chart of for loop is shown below:
12
2 Decision Making
PIC-17212
13
2 Decision Making
PIC-17212
clrscr( );
printf(Enter value of n: );
scanf(%d,&n);
sum=0;
for(int i=1; i<=n; i++)
{
sum=sum+i;
}
printf(Value of sum=%d ,sum);
getch();
return 0;
}
Program #9: To find factorial of n using for loop:
#include<stdio.h>
#include<conio.h>
int main( )
{
int n, factorial;
clrscr( );
printf(Enter value of n: );
scanf(%d,&n);
factorial=1;
for(int i=1; i<=n; i++)
{
factorial = factorial *i;
}
printf(Factorial=%d ,factorial);
getch();
return 0;
}
break statement:
break statement is used in switch-case structure and in loops. When break statement is executed in
switch-case, the execution is transferred to the statement following the switch block. When break
statement is encountered in any loop, the loop gets terminated and execution resumes at the next
statement followed by loop. In loops, it is used with some condition.
continue statement:
continue statement transfers the control to the beginning of loop. In while and do-while loops, if
continue statement is executed, control is transferred to conditional expression that controls the loop.
In for loop, if continue statement is executed, the control is transferred to update expression and then
condition of for loop is checked.
Computer Department, Jamia Polytechnic (0366)
14
2 Decision Making
PIC-17212
15
2 Decision Making
PIC-17212
{
printf("0 1");
getch();
return 0;
}
if(num>2)
{
printf("0 1 ");
count=2;
while(count<num)
{
f1=f2;
f2=f3;
f3=f1+f2;
printf("%d ",f3);
count++;
}
}
getch();
return 0;
}
End of Chapter 2
16