C langugae : Selection Statements

A selection statement can direct the flow of program execution along different paths depending on a given condition. There are two selection statements in C: if and switch.
If Statements
The general form of the if statement is
if (expression) statement;
else statement;
If expression evaluates to true (anything other than 0), the statement or block that forms the target of if is executed; otherwise, the statement or block that is the target of else will be executed, if it exists.Remember, only the code associated with if or the code associated with else executes, never both.
Exp: -
// To Find the maximum value between two variable using if statement
#include
#include
void main()
{
int r,c;
printf ("enter first number");
scanf("%d",&r);
printf ("enter first number");
scanf("%d",&c);
if(r>c)
printf("r is max = %d ",r);
else
printf("c is max = %d ",c);
getch();
}
The if conditions are evaluated one after another. As soon as one of these expression yields TRue, the corresponding statement is executed. Because the rest of the else if chain is cascaded under the corresponding else clause, it is alternative to the statement executed, and hence skipped over. If none of the if conditions is true, then the last if statement's else clause is executed, if present.
The if-else-if Ladder
A common programming construct is the if-else-if ladder, sometimes called the if-else-if staircase because of its appearance. Its general form is: -
if (expression) statement;
else
if (expression) statement;
else
if (expression) statement;
else statement;
\\ To evaluate then marks of students as per given subjects English, Maths,Science
#include< stdio.h >
#include< conio.h >
#include< string.h >
void main( )
{
char st[20],div[20];
int e,s,m,tot=0,per;
printf(" enter the name of the student");
scanf("%s",st);
printf(" enter the English marks");
scanf("%d",&e);
printf(" enter the Science marks");
scanf("%d",&s);
printf(" enter the Maths marks");
scanf("%d",&m);
clrscr();
if(((e<100 || s<100)&&(m< 100 || e>0))&&(s>0 || m>0))
exit(1);
tot=e+s+m;
per=(e+s+m)/3;
if(per<=100 && per>=75)
strcpy(div,"Distinction");
else if(per<75 && per>=60)
strcpy(div,"First Division");
else if(per<60 && per>=48)
strcpy(div,"Second Division");
else if(per<48 && per>=36)
strcpy(div,"Third Division");
else if(per<36 && per >0)
strcpy(div,"Fail");
else
strcpy(div,"INVALID VALUES");
printf(" Name of Student = %s\n",st);
printf(" Marks of English = %d\n",e);
printf(" Marks of Science= %d\n", s);
printf(" Marks of Maths = %d\n", m);
printf(" Total Marsk of Students = %d\n", tot);
printf(" Percentahr of students = %d%\n", per);
printf(" Division of Student = %s\n", div);
getch();
}
The conditions are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed and the rest of the ladder is bypassed. If none of the conditions are true, the final else is executed. That is, if all other conditional tests fail, the last else statement is performed. If the final else is not present, no action takes place if all other conditions are false.
Switch Statements
In a switch statement, the value of the switch expression is compared to the constants associated with case labels. If the expression evaluates to the constant associated with a case label, program execution continues at the matching label. If no matching label is present, program execution branches to the default label if present; otherwise execution continues with the statement following the switch statement.
switch (integer expression)
{
case constant1:
statements    <----- optional
case constant2:
statements    <----- optional
default :    <------ optional
statements    <------ optional
} The expression is an integer expression and statement is a block statement with case labels and at most one default label. Every case label has the form case const:, where const is a constant integer expression. All case constants must be different from one another.
The expression in the parentheses following the word switch is evaluated. In this case, it has whatever value you last entered for ch. Then the program scans the list of labels (here, case 'a' :, case 'b' :, and so on) until it finds one matching that value. The program then jumps to that line. What if there is no match? If there is a line labeled default :, the program jumps there. Otherwise, the program proceeds to the statement following the switch.
// To calculate the total no of vowels used in words using switch statements
#include< stdio.h >
#include< conio.h >
void main()
{
char s[20],x=0,r,t;
printf("enter the words");
scanf("%s",s);
clrscr();
t=strlen(s);
for(r=0;r<=t;r++)
{
switch(s[r])
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
x++;
break;
}
}
printf("%s\tTotal Vowels = %d",s,x);
getch();
}
After the jump from the switch to a label, program execution continues sequentially, regardless of other labels. The break statement can be used to exit the switch block at any time. A break is thus necessary if the statements following other case labels are not to be executed.
The default label is optional, and can be placed at any position in the switch body. If there is no default label, and the control expression of the switch statement does not match any of the case constants, then none of the statements in the body of the switch statement is executed. In this case, the program flow continues with the statement following the switch body.


Free Web Hosting