The if statement allows a programmer to test the truth value of a conditional expression and to either select or reject the execution of certain statements depending on this value. It is a powerful decision making or branching tool.
In C language, the syntax of if statement is as follows:
if (conditional expression)
Statement;
The condition (which can be simple or compound) following the keyword if is always enclosed in parenthesis. The statement may be a single statement or any valid block of statements enclosed within {and}. If the conditional expression is true, the statement is executed, otherwise nothing is executed. Consider the following C program for Illustration.
/* magic number program */
#include <stdio.h>
main ( )
#include <stdio.h>
main ( )
{
int magic = 223;
int guess;
printf ("Enter your guess for the number \n");
scanf ("% d" &guess);
if (guess = = magic)
printf ("\n Congratulation! Right guess");
}
This program prompts the user to enter a number. It reads the number entered through keyboard and stores it in the variable guess. It then compares this value with 223. If the value .entered is equal to 233 then the message - Congratulation! Right guess - is displayed otherwise nothing is displayed.
The if statement has another form also, as given below:
if(conditional expression)
statement1;
else
statement2;
In this form statement 1 is executed if the conditional expression evaluates to true, otherwise statement 2 is executed. Consider the following C program for illustration.
/* magic number program */
#include <stdio.h>
main ( )
#include <stdio.h>
main ( )
{
int magic = 223;
int guess;
printf ("Enter your guess for the number \n");
scanf ("% d" &guess);
if (guess = = magic)
printf ("\n Congratulation ! Right guess"); else
printf ("\n Sorry ! Wrong guess");
}
Nesting ifs: Note that the statement part of if statement can be another if statement. Such structure is referred to as nested ifs. Consider the following C program for illustration that prints the largest of three values stored in variables.
Flow Chart of if else statement in Nested Form / * finding the largest of 3 numbers * / #include <stdio.h> main( ) { float a » 5, b = 2 , c = 7; 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"); } else if Ladder. There is another way of putting ifs together when multipath decisions are involved. Multipath decision is a chain of ifs in which statement associated with each else is an if. It takes the following general form: if (condition 1) Statement l; else if (condition 2) Statement 2; else if (condition 3) Statement 3; Statement x; The conditions in elseif ladder are evaluated from the top (of the ladder) downwards. As soon as the true condition is found associated statement is executed and control is transferred to statement x. #include <stdio.h> main( ) { int unit, cust; float charges; printf ("Enter Customer No. and Units Consumed: \n"); scanf ("% d % d", &cust, &unit); if (unit <= 200) charges = 0.5 *unit; else if (unit < = 400) charges = 100 + 0.65* (unit - 200); else if (unit <= 600) charges = 230 + 0.8 * (unit - 600); printf ("\n \n Customer No: % charges: %0.2f \n" Cust, Charges); } Remember that each else is associated with the nearest preceding if as is illustrated below: if (condition-1) if (condition-2) Statement-1; else Statement-2; Here if condition-1 is true then condition-2 is evaluated. If condition-2 is also true then statement-1 is executed. If condition-2 is false then statement-2 is executed. Note that if condition-1 is false nothing is executed because there is no else part associated with condition-1 even though the indentation of the program suggests that. |
No comments:
Post a Comment