If/else if statement
Name:sanvari………….
Subject:…………….
Std…
Rollno…..
Index
Sr no. title
page no.
01 what is decision making in c programmimg?
02
02 types of decision making statements
03
03 if/else if statement
04
04 syntex of if…..
05
05 flowchart of if…..
06
06 example of if…..
07
07 reference links
08
What is decision making in c programmimg?
Decision making structures require that the programmer specifies one or
more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined to
be true, and optionally, other statements to be executed if the condition
is determined to be false.
the general form of a typical decision making structure
C programming language provides the following types of decision
making statements.
Sr.No. Statement & Description
1 if statement
An if statement consists of a boolean expression followed by one or
more statements.
2 if...else statement
An if statement can be followed by an optional else statement, which
executes when the Boolean expression is false.
3 nested if statements
You can use one if or else if statement inside another if or else
if statement(s).
4 switch statement
A switch statement allows a variable to be tested for equality against a
list of values.
5 nested switch statements
You can use one switch statement inside another switch statement(s).
If/else if statement
The if/else if statement allows you to create a chain of if
statements. The if statements are evaluated in order
until one of the if expressions is true or the end of the
if/else if chain is reached. If the end of the if/else if
chain is reached without a true expression, no code
blocks are executed.
In simple words if /Else If conditional statements have more than one
condition. If the first condition is false, only then will the second condition
will be checked. If the second one is also false, then the app will default
to else or it will do nothing.
A conditional statement with one condition and two possible outcomes.
A conditional statement with two conditions and three possible outcomes.
Condition 2 is only checked if Condition 1 is false.
syntax
if (condition1)
{
// do this if condition1 is true
// condition 1 statements
// then exit if/else if
}
else if (condition2)
{
// do this if condition2 is true
// condition 2 statements
// then exit if/else if
}
else if (condition3)
{
// do this if condition3 is true
// condition3 statements
// then exit if/else if
}
// continuation point after if/else if is complete
Flowchart
Example of if/else if statement
#include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 !=var2)
{
printf("var1 is not equal to var2\n");
}
else if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else if (var2 > var1)
{
printf("var2 is greater than var1\n");
}
else
{
printf("var1 is equal to var2\n");
}
return 0;
}
Output:
Input the value of var1:12
Input the value of var2:21
var1 is not equal to var2
Reference links
Pgno3: www.tutorialspoint.com
Pg4: www.tutorialspoint.com
Pg5: eecs.oregonstate.edu
technovationchallenge.org
Pg6: eecs.oregonstate.edu
Pgno7: eecs.oregonstate.edu
Pgno8: beginnersbook.com