C program of decision making by using if & if-else statement
ADVERTISEMENTS
C program of decision making, There are you will learn how to make control decisions in the C program.
By using the if statement:
The syntax of the if statement is:
if (test expression)
{
// statements to be executed if the test expression is true
}
{
// statements to be executed if the test expression is true
}
Let us understand this example through the C program:
// C program of decision making by using if statement
#include <stdio.h>
int main() {
int n;
printf("Enter an integer value::\n");
scanf("%d", &n);
// true if number is less than 6
if (n < 6) {
printf("Condition is true.\n");
}
return 0;
}
Output:
Enter an integer value::
5
Condition is true.
By using the if...else statement:
The syntax of the if...else statement is:
if (test expression) {
// statements to be executed if the test expression is true
} else {
// statements to be executed if the test expression is false
}
// statements to be executed if the test expression is true
} else {
// statements to be executed if the test expression is false
}
Example-1: In this example, you will understand how true a condition occurs.
// C program of decision making by using if-else statement
#include <stdio.h>
int main() {
int n;
printf("Enter an integer value::\n");
scanf("%d", &n);
// true if number is less than 6 otherwise it will be false
if (n < 6) {
printf("Condition is true.\n");
} else {
printf("Condition is false.\n");
}
return 0;
}
Output:
Enter an integer value::
5
Condition is true.
Example-2: In this example, you will understand how false a condition occurs.
// C program of decision making by using if-else statement
#include <stdio.h>
int main() {
int n;
printf("Enter an integer value::\n");
scanf("%d", &n);
// true if number is less than 6 otherwise it will be false
if (n < 6) {
printf("Condition is true.\n");
} else {
printf("Condition is false.\n");
}
return 0;
}
Output:
Enter an integer value::
7
Condition is false.
By using the if...else ladder:
The syntax of the if...else ladder is:
if (test expression1) {
// statement(s)
} else if(test expression2) {
// statement(s)
} else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
// statement(s)
} else if(test expression2) {
// statement(s)
} else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
Let us understand this example through the C program:
// C program of decision making by using if-else ladder
#include <stdio.h>
int main() {
int n;
printf("Enter an integer value::\n");
scanf("%d", &n);
if (n == 0) {
printf("Fisrt condition is true.\n");
} else if (n == 1) {
printf("Second condition is true.\n");
} else if (n == 2) {
printf("Third condition is true.\n");
} else {
printf("Condition failed!\n");
}
return 0;
}
Output:
Enter an integer value::
1
Second condition is true.
By using the nested if...else statement:
The syntax of the nested if...else statement is:
if (test expression 1) {
else if(test expression 2) {
// statement(s)
} else if (test expression 3) {
// statement(s)
}
.
.
else {
// statement(s)
}
} else if (test expression n) {
// statement(s)
}
.
.
else {
// statement(s)
}
else if(test expression 2) {
// statement(s)
} else if (test expression 3) {
// statement(s)
}
.
.
else {
// statement(s)
}
} else if (test expression n) {
// statement(s)
}
.
.
else {
// statement(s)
}
Example-1: In this example, you will understand the first input is less than the second input.
// C program of decision making by using the nested if-else statement
#include <stdio.h>
int main() {
int n, m;
printf("Enter the two integer values::\n");
scanf("%d %d", &n, &m);
if (n >= m) {
if (n == m) {
printf("Result: %d = %d\n", n, m);
} else {
printf("Result: %d > %d\n", n, m);
}
} else {
printf("Result: %d < %d\n", n, m);
}
return 0;
}
Output:
Enter the two integer values::
5
7
Result: 5 < 7
Example-2: In this example, you will understand the first input is greater than the second input.
// C program of decision making by using the nested if-else statement
#include <stdio.h>
int main() {
int n, m;
printf("Enter the two integer values::\n");
scanf("%d %d", &n, &m);
if (n >= m) {
if (n == m) {
printf("Result: %d = %d\n", n, m);
} else {
printf("Result: %d > %d\n", n, m);
}
} else {
printf("Result: %d < %d\n", n, m);
}
return 0;
}
Output:
Enter the two integer values::
7
5
Result: 7 > 5