Decision making with if statement
The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are,
- Simple if statement
- If....else statement
- Nested if....else statement
- else if statement
Simple if statement
The general form of a simple if statement is,
if( expression ) { statement inside; } statement outside;
If the expression is true, then 'statement-inside' it will be executed, otherwise 'statement-inside' is skipped and only 'statement-outside' is executed.
Example :
#include <stdio.h> void main( ) { int x,y; x=15; y=13; if (x > y ) { printf("x is greater than y"); } }
Output
x is greater than y
if...else statement
The general form of a simple if...else statement is,
if( expression ) { statement block1; } else { statement block2; }
If the 'expression' is true, the 'statement-block1' is executed, else 'statement-block1' is skipped and 'statement-block2' is executed.
Example :
#include <stdio.h> void main( ) { int x,y; x=15; y=18; if (x > y ) { printf("x is greater than y"); } else { printf("y is greater than x"); } }
Output
y is greater than x
Nested if....else statement
The general form of a nested if...else statement is,
if( expression ) { if( expression1 ) { statement block1; } else { statement block2; } } else { statement block3; }
if 'expression' is false the 'statement-block3' will be executed, otherwise it continues to perform the test for 'expression 1' . If the 'expression 1' is true the 'statement-block1' is executed otherwise 'statement-block2' is executed.
Example :
#include <stdio.h> #include <conio.h> void main( ) { int a,b,c; clrscr(); printf("enter 3 number"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if( a > c) { printf("a is greatest"); } else { printf("c is greatest"); } } else { if( b> c) { printf("b is greatest"); } else { printf("c is greatest"); } } getch(); }
else-if ladder
The general form of else-if ladder is,
if(expression1) { statement block1; } else if(expression2) { statement block2; } else if(expression3 ) { statement block3; } else default statement;
The expression is tested from the top(of the ladder) downwards. As soon as the true condition is found, the statement associated with it is executed.
Example :
#include <stdio.h> #include <conio.h> void main( ) { int a; printf("enter a number"); scanf("%d",&a); if( a%5==0 && a%8==0) { printf("divisible by both 5 and 8"); } else if( a%8==0 ) { printf("divisible by 8"); } else if(a%5==0) { printf("divisible by 5"); } else { printf("divisible by none"); } getch(); }
Points to Remember
- In if statement, a single statement can be included without enclosing it into curly braces
{ }
int a = 5; if(a > 4) printf("success");
No curly braces are required in the above case, but if we have more than one statement inside if condition, then we must enclose them inside curly braces. ==
must be used for comparison in the expression of if condition, if you use=
the expression will always return true, because it performs assignment not comparison.- Other than 0(zero), all other values are considered as true.
if(27) printf("hello");
In above example, hello will be printed.
No comments:
Post a Comment