2. Introduction
A program is execution of one or more instructions
sequentially in the order in which they come in to sight.
This process is similar to reading the text that appears in
notebook.
Quite often it is desirable in a program to alter the sequence
of the statements depending upon certain circumstances.
Decision making statements in a programming language help
the programmer to transfer the control from one part to other
parts of the program.
3. Introduction (cont..)
Decision making statements helps the programmer in
determining the flow of control.
This involves a decision making conditions to see whether the
particular condition is satisfied or not.
4. Decision making statements in C
if statement
if –else statement
if-else-if ladder statement
nested if-else statement
switch-case statement
goto statement
5. if statement
C uses the keyword if to execute a set of command lines or one command line
when logical condition is true.
void main()
{
if(expression)
{
statement1;
statement2;
}
statement3;
}
6. if statement (continue..)
If statement contains an expression which is evaluated. If the expression is
true it returns 1 otherwise 0.
In case the condition is true the compiler execute the lines within if block.
Else compiler skips the lines within if block.
The conditional statement should not be terminated with semi-colon.
The multiple statements which are to be executed if condition is true are
enclosed within curly bracket.
The curly bracket indicates the scope of if statement.
8. Write a program to check whether entered number is less than 10. if
yes print the same.
void main()
{
int n;
clrscr();
printf(“enter a number”);
scanf(“%d”, &n);
if(n<10)
printf(“entered number is less than 10);
getch();
}
10. Write a program to check whether entered number is less 10 and
greater than 0. if yes print the same.
void main()
{
int n;
clrscr();
printf(“enter a number”);
scanf(“%d”, &n);
if(n<10 && n>0)
printf(“entered number is less than 10 and greater than 0);
getch();
}
11. Output?
main( )
{
int a = 300, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "n%d %d", b, c ) ;
}
main( )
{
int a = 300, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "n%d %d", b, c ) ;
}
12. if-else statement
if block statement execute only when the condition in if is true.
When condition is flase, program control executes the next statement
which appear after if statement.
The if-else statement takes care of the true and false condition.
It has two blocks. One block is for if and it is executed when the condition
is true.
The other block is of else and it is executed when the condition is false.
The else statement can not be used without if.
No multiple else statements are allowed with one if
13. Is condition
true?
if block execution
else block execution
Next statement
Stop
NO
Yes
From previous statement
If-else
Statement
16. Write a program to add three numbers and after addition check if sum is between of 100
and 200 or not.
void main()
{
int a,b,c,d;
clrscr();
printf(“enter three numbers a,b,c”);
scanf(“%d,%d,%d”,&a, &b, &c);
d=a+b+c;
if(d>100 && d<200)
printf(“n sum is %d which is in between 100 and 200”);
else
printf(“n sum is %d which is out of range”);
getch();
}
17. Output?
main( )
{
int x = 3, y = 5 ;
if ( x == 3 )
printf ( "%d", x ) ;
else
printf ( "%d", y ) ;
}
18. Output?
main( )
{
int i = 65 ;
char j = ‘A’ ;
if ( i == j )
printf ( “C is WOW” ) ;
else
printf( "C is a headache" ) ;
}
19. Find out error
void main( )
{
float a = 12.25, b = 12.52 ;
if ( a = b )
printf ( "na and b are equal" ) ;
}
25. Example
Void main()
{
Int i, age;
Printf (“enter 1 for male and 2 for female”);
Scanf(“%d”, &i);
printf(“Enter your age”);
scanf(“%d”, &age);
If(i==1)
{
if(age>20)
printf(“Eligible for marriage”);
else
printf(“Not eligible for marriage”);
26. If-else-if Ladder statement
If the first logical condition is true the compiler executes block followed by
first if condition, otherwise it skips that block and checks for next logical
condition followed by else-if and if it is true the block statement followed
by that else-if will be executed.
27. Is 1st
condition
true?
Is 2nd
condition
true?
Is 3rd
condition
true?
Is 4th
condition
true?
STATEMENT 5
STATEMENT 3
STATEMENT 2
STATEMENT 1 STATEMENT 4
FALSE
FALSE
FALSE
FALSE
TRUE
TRUE
TRUE
TRUE
TO NEXT STATEMENT
29. goto statement- Unconditional
branching
This statement does not require any condition for jump.
This statement passes control anywhere in program.
Control is passed to another part of program without testing any
condition.
goto statement can be defined as follows:
goto label;
where label name must start with character.
The label is the position where the control is to be transferred.
30. goto statement (continue..)
void main()
{
int x;
clrscr();
printf(“enter a number”);
scanf(“%d”,&x);
if(x%2==0)
goto even;
else
goto odd;
31. even: printf(“%d is even”,x);
getch();
return;
odd: printf(“%d is odd”,x);
getch();
}
34. Switch-case-default
The control statement that allows us to make a decision from the number
of choices is called a switch,
or more correctly a switch-case-default, since these three keywords go
together to make up the control statement.
35. switch ( integer expression )
{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}
36. The integer expression following the keyword switch is any C expression
that will yield an integer value.
It could be an integer constant like 1, 2 or 3, or an expression that
evaluates to an integer.
The keyword case is followed by an integer or a character constant.
Each constant in each case must be different from all the others.
37. main( )
{
int i = 2 ;
switch ( i )
{
case 1 :
printf ( "I am in case 1 n" ) ;
case 2 :
printf ( "I am in case 2 n" ) ;
case 3 :
printf ( "I am in case 3 n" ) ;
default :
printf ( "I am in default n" ) ;
}
}
38. If you want that only case 2 should get executed, it is upto you to get out
of the switch then and there by using a break statement.
39. main( )
{
int i = 2 ;
switch ( i )
{
case 1 :
printf ( "I am in case 1 n" ) ;
break ;
case 2 :
printf ( "I am in case 2 n" ) ;
break ;
case 3 :
printf ( "I am in case 3 n" ) ;
break ;
default :
printf ( "I am in default n" ) ;
}
}