Lab No3
Lab No3
3
Use of Conditional statement
Objective
In this lab you will learn how to write C++ conditions (logical expressions), and you
will learn one way of controlling which C++ statement is executed next (the if
statement)
Tools:
Dev C++
Background Knowledge:
In the sequence construct each statement is executed in the order in which it occurs in
the program. However, many times our actions are dependent on certain conditions.
For instance, if it is raining outside, then you will take an umbrella. But if the sun is
shining, you will leave the umbrella at home. Similarly, a program might need to
execute one statement if one condition occurs and execute a different statement if
another condition occurs.
In programming, decision making is used to specify the order in which statements are
executed. Decision making Decision-making structures require that the programmer
specifies one or more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined to be true, and
optionally, other statements to be executed if the condition is determined to be false.
IF Statement
If statement consists of a Boolean expression followed by one or more statements.
Syntax
The syntax of an ‘if’ statement in C programming language is:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
If-else Statement
The flow diagram as shown in figure 3.2
Syntax
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}
Sample Tasks Task 1
Write a program to assign value in any variable and check whether it is less than
20 or not?
Task 2
Write a program to assign value in to variable ‘a’ and check whether it is less
than 20 or not using if else statement
#include<iostream>
Using namespace std;
int main () { int a =
100; if( a < 20 )
{ cout<< “a is less than
20"<<endl;
}
else
{ cout<<"a is not less than 20"<<endl; }
cout<<"value of a is :"<<a<<endl;
}
Task 3
Write a program to read three integer numbers then find and print the largest
one among these numbers.
#include <iostream>
using namespace std;
int main( )
{ int x,y,z,
max;
cin>>x;
cin>>y;
cin>>z; max=x;
if(max<y)
max=y;
if(max<z) max=z; cout<<” The Max Value
is:"<<max<<endl; }
Task 4
Write a program to executes a single statement if the given condition is true
#include <iostream>
using namespace std;
main ()
{ int a, b;
a=100;
b=50;
if(a>b)
cout<<“Lahore”<<endl; cout<<“ok”<<endl;
}
Task 5
Write a C++ program to check whether an alphabet is vowel or consonant using
switch case.
#include <iostream> using namespace std; int main() {
char ch;
// Ask the user to enter an alphabet
cout << "Enter an alphabet: "; cin
>> ch;
// Convert to lowercase to handle both uppercase and lowercase inputs
ch = tolower(ch);
// Check if the input is a letter
if (ch < 'a' || ch > 'z') {
cout << "Invalid input. Please enter a valid alphabet." << endl;
return 1; // Exit with an error code
}
// Use switch case to check if the character is a vowel or
consonant switch (ch) { case 'a': case 'e': case 'i': case 'o':
case 'u':
cout << ch << " is a vowel." << endl;
break;
default:
cout << ch << " is a consonant." << endl;
}
}
Exercise :
Exercise 1:
Write a program that determines a student’s grade. The program will read three types
of scores (quiz, mid-term, and final scores) and determine the grade based on the
following rules:
if the average score =90% =>grade=A
if the average score >= 70% and <90% => grade=B
if the average score>=50% and <70% =>grade=C
if the average score<50% =>grade=F
Solution:
Instructor Signatures:
Remarks:
Exercise 2
Write a program that prompts the user for the current year, the user's current age, and
another year. It then calculates the age that the user was or will be in the second year
entered Solution:
Instructor Signatures:
Remarks:
Exercise 3
Write a program to calculate the electricity bill. The rates of electricity per unit are as
follow If the units consumed are equal or less than 300, then the cost is Rs 3/ per unit
If units consumed are more than 300, then the cost is Rs. 3.5/- per unit and a surcharge
of 5% of bill is added.
Solution:
Write your code here
Instructor Signatures:
Remarks:
Exercise 4
Write a C program to create Simple Calculator using switch case.
Solution:
Write your code here
Instructor Signatures:
Remarks:
FINALEVALUTION LAB 03
Student Name:
Registration
Number:
Date:
Marks Obtained Total Marks
Instructor
Signatures:
Remarks: