In this tutorial, you will be familiarize with one type of conditional statement that is if else statement available for C programming language, along with its syntax and example of if-else .
Conditional statements :
Conditional statements are those code which allows you to control the program’s flow of the execution centred on a condition.
It means that the statements in the program are not executed in sequence.
Rather, one group of statements is executed, depending on how the condition is evaluated.
if else statement:
Syntax :
if (condition) { statement; } else { statement; } statements;
How if else statement works :
If condition written in brackets() is true, then single statement or multiple statements inside the curly braces executes, otherwise it is skipped and the statements written below else will executes.
Numerical values other than zero is considered true while zero is false.
Example of if-else statement :
1. Code to check whether the number entered by user is an even number or an odd number:
// Header file which contains functions for standard input and output manipulative operation. #include <stdio.h> int main() // Compiling of code start from main function { int x ; // Declaration of local variable // To display on console screen printf(“Enter a value…… /n ”); // To get input from user scanf(“%d”,&x); // Use of if-else statement // to check whether the number entered by user is an even or an odd number. if ( x % 2 == 0) { printf(“Entered number is an even number.”); } else { printf(“Entered number is an odd number.”); } }
Here I include both the possible aspects for this code such as entered number is an even number like 100 or it may be an odd number like 145.
Output of both the cases shown below :
case 1. if entered value is 100 then
OUTPUT:
Enter a value……100 Entered number is an even number.
But,
case 2. if entered value is 145 then
OUTPUT:
Enter a value……145 Entered number is an odd number.
1. Code to check whether the entered number by user is greater than 100 or not :
// Header file which contains functions for standard input and output manipulative operation. #include <stdio.h> int main() // Compiling of code start from main function { // Declaration of local variable int x ; // To display on console screen printf(“Enter a value…… /n ”); // To get input from user scanf(“%d”,&x); // Use of if-else statement // to check whether the number entered by user is greater than or lesser than or equal to 100. if ( x > 100 ) { printf(“Entered number is greater than 100.”); } else if ( x = 100 ) { printf(“Entered number is equal to 100.”); } else { printf(“Entered number is lesser than 100.”); } }
Here I include three possible aspects for this code such as entered number is greater than 100 like 150 or it may be lesser than 100 like 26 and the entered number may be equal to 100 also.
Output of both the cases shown below :
case 1. if entered value is 150 then
OUTPUT:
Enter a value……150 Entered number is greater than 100.
But,
case 2. if entered value is 26 then
OUTPUT:
Enter a value……26 Entered number is lesser than 100.
Also,
case 3. if entered value is 100 then
OUTPUT:
Enter a value……100 Entered number is equal to 100.
This is all about if else statement available for C programming language, along with its syntax and example of if-else .
If you have any query please comment below.