C If, If... Else and Nested If..
C If, If... Else and Nested If..
else Statement
TUTORIAL EXAMPLES
C if...else Statement
In programming, decision making is used to specify the order in which statements are
executed. In this tutorial, you will learn to create decision making program using if...else
statements.
C if statement
if (testExpression)
{
// statements
}
If the test expression is evaluated to true (nonzero), statements inside the body of if is executed.
If the test expression is evaluated to false (0), statements inside the body of if is skipped from
execution.
To learn more on when test expression is evaluated to nonzero (true) and 0 (false), check out
relational and logical operators.
Flowchart of if statement
https://www.programiz.com/c-programming/c-if-else-statement/1?utm_expid=66600334-5.iwbEljyDSDadlwkqEY3rxA.1&utm_referrer=https%3A%… 1/7
12/13/2017 C if, if...else and Nested if...else Statement
TUTORIAL EXAMPLES
#include <stdio.h>
int main()
{
int number;
return 0;
}
Output 1
Enter an integer: -2
You entered -2.
The if statement is easy.
https://www.programiz.com/c-programming/c-if-else-statement/1?utm_expid=66600334-5.iwbEljyDSDadlwkqEY3rxA.1&utm_referrer=https%3A%… 2/7
12/13/2017 C if, if...else and Nested if...else Statement
When user enters -2, the test expression (number < 0) becomes true. Hence, You entered -2 is
TUTORIAL EXAMPLES
displayed on the screen.
Output 2
Enter an integer: 5
The if statement in C programming is easy.
When user enters 5, the test expression (number < 0) becomes false and the statement inside the
body of if is skipped.
C if...else statement
The if...else statement executes some code if the test expression is true (nonzero) and some
other code if the test expression is false (0).
Syntax of if...else
if (testExpression) {
// codes inside the body of if
}
else {
// codes inside the body of else
}
If test expression is true, codes inside the body of if statement is executed and, codes inside the
body of else statement is skipped.
If test expression is false, codes inside the body of else statement is executed and, codes inside
the body of if statement is skipped.
https://www.programiz.com/c-programming/c-if-else-statement/1?utm_expid=66600334-5.iwbEljyDSDadlwkqEY3rxA.1&utm_referrer=https%3A%… 3/7
12/13/2017 C if, if...else and Nested if...else Statement
TUTORIAL EXAMPLES
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
// True if remainder is 0
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
Output
Enter an integer: 7
7 is an odd integer.
When user enters 7, the test expression ( number%2 == 0 ) is evaluated to false. Hence, the
statement inside the body of else statement printf("%d is an odd integer"); is executed and
the statement inside the body of if is skipped.
The if...else statement executes two di erent codes depending upon whether the test
TUTORIAL EXAMPLES
expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.
The nested if...else statement allows you to check for multiple test expressions and execute
di erent codes for more than two conditions.
if (testExpression1)
{
// statements to be executed if testExpression1 is true
}
else if(testExpression2)
{
// statements to be executed if testExpression1 is false and testExpression2 is true
}
else if (testExpression 3)
{
// statements to be executed if testExpression1 and testExpression2 is false and tes
}
.
.
else
{
// statements to be executed if all test expressions are false
}
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
TUTORIALreturn
}
0;
EXAMPLES
Output
You can also use switch statement to make decision between multiple possibilites.
Subscribe
ABOUT
CONTACT
ADVERTISE
https://www.programiz.com/c-programming/c-if-else-statement/1?utm_expid=66600334-5.iwbEljyDSDadlwkqEY3rxA.1&utm_referrer=https%3A%… 6/7