if-else statement
Many times we need to execute some statements only once when some condition gets true or false.
To complete this requirement we have several methods like –
syntax
1 2 3 4 5 6 7 8 9 10 11 |
if(condition) { statement1; statement2; statement3; . . . . statementn; } |
As shown in syntax, statement1….statementn will execute only when condition inside if gets true.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> int main() { unsigned int var1; unsigned int var2; printf("Program to say hello if var1 is greater"); printf("\nenter var1 = "); scanf("%d",&var1); printf("\nenter var2 = "); scanf("%d",&var2); if(var1>var2) { printf("hello"); /*condition control statemnet*/ } return 0; } |
1 2 3 4 |
Program to say hello if var1 is greater enter var1 = 45 enter var2 = 25 hello |
In above program, we can see that execution of printing hello statement depends on the condition.
This method is used where we need to execute some statements when condition inside if gets true and some need to execute when condition gets false.
statements which need to execute when condition gets true will put in the if block and which need to execute on false behaviur will put in the else block.
Syntax
1 2 3 4 5 6 7 8 9 10 11 12 |
if(condition) { statement1; statement2; statement3; } else { statement4; statement5; statement6; } |
if block will execute when condition gets true otherwise else block statement will execute.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include<stdio.h> int main() { unsigned int var1; unsigned int var2; printf("Program to find a greater number between two integers"); printf("\nenter var1 = "); scanf("%d",&var1); printf("\nenter var2 = "); scanf("%d",&var2); if(var1>var2) { printf("\nvar1 is greater"); /*will execute when condition gets true*/ } else { printf("\nvar2 is greater"); /*will execute when condition gets false*/ } return 0; } |
1 2 3 4 |
Program to find greater number between two integer enter var1 = 643878 enter var2 = 363878 var2 is greater |
This method is used where we need to execute blocks of statements when respective conditions inside if or elseif gets true.
syntax
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
if(condition1) { statement1; statement2; } else if(condition2) { statement3; statement4; } else if(condition3) { statement5; statement6; } else { statement7; statement8; } |
Example
Program To find the greatest of three integers(var1,var2,var3).[See the use of Logical operator]
Possible Conditions:
1 2 3 4 5 6 7 |
1. (var1>var2)and(var1>var3) ----> var1 is greatest 2. (var2>var1)and(var2>var3) ----> var2 is greatest 3. (var3>var1)and(var3>var1) ----> var2 is greatest 4. None of all above ----> all are equal |
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include<stdio.h> int main() { unsigned int var1; unsigned int var2; unsigned int var3; printf("Program to find the greatest Number between three integers by use of logical operator"); printf("\nEnter var1 = "); scanf("%d",&var1); printf("\nEnter var2 = "); scanf("%d",&var2); printf("\nEnter var3 = "); scanf("%d",&var3); if((var1>var2)&&(var1>var3)) { printf("\nvar1 is greatest"); } else if((var2>var1)&&(var2>var3)) { printf("\nvar2 is greates"); } else if((var3>var1)&&(var3>var2)) { printf("\nvar3 is greatest"); } else { printf("\nAll are Equal"); } return 0; } |
1 2 3 4 5 6 |
Output: Program to find greatest Number between two integer by use of logical operator enter var1 = 643878 enter var2 = 363878 enter var3 = 435367 var1 is greater |
Nested if means use of if statement inside the master if-else statement.let’s solve the above example through if nested if statement use-
Example
Program To find the greatest of three integers(var1,var2,var3).[Nested If statement]
Pseudo Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
if(var1>var2) { if(var1>var3) /*nested if- else*/ { var1 is greatest; } else { var3 is greatest; } } else { if(var2>var3) /*nested if- else*/ { var2 is greatest; } else { var3 is greatest; } } |
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include<stdio.h> int main() { unsigned int var1; unsigned int var2; unsigned int var3; printf("Program to find greatest number between three integer through Nested if Method"); printf("\nEnter var1 = "); scanf("%d",&var1); printf("\nEnter var2 = "); scanf("%d",&var2); printf("\nEnter var3 = "); scanf("%d",&var3); if(var1>var2) { if(var1>var3) /*nested if- else*/ { printf("\nvar1 is greatest"); } else { printf("\nvar3 is greatest"); } } else { if(var2>var3) /*nested if- else*/ { printf("\nvar2 is greatest"); } else { printf("\nvar3 is greatest"); } } return 0; } |