Decision making constructs.

Program 2: Programs using decision making constructs

Intro Text

In C programming language decision are plays a vital role. This experiment deals with basic conditions. C program has basically 3 Categories of conditional statements, some say 5. Those conditional statements are

  1. if | if…else statement
  2. if….else…if statement
  3. switch statement
if  and if..else statement

The basic statement for the if statement will be like

 if (condition){
 //BLOCK | Statement goes here!!!
 }

If the condition returns 1 (true). The control will be passed to the following blocks else it will omit the block form the process flow. We can also make the decision like either..or condition in C. we just need to modify the if statement syntax a little. The syntax will be as follows

 if(condition){
 /*BLOCK 1:
 If condition returns true
 this block will run
 */
 }
 else{
 /*BLOCK 2:
 If Condition return other than true
 this block will run
 */
 }

I wrote the simple example which uses If…else conditional statement which will get your age as an input and it tell you whether you are eligible to vote or not. the conditions will be like if age is greater than or equal to 18 you are eligible to vote. else you are not eligible. This scenario will be satisfied by the following program.

Program : Voting Eligibility Check : using if.. else..
/*
 Experiment:Decision Making Construct
 :IF-ELSE Condition
 */
 #include<stdio.h>
 #include<conio.h>
 void main(){
 int age;
 clrscr();
 printf("Enter ur age...");
 scanf("%d",&age);
 if (age>=18){
 printf("\nU are eligible to Vote.");
 }else{
 printf("\nU are not eligible to Vote.");
 }
 getch();
 }
If else ladder

The another way of using if statement is like a ladder. In technical terms if one conditions fails the control will check another condition if that condition again fails it will check the next one until all the conditions are checked or any condition returns true. Don’t be confused. Check the Following program. The program is to find the grade when mark is given as an input. It first check whether the mark is above 90 or not. If it is greater than 90, then your grade will be S . else it will check whether the mark is above 80 [(i.e) marks between 80 and 90].If it is greater than 80, then your grade will be A. The process will go until the check  the mark is greater than 50 if so the grade will be E else it will be U.

Program : Find your grades using marks
/*
 Experiment:Decision Making Construct
 :IF-ELSE-IF ladder
 */
 #include<stdio.h>
 #include<conio.h>
 void main(){
 int mark;
 clrscr();
 printf("FIND YOUR GRADE");
 printf("\n***************");
 printf("\nENTER YOUR MARK...");
 scanf("%d",&mark);
 if (mark>95){
 printf("\nYour grade is S.");
 }
 else if(mark>90){
 printf("\nYour grade is A");
 }
 else if(mark>80){
 printf("\nYour grade is B");
 }
 else if(mark>70){
 printf("\nYour grade is C");
 }
 else if(mark>60){
 printf("\nYour grade is D");
 }
 else if(mark>=50){
 printf("\nYour grade is E");
 }
 else{
 printf("\nYour grade is U");
 }
 getch();
 }
switch statement

Switch is a different kind of conditional statement.Its syntax is as follows

switch(option){
 case 1: //some statements;                 
break;   
 case 2: //some statements;
 break;
 case 'a'://some statements;
 break;
 default://statement;

}

option is a variable which has some value the switch statement make a check case which match with the option. if it finds a match it will run that particular block else it will run the default block.

break in switch statement is necessary to stop the control flow to next statement. the following example is done using switch statement.

Program: calculator
/*
Experiment:Decision Making Construct:SWITCH Condition
*/
#include<stdio.h>
#include<conio.h>
void main(){
float num1,num2,result;
int choice;
clrscr();
printf("\nCALCULATOR");
printf("\n**********");
printf("\n1.Additon\n2.Subtraction\n3.Multiplication\n4.Division");
printf("\nEnter the two numbers...");
scanf("%f%f",&num1,&num2);
printf("\nEnter your choice...");
scanf("%d",&choice);
switch(choice){
case 1: result=num1+num2;break;
case 2: result=num1-num2;break;
case 3: result=num1*num2;break;
case 4: result=num1/num2;break;
default: printf("\nWorng Choice...");
getch();
exit(0);
}
printf("\nResult is...%.2f",result);
getch();
}