23CSE201
PROCEDURAL PROGRAMMING USING C
DECISION MAKING IN C
CONDITIONAL STATEMENTS
• C Conditional Statements Allow you to make a Decision, based on the result
of a condition. These statements are called Decision Making statements or
Conditional statements or Branching Statements.
• In Decision control statements, group of statements are executed when
condition is true and if condition is false, then else part statements will get
executed. (Another group of statements are executed)
CONDITIONAL STATEMENTS
• C language has such decision-making capabilities within its program by the
use of following the decision-making statements:
1) If Statement
2) If-else Statement
3) Ladder it-else / Else- if / Multiple if – else Statement
4) Nested if-else Statement
Switch case
IF - STATEMENT
• If statement in C is used to control the program flow
based on some condition. It’s used to execute some
statement, if the condition is true otherwise it will get
skipped.
• This is the simplest way to modify the control flow of
the program.
IF – STATEMENT - Example
#include<stdio.h>
int main()
{
int a=10;
int b=10;
if(a==b)
{
printf(“a and b both are equal”);
}
return 0;
Output:
}
a and b both are equal
IF –ELSE STATEMENT
• If else statement in C is used to execute some
statement block if the condition is true,
otherwise the else block will be executed.
IF –ELSE STATEMENT - Example
#include<stdio.h>
int main()
{
int a=10;
int b=5;
if(a>b)
{
printf(“a is greater number”);
}
else
{
printf(“b is greater number”); Output:
}
return 0;
a is greater number
}
MULTIPLE IF-ELSE (LADDER IF-ELSE)
• Multiple if-else /ladder if-else statements in C is like another of if condition,
it’s used in a program when if statement having multiple Decisions.
• If-else-if ladder in C programming is used to test a series of conditions
sequentially.
• Condition is tested only when all previous if conditions in the if-else ladder
are false. If any of the conditional expressions evaluate to be true, the,
appropriate code block will be executed and if all the conditions are false
than the else block will get executed.
MULTIPLE IF-ELSE (LADDER IF-ELSE)
MULTIPLE IF-ELSE (LADDER IF-ELSE)- Example
#include<stdio.h>
int main() {
int a,b,c; else
a=10; {
b=20; printf(“c is greater”);
c=5; }
if( a>b && a>c ) return 0;
{ }
printf (“a is greater”);
} Output:
else if( b>c && b>a) b is greater
{
printf (“b is greater”);
}
NESTED IF ELSE STATEMENT
• Nested if-else statements play an important role
in C programming; it means you can use
conditional statements inside another
conditional statement.
• It is always legal in C programming to nest if-
else statements, which means you can use one if
or else-if statement inside another if or else if
statement(s).
NESTED IF ELSE STATEMENT
• In this, first if condition is evaluated. If the
if condition is true then the block of if
statements will get executed. Inside if there
is another if condition, is checked and will
print output accordingly. If first if condition
is not true, else of the first if will get
executed.
NESTED IF ELSE STATEMENT - Example
#include<stdio.h> else
int main() {
{ printf (“ \n no2 is greater than no1 ”);
int no1, no2; }
printf (“Enter value of no1 :”); }
scanf(“%d”, & no1); else
printf (“Enter value of no2 :”); {
scanf(“%d”, & no1); printf (“ no1 is equal to no2 ”);
if (no1 != no2) }
{ return 0;
printf (“Both Numbers are not equal”); } Output:
if (no1 > no2) Enter value of no1: 21
{ Enter value of no2: 12
printf (“ \n no1 is greater than no2 ”); Both Numbers are Not Equal
} No1 is greater than no2
SWITCH STATEMENT
• The switch statement in C is an alternate to if-else–if ladder statement which
allows us to execute multiple operations for different possibilities.
• Switch statement is a control statement that allows us to choose only one
choice among the many given choices.
• A switch statement tests the value of a variable and compares it with multiple
cases. once the case match is found a block of statements associated with that
particular case is executed.
• Each case in a block of a switch has a different name/number which is
referred to as an identifier.
• The value provided by the user is compared with all the cases inside the
switch block until the match is found.
SWITCH STATEMENT
SWITCH STATEMENT - Example
#include<stdio.h> case 1: ans= no1 + no2;
int main () printf ( “ %d + %d = %d ” , no1, no2, ans);
{ break;
int no1,no2, ans, op; case 2: ans= no1 - no2;
clrscr(); printf ( “ %d - %d = %d ” , no1, no2, ans);
printf ( “ Enter Values of No1 and No2 :” ); break;
scanf( “ %d %d ”, &no1 , &no2 ); case 3: ans= no1 * no2;
printf ( “ 1.Addition \n” ); printf ( “ %d * %d = %d ” , no1, no2, ans);
printf ( “ 2.Subtracation \n” ); break;
printf ( “ 3.Multiplication \n” ); case 4: ans= no1 / no2;
printf ( “ 4.Division \n” ); printf ( “ %d / %d = %d ” , no1, no2, ans);
printf ( “ Enter Number For Operation : ” ); break;
scanf ( “ %d ” , &op ); default : printf ( “ Invalid Operation” );
switch (op) }
{ return 0;
}
SWITCH STATEMENT - Output
Enter Values of No1 and No2 :
10
2
1.Addition
2.Subtracation
3.Multiplication
4.Division
Enter Number for Operation: 3
10 * 2 = 20
UNCONDITIONAL STATEMENTS
• Unconditional statements allow you to direct the program's flow to another
part of your program without evaluating conditions. Most unconditional
statements require a programmer defined keyword.
• Unconditional branching is when the programmer forces the execution of a
program to jump to another part of the program.
• Example: break, goto , continue ….
BREAK STATEMENT
• Break statements is used when we need to exit from the loop.
• Break statement provides us the facility to exit the loop in the for ,
while , do-while and switch looping statements.
• Syntax: break;
BREAK STATEMENT- Example
#include<stdio.h>
void main()
{ Output:
int a= 10;
while (a<20) value of a : 10
{ value of a : 11
printf("\n value of a : %d",a); value of a : 12
a++; value of a : 13
if (a>15) value of a : 14
{ value of a : 15
break;
}
}
}
GOTO STATEMENT
• The goto statement in C programming is used to transfer the control unconditionally from
one part of program to other part.
• In other words goto statements transfer control to labelled statements.
• Syntax:
GOTO STATEMENT- Example
#include <stdio.h>
void main() Output:
{ 1
int n=1; 2
repeat: 3
printf ("%d\n", n); 4
n++; 5
if (n<=10) 6
{ 7
goto repeat ; 8
} 9
} 10
CONTINUE STATEMENT
• The continue statement in C program works
somewhat like the break statement, Instead of
forcing termination, it forces the next iteration of the
loop to take place, skipping any code in between.
• Mostly continue statement is used inside loops.
• When continue statement is encountered inside a
loop , control jumps to the beginning of the loop for
next iteration.
• Syntax: continue;
CONTINUE STATEMENT- Example
#include<stdio.h>
void main() {
int j =0;
do Output:
{ 0
if ( j==7 )
1
{
j++; 2
continue; 3
} 4
printf( “ %d\n” , j); 5
j++; 6
}
8
while ( j<10);
} 9
THANK YOU