You are on page 1of 8

GC UNIVERSITY FAISALABAD

Department of Electrical Engineering

Introduction to Computer Programming


CSE-122
Lab # 6

Conditional Statements

Supervised By: Engr. Arslan Ahmad


Lab Instructor: Engr. Muhammad Fraz Anwar
1

Objective:
The student should practice the following statements:
One-Way if
Two-Way if
Multi-Way if
Nested if
Switch statement

Scope:
Student should know the:
Syntax of if and nested if statements.
Syntax of switch and nested switch statements.

Useful Concept
If statement:
Sometimes in your program you need to do something if a given a condition is satisfied. Suppose
for example, you want to evaluate the square root of a number entered by the user. To find the
square root of that number, it must be greater than zero:
cout<<Enter a number: ;
cin>>num;
if (num > 0)
cout<<The square root is :<< sqrt(num);

If you want to do more than one thing based on that condition, then your statements should be
enclosed with brackets:
cout<<Enter a number: ;
cin>>num;
if (num > 0)
{
cout<<The square root is : << sqrt(num);
cout<<The natural logarithm is :<< log(num);
}

In case you want to have an alternative action to be taken if the condition is not satisfied, then
use an ifelse statement. For example:
cout<<Enter a number: ;
cin>>num;
if (num >= 0)
cout<<The number is positive;
else
cout<<The number is negative;

Here, if the condition is true, the if-part will be executed and the else-part is ignored. But if the
condition is false, the if-part is ignored and the else-part is executed.
Be careful with compound statements, forgetting the brackets will produce an error because only
the first statement is executed if the condition is true, the rest are considered to be unrelated
statements and the compiler will complain about the keyword else.
In some cases you may have multiple alternatives for given information. Consider this example:
cout<<Enter the grade: ;
cin>>grade;
if (grade > 90)
cout<<your grade is A;
else if(grade > 80)
cout<<your grade is B;
else if (grade > 70)
cout<<your grade is C;
else if (grade > 60)
cout<<your grade is D;
else
cout<<your grade is F;

Example 1: Here each condition is tested one by one starting from the first one. If one of them
is true, then the statement associated with that condition is executed and the rest are ignored.
Consider the following example containing nested if
This program prompts the user for the age and determines if he is child, teen, adult, retired senior,
or working senior.
#include <iostream.h>
#include <stdio.h>
int main(void){
int age;
char status;
cout<<Enter the age: ;
cin>>age;
if (age > 59) {
cout<<Enter work status:;
cin>>status;
if(status == W || status == w)
cout<<Working senior;
else
cout<<Retired senior;
}
else if(age > 20)
cout<<Adult;
else if (age > 12)
cout<<Teen;
else
cout<<Child;
3

getche();
return 0;
}

Switch statement:
Another useful statement in C is the switch statement. This statement is somehow similar to if
statement in giving you multiple options and do actions accordingly. But its behavior is different.
This statement tests whether an expression matches one of a number of constant integer values
Labeled as cases. If one matches the expression, execution starts at that case. This is the general
structure of the switch statement:
switch (expression) {
case constant: statements break;
case constant: statements break;
case constant: statements break;
default: statements
}

The default clause is optional. If it is not there and none of the cases matches, no action is taken.
The break keyword is used to skip the switch statement. For example if a case matches the
expression and no break key words are used, the execution will go for all the statements in all
cases. Consider this example:
cout<<Enter your grade: ;
cin>>grade;
switch (grade) {
case A:
case B:
cout<<Good standing;
break;
case C:
cout<<O.K.;
break;
case D:
case F:
cout<<Poor, student is on probation;
break;
default:
cout<<Invalid letter grade;
}

Example-2:- A number is entered through the keyboard. If the number even program display the
message that number is even otherwise show message that number is odd.
/* if else statments */
#include <iostream.h>
#include<conio.h>

void main( )
{
int number ;
cout<< "Enter number : " ;
cin>>number;
if ( number%2 == 0 )
{
cout<<Number is even :;
}
else
{
cout<<Number is odd :;
}
getche();
}
Here is the output:

Example -3:- The expressions in a multiple-alternative decision are evaluated in sequence until a
true condition is reached. If an expression is true, the statement following it is executed, and the
rest of the multiple-alternative decision is skipped. If an expression is false, the statement
following it is skipped, and the next expression is checked. If all the expressions are false, the
statement following the final else is executed. For example, to check the value of the variable
marks and print the message accordingly, we would write.
#include <iostream.h>
#include<conio.h>
void main( )
{
int marks;
cout<<Enter marks :;
cin>>marks;
if (marks >= 75)
cout << Distinction\n ;
else if (marks >= 60)
cout << Pass\n ;
else if (marks >= 50)
cout << Average\n ;
else
cout << Fail\n ;
getche();
}
Heres the programs output:

Example-4:- Write an interactive program that contains a compound if statement and that may be
used to compute the area of a square (area=side2) or a triangle (area=base*height/2) after
prompting the user to type the first character of the figure name (S or T).
#include <iostream.h>
#include<conio.h>
void main( )
{
char choice;
float side, height, base, area;
cout<<"To find the shape's area choose either 'S' for square , or 'T'
for triangle : ";
cout<<\nEnter your choice to find the area : ";
cin>> choice;
switch(choice)
{
case 'S':
cout<<"\nEnter its side: ";
cin>>side;
area = (side * side);
cout<<"The area of square is :"<< area;
break;
case 'T' :
cout<<"\nEnter the height and base: ";
cin>> height>> base;
area = (0.5) * height * base;
cout<<" The area of triangle :"<< area;
break;
default:
cout<<"Sorry ,your choice is not included \nPlz try
again ";
}
getche();
} // end of main
Heres the programs output:

Exercises for Lab:


Exercise 1: This program finds the larger of two variables x and y and print the answer and
assign the larger value to max variable and smaller value to min variable.
Exercise 2: Find a logical error in the program.
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
int i ;
cout<< "Enter value of i " ;
cin>> i ;
if ( i = 5 )
cout<< "You entered 5" ;
else
cout<<"You entered something other than 5";
getche();
return 0;
}

Post Lab Task:


1) Find a logical error in the program.
#include <iostream.h>
#include<conio.h>
void main( )
{
char choice;
float side, height, base, area;
cout<<"To find the shape's area choose either 'S' for square , or 'T'
for triangle : ";
cout<<\nEnter your choice to find the area : ";
cin>> choice;
switch(choice)
{
case 'S':
cout<<"\nEnter its side: ";
cin>>side;
area = (side * side);
cout<<"The area square is :"<< area;
case 'T' :
cout<<"\nEnter the height and base: ";
cin>> height>> base;
area = (0.5) * height * base;
cout<<" The area of triangle :"<< area;
default:
cout<<"Sorry ,your choice is not included \nPlz try again ";
}
getche();
} // end of main

2) Write a program that takes date as input in (dd/mm/yyyy) format and display date in (month
name day, yyyy) format using switch statement.
3) The current year and the year in which the employee joined the organization are entered through
the keyboard. If the number of years for which the employee has served the organization is greater
than 3 then a bonus of Rs. 2500/- is given to the employee. If the years of service are not greater
than 3, then the program should do nothing.
4) Write a program that takes 5 digit number form user in an integer variable and calculate the sum
of all digits.
Hints: - if five digit number is 89754 then out is 8+9+7+5+4=33
5) write a program that have ten integer type of variables. Your program should prompt value in
each variable and calculate the largest values among all variables.

You might also like