We all need to alter our actions in the face of changing circumstance. If the weather is fine, then i will go for the game . If highway is busy i would take a diversion. if she say no, i would look else where, you can notice that all these decisions depend on same condition being met.
C language too much be able to perform different sets of actions - depending on the circumstances. C has three major decision making instructions - the if statement, the if else statement, and the switch statement. A fourth , some what less important structure is the one that used conditional operator. In this lecture we will learn all these ways. ( except switch, which has a separate lecture devoted to it later) in which a C program can react to changing circumstances.
Decision! Decision!
As mentioned earlier, a decision control instruction can be implemented in C using:
a) The if statement b) The if- else statement c) The conditional operators.
The if statement:
Like most languages, C uses the keyword if to implement the decision control instruction.
The general form of if statement look like this.
if( condition)
{
do this;
}
The condition following the keyword if is always enclosed with in a pair of parentheses. If the condition what ever is true, then the statement is executed. If the condition is not true then the statement is not executed: instead the program skips past it.
But who do we express the condition it self in c? as a general rule, we express a condition using C relational operator. The relational operators allow us to compare two values to see weather they are equal to each other unequal , or weather one is greater than other.
Multiple statement with in if :
if multiple statements are to be executed then they must be placed with in a pair of braces.
For example:
if (condition)
{
statement 1;
statement 2;
statement 3;
}
Nested if:
Nested if mean that there is another if condition in if.
For example:
if ( condition)
{
if ( condition)
do this;
}
The if-else statement:
If else is used when if statement fails than the else statement will execute.
Example1: Example 3:
if( condition) if ( condition)
do this; {
else do this;
do this; do this;
}
Example 2: else
if ( condition) {
do this; do this;
else do this;
{ }
do this;
do this;
}
Note:
Had there been only one statement in the if and one statement in else then we dropped the pair of braces but if there are more than one statement then we close the if and else in braces separately.
Nasted if-else statement:
It is like nested if but we make another if -else is nested if else program.
For example:
main()
{
int i:
printf ("enter either 1 or 2");
scanf ( " %d",a);
if (i= =1)
printf( " you would go to heaven");
else
{
if( i= =2)
printf ("hell was created with you in your mind");
else
printf( " how about mother earth"):
}
}
Note:
That the second if-else construct is nested in the first else statement. If the condition in the first if statement is false, then the condition in the second if statement is checked . If it is false as well, then the final else statement is executed.