Decision Making
Decision Making
Statements
Introduction
• Usually, the compiler executes C programs line by line in the order
that we write in our program.
• Apart from this, we might get into a situation where we have to
execute some statements repeatedly.
• For this, you can use the C Programming loops.
• But there are some situations where we have to run the statements
based on the condition.
• For that, we can use the conditional/decision making statements.
• Some common condition statements are; if, if- else, else if, nested if,
….
If Statement
• If statement in C programming is one of the most useful decision-
making statements in real-time programming.
• C If statement allows the compiler to test the condition first, and then,
depending upon the result, it will execute the statements.
• If the test condition is true, then only statements within the if
statement will be performed/executed by the C compiler.
If Statement syntax
/ /If statement Syntax in C
if (test condition)
{
Statement 1;
Statement 2;
Statement 3;
………….
………….
Statement n;
}
• From the above code, If the test condition in the If statement is true,
then the statements (Statement 1, Statement 2, Statement 3, …….,
Statement n) will be executed.
• Otherwise, all these statements will skip.
Example
/* If Statement in C Programming Example */
#include <stdio.h>
int main()
{
int number;
printf("Enter any integer Value\n");
scanf("%d",&number);
if( number >= 1 )
{
printf("You Have Entered Positive Integer\n");
}
return 0;
}
If Statement Flow Chart
• If the test condition is true, STATEMENT 1 executed followed by
STATEMENT N.
• If the condition is False, STATEMENT N execute because it is out of
the if statement block.
Example
• /* If Statement in C Programming Example */
#include <stdio.h>
int main(){
int number;
printf("Enter any integer Value\n");
scanf("%d",&number);
if( number >= 1 ){
printf("You Have Entered Positive Integer\n");}
printf("This Message is not coming from IF STATEMENT\n");
return 0;
}
IF ELSE Statement in C
• The If Else statement in C Programming is an extension to the If
statement.
• We already saw the If statement, and it will only execute the
statements when the given condition is true.
• And if the condition is false, it will not execute statements.
• In the real-world, it would be nice to execute something when the
condition fails.
• To do so, If else statement used. Here, Else statement will execute the
statements when the condition fails.
If Else statement Syntax
if (Test condition)
{
//If the condition is TRUE then these statements will be executed
True statements;
}
else
{
//If the condition is FALSE then these statements will be executed
False statements;
}
• If the test condition present in the above structure is true, the True
statements will execute.
• When the condition is false, False statements will execute.
The flow chart of the If Else Statement
Example in If Else Statement
• This program, is going to use 4 different printf statements.
• If the condition is true, it will print 2 separate statements.