Sie sind auf Seite 1von 16

2 Decision Making

PIC-17212

Chapter 2: Decision making


2.1 Decision making and branching if statement (if, if-else, else-if ladder, nested if-else) Switch case
statement, break statement.
(14M)
2.2 Decision making and looping while, do, do-while statements for loop, continue statement (14M)

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

Decision making statements:


if statement:
The syntax/general form of if statement is as follows:
if(condition)
statement;
If condition is true then the statement followed by if will be executed, otherwise, it will be
skipped.
if(condition)
{
statement-1;
statement-2;

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:

Computer Department, Jamia Polytechnic (0366)

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;
}

Computer Department, Jamia Polytechnic (0366)

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.);
}

Program #1: To find larger of two integer numbers:


#include<stdio.h>
#include<conio.h>
int main( )
{
int a,b;
clrscr( );
printf(Enter value of a and b: );
scanf(%d%d,&a,&b);
if(a>b)
Computer Department, Jamia Polytechnic (0366)

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;
}

Program #2: To find even/odd:


#include<stdio.h>
#include<conio.h>
int main( )
{
int number;
clrscr( );
printf(Enter a number: );
scanf(%d,&number);
if(number%2 == 0)
{
printf(Number is even.);
}
else
{
printf(Number is odd.);
}
getch();
return 0;
}
else if ladder:
When there are multiple conditions to check, else-if ladder can be used. In else-if statement, if the
previous condition is false, the statements after else are executed. But in else , there is another
if statement. In this if statement, one more condition is checked. Depending upon the result of
this condition statements of either if block or else block will be executed.
The general form of else if is shown below:
if(condition-1)
{
statements-1;
}
Computer Department, Jamia Polytechnic (0366)

2 Decision Making

PIC-17212

else if(condition-2)
{
statements-2;
}
else if(condition-3)
{
statements-3;
}

Program #3: To find grade of student if average marks (integer) is entered:


#include<stdio.h>
#include<conio.h>
int main( )
{
int average;
clrscr( );
printf(Enter a your average marks: );
scanf(%d,&average);
if(average >= 75)
{
printf(Your grade is: First class with distinction.);
}
else if(average >= 60 && average < 75)
Computer Department, Jamia Polytechnic (0366)

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.

Program #4: To find largest of 3 distinct integer numbers:


#include<stdio.h>
Computer Department, Jamia Polytechnic (0366)

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:

Computer Department, Jamia Polytechnic (0366)

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;
}

Body of while loop

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:

Program #6: To find sum of series 1+2+ + n :


#include<stdio.h>
#include<conio.h>
int main( )
{
int n, i, sum;
clrscr( );
printf(Enter value of n: );
Computer Department, Jamia Polytechnic (0366)

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);

Body of do-while loop

Computer Department, Jamia Polytechnic (0366)

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;

Body of for loop

}
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:

Computer Department, Jamia Polytechnic (0366)

12

2 Decision Making

PIC-17212

Using the Comma:


More than one statement in the initialization and update expression portions of for loop can be used.
For example,
for(a=1, b=4; a<b; a++, b--)
{
//
}
Infinite for loop:
Here is one more for loop variation. You can intentionally create an infinite loop (a loop that never
terminates) if you leave all three parts of the for empty. For example:
for( ; ; )
{
// ...
}
This loop will run forever, because there is no condition under which it will terminate.
Program #8: To find sum of series 1+2+ + n using for loop :
#include<stdio.h>
#include<conio.h>
int main( )
{
int n, sum;
Computer Department, Jamia Polytechnic (0366)

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

Program #10: To check whether entered number is prime or not prime:


#include<stdio.h>
#include<conio.h>
int main( )
{
int num;
int flag = 1;
clrscr( );
printf(Enter value of num: );
scanf(%d,&num);
for(int i=2; i <= num/2; i++)
{
if((num % i) == 0)
{
flag = 0;
break;
}
}
if(flag==1)
printf("%d is prime.",num);
else
printf("%d is not prime.",num);
getch( );
return 0;
}

Program #11: To print Fibonacci series:


(Note: In Fibonacci series, first two numbers are 0 and 1. Next number in the series is sum of
previous two numbers. Therefore the series is: 0 1 1 2 3 5 8 13 21 34 )
#include<stdio.h>
#include<conio.h>
int main()
{
int f1=0,f2=0,f3=1,num,count;
clrscr();
printf("\nEnter how many numbers from series you want to print: ");
scanf("%d",&num);
if(num==1)
{
printf("0");
getch();
return 0;
}
if(num==2)
Computer Department, Jamia Polytechnic (0366)

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

Computer Department, Jamia Polytechnic (0366)

16

Das könnte Ihnen auch gefallen