The programs we have seen so far are executed sequentially. What if you need to execute a set of statements on conditional basis ? Conditional Statements are used to execute a set of statements based on the conditions you provide.
The following are the conditional statements:
The following are the conditional statements:
- Simple if
- if ... else
- else-if ladder
- Nested if
- switch
- 1. Simple if
- This will help you check a condition and based on the returned truth value it will execute a set of statements i.e if the condition(usually a conditional or logical expression) returns 1(true), the block of statements will be executed
Syntax :
The following example will help you get a grasp of it :if(condition) { /* Statements to be executed */ }
You can also combine multiple conditions using logical operators.#include<stdio.h> void main() { int amt = 500; if(amt>200) /* Returns true as 500 > 200 */ { printf("\nYou have %d Rupees"); /* This will be printed */ } amt = 100; /* Returns false as 100 < 200 */ if(amt>200) { printf("\nYou have %d Rupees"); /* This will not be printed */ } }
Output : You have 500 Rupees
- 2. if .... else
- The Simple if statement executes a set of statement when a condition is evaluated as true and ignored(not executed) when it is evaluated to false. But what if we need to execute another set of statements when the given condition fails. The if .... else will accomplish this.
The if part will be executed when the condition is evaluated as true otherwise the else part will be executed.
Syntax :
Lets see this with an example :if(condition) { /* Statements to be executed when the given condition is true */ } else { /* Statements to be executed when the given condition fails */ }
Here, in this example, in the first if - else block (commented), the if part will be executed since amt>500 is evaluated as true. But in the second if - else block, the else part will be executed since amt>500 is evaluated as false.#include<stdio.h> void main() { int amt = 600; /* Beginning of first if - else block */ if(amt > 500) { printf("\n You have Rs.%d",amt); /* This will be printed */ } else { printf("\n You have less than or equal to Rs.500"); } /* End of first if - else block */ amt = 400; /* Beginning of second if - else block */ if(amt > 500) { printf("\n You have Rs.%d",amt); } else { printf("\n You have less than or equal to Rs.500"); /* This will be printed */ } /* End of second if - else block */ }
- 3. else - if ladder
- What will you do if you want to check another condition if the condition fails before going to the else part? Now, the else - if ladder comes handy.
Syntax :
And its time to see this with an example :if(condition_1) { /* Statements to be executed when condition 1 is true */ } else if(condition_2) { /* Statements to be executed when condition 2 is true */ } . . . else if(condition_n) { /* Statements to be executed when condition n is true */ } else { /* Statements to be executed when none of the conditions listed hold */ }
#include<stdio.h> void main() { int speed; printf("\n Enter the speed of the vehicle \t"); scanf("%d",&speed); if(speed>60) /* Speed greater than 60 */ { printf("\n Slow down a bit "); } else if(speed>50) /* Speed : 51 - 60 */ { printf("\n Good speed... Maintain this "); } else if(speed>40) /* Speed : 41 - 50 */ { printf("\n Fuel Economic Speed... Great "); } else /* Speed less than or equal to 40 */ { printf("\n You can speed up a bit "); } }
Sample Output 1: Enter the speed of the vehicle 65 Slow down a bit Sample Output 2: Enter the speed of the vehicle 57 Good speed... Maintain this Sample Output 3: Enter the speed of the vehicle 46 Fuel Economic Speed... Great Sample Output 4: Enter the speed of the vehicle 38 You can speed up a bit
- 4. Nested if
- The nested - if is an 'if' block within another if block. The syntax is as below :
Lets take this up with an example. The previous sample program modified to use nested if:Syntax : if(condition1) { if(condition2) { /* Statements to be executed when condition 2 holds */ /* This is similar to using if((condition1)&&(condition2)) */ } else { /* Statements to be executed when condition 2 fails */ /* This is similar to using if((condition1)&&(!condition2)) */ } /* Statements to be executed when condition 1 is true */ }
#include<stdio.h> void main() { int speed; printf("\n Enter the speed of the motor vehicle \t"); scanf("%d",&speed); if(speed<=60) /* Speed is less than or equal to 60 */ { if(speed<=50) /* Speed : less than or equal to 50 */ { if(speed<=40) /* Speed : less than or equal to 40 */ { printf("\n You can speed up a bit "); } else /* Speed : 41 to 50 */ { printf("\n Fuel Economic Speed... Great"); } } else /* Speed : 51 to 60 */ { printf("\n Good Speed... Maintain This "); } } else /* Speed is greater than 60 */ { printf("\n Slow down a bit "); } }
Sample Output 1: Enter the speed of the vehicle 65 Slow down a bit Sample Output 2: Enter the speed of the vehicle 57 Good speed... Maintain This Sample Output 3: Enter the speed of the vehicle 46 Fuel Economic Speed... Great Sample Output 4: Enter the speed of the vehicle 38 You can speed up a bit
- 5. switch
- In switch statements you will evaluate the value of a single expression and just pass the control to the case statements specified within the switch block. If the value of the expression matches the value specified right next to the 'case' keyword, then the statements for that case will be executed.
switch(expression) { case constant_1: /* When the value of expression is equal to the constant 1 */ statements; jump statements; case constant_2: /* When the value of expression is equal to the constant 2 */ statements; jump statements; . . . case constant_n: /* When the value of expression is equal to the constant n */ default: /* When the value of expression does not match any of those in the listed cases */ statements; jump statements;
#include<stdio.h> void main() { char choice; abc: printf("\nWould you like to have a cup of tea ?(y/n) \t"); scanf("\n%c",&choice); switch(choice) { case 'y': /* If the input is 'y' (yes) */ printf("\nJust wait for a minute. Here, I'll come with it "); break; case 'n': /* If the input is 'n' (no) */ printf("\nAll right then. "); break; default: /* The character entered is neither 'y' nor 'n' */ printf("\nThe choice is invalid. Please enter a valid choice "); goto abc; } }
Sample Output 1: Would you like to have a cup of tea ?(y/n) g The choice is invalid. Please enter a valid choice Would you like to have a cup of tea ?(y/n) y Just wait for a minute. Here, I'll come with it
Sample Output 2: Would you like to have a cup of tea ?(y/n) h The choice is invalid. Please enter a valid choice Would you like to have a cup of tea ?(y/n) n All right then.
No comments:
Post a Comment