Friday, January 9, 2009

Basic Structure of a Program : Selection

Basic Structure of a Program : Selection
Selection construct in any programming language is for controlling the execution of certain part of an algorithm when certain condition occurs. In C/C++ language this structure is implemented with if/if-else if-else and switch-case statements. For example we decide not to drink tea when it is hot, we eats when we feel hungry, A lecturer decides that a student passes an exam if his mark is more or equal to 60. Bellow is an example of if construct :

First Example : if
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
float floatGrade; /*Declaring a variable for storing a student's grade*/
system("clear");
printf("Student's Mark : -->");
scanf("%f", &floatGrade);
if (floatGrade >= 60.00)
{
printf("Passes\n");
}
else
{
printf("Fails\n");
}
return 0;
}

Second Example : if/else-if/else
In the next example we specify that a student get :
1.Grade C if his mark is more or equal 60 and less than 70
2.Grade B if his mark is more or equal 70 and less than 80
3.Grade A if his mark is more or equal 80 and less or equal than 100
4.Fails if his mark is more or equal 0 and less than 60
5.The program will say that the value is not legal if it is not in the range between 0 and 100.
The sorce code is :
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
float floatGrade; /*Declaring a variable for storing a student's grade*/
system("clear");
printf("Student's Mark : -->");
scanf("%f", &floatGrade);
if (floatGrade >= 60.00 && floatGrade < 70) /*Condition for grade C*/
{
printf("Passes with grade C\n");
}
else if (floatGrade >= 70.00 && floatGrade < 80) /*Condition for grade B*/
{
printf("Passes with grade B\n");
}
else if (floatGrade >= 80.00 && floatGrade <= 100) /*Condition for grade A*/
{
printf("Passes with grade A\n");
}
else if (floatGrade >= 0.00 && floatGrade < 60) /*Condition for Fail*/
{
printf("Fails \n");
}
else /*Specifying that another value outside the range of 0 to 100 is not legal*/
{
printf("Value is not legal\n");
}
return 0;
}

Third Example : switch-case
Another selection type is using switch-case. A switch-case construct consists of :
1.switch key word accompanied by the variable that will be evaluated to determine which program statement that will be executed.
2.program statements inside the switch-case nest.
The example of this statement is that a student will be awarded a sum of money, so that he can go to the movie.
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h" /*We use toupper to convert the lowercase input to uppercase*/
int main()
{
char chrGrade;

system("clear");
printf("Student Grade :---> ");
scanf("%c", &chrGrade);
chrGrade=toupper(chrGrade);
switch(chrGrade)
{
case 'A' : printf("You are awarded $100");break;
case 'B' : printf("You are awarded $50");break;
case 'C' : printf("You are awarded $25");break;
case 'D' : printf("You are awarded $0");break;
default : printf("Wrong Grade");break;
}
return 0;
}

Now you have already learn how to create a selection construct.