THE DECISION CONTROL STRUCTURE
C language must be able to perform different set of actions depending on the circumstances.Here comes the importance of decision control structures.Decision making is about deciding the order of the execution of statements based on certain conditions.This decision control instruction can be implemented in C using :
The 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
statementif else
statementNested if
statementif else
ladder statement
Simple if statement
The general syntax of simple if statement is
if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ }
If the Boolean expression evaluates to true, then the block of code inside the if statement will be executed. If the Boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing curly brace) will be executed.
Example
#include <stdio.h>
int main ()
{
int a = 10;
if( a < 20 )
{
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
If-else statement
In if-else statement an if statement is followed by an else statement, which executes when the Boolean expression is false.
The general form of if-else statement is
if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ }
Example
#include <stdio.h> int main () { int a = 100; if( a < 20 ) { printf("a is less than 20\n" ); } else { printf("a is not less than 20\n" ); } printf("value of a is : %d\n", a); return 0; }
Nested if statement
Here it is possible to use one if or else if statement inside another if or else if statements.
The general syntax of nested if is
if( boolean_expression 1) { /* Executes when the boolean expression 1 is true */ if(boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } }
Example
#include <stdio.h> int main () { int a = 100; int b = 200; if( a == 100 ) { if( b == 200 ) { printf("Value of a is 100 and b is 200\n" ); } } printf("Exact value of a is : %d\n", a ); printf("Exact value of b is : %d\n", b ); return 0;}
If-else ladder statement
The if-else ladder statement in C programming language is used to test set of conditions in sequence. An if condition is tested only when all previous if conditions in if-else ladder is false. If any of the conditional expression evaluates to true, then it will execute the corresponding code block and exits whole if-else ladder.The syntax of if-else ladder statement is
if(boolean_expression 1) { /* Executes when the boolean expression 1 is true */ } else if( boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } else if( boolean_expression 3) { /* Executes when the boolean expression 3 is true */ } else { /* executes when the none of the above condition is true */ }
Example
#include <stdio.h> int main () { int a = 100; if( a == 10 ) { printf("Value of a is 10\n" ); } else if( a == 20 ) { printf("Value of a is 20\n" ); } else if( a == 30 ) { printf("Value of a is 30\n" ); } else { printf("None of the values is matching\n" ); } printf("Exact value of a is: %d\n", a ); return 0; }
Comments
Post a Comment