0% found this document useful (0 votes)
6 views

Lab 04. Relational, Logical and Conditional Operators in C++

The document outlines Lab 04 for the CS112 course at NUST, focusing on relational and conditional operators in C++. It includes explanations of relational operators, conditional statements, and provides examples and tasks for students to complete, such as calculating car insurance premiums and creating simple diagnostic tools. The lab aims to enhance students' understanding of C++ programming through practical exercises.

Uploaded by

Syeda Noorain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab 04. Relational, Logical and Conditional Operators in C++

The document outlines Lab 04 for the CS112 course at NUST, focusing on relational and conditional operators in C++. It includes explanations of relational operators, conditional statements, and provides examples and tasks for students to complete, such as calculating car insurance premiums and creating simple diagnostic tools. The lab aims to enhance students' understanding of C++ programming through practical exercises.

Uploaded by

Syeda Noorain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

NUST Institute of Civil Engineering (NICE-SCEE)

National University of Sciences & Technology (NUST)

CS112: Computer Programming

Lab 04: Relational and Conditional Operators in C++

Instructor: Qurrat-ul-ain Babar


NUST Institute of Civil Engineering (NICE-SCEE)
National University of Sciences & Technology (NUST)
Lab 04: Relational and Conditional Operators in C++

The aim of this lab is to cover some other basic operators of C++
programming.

Our aims today are:


To learn
 Relational Operators
 Conditional Operators

Deliverables
Execute all Tasks given in this document and submit on LMS.

Relational Operators
 In order to evaluate a comparison between two expressions
 The result of a relational operation is a Boolean value i.e. True or False.
 Here is a list of the relational and equality operators that can be used in C++

There are following relational operators supported by C++ language

Assuming variable A holds 10 and variable B holds 20, then

Operat Description Example


or

== Checks if the values of two (A == B) is not true.


operands are equal or not, if yes
then condition becomes true.

!= Checks if the values of two (A != B) is true.


operands are equal or not, if
values are not equal then
condition becomes true.

> Checks if the value of left (A > B) is not true.


operand is greater than the
value of right operand, if yes
then condition becomes true.
NUST Institute of Civil Engineering (NICE-SCEE)
National University of Sciences & Technology (NUST)

< Checks if the value of left (A < B) is true.


operand is less than the value of
right operand, if yes then
condition becomes true.

>= Checks if the value of left (A >= B) is not true.


operand is greater than or equal
to the value of right operand, if
yes then condition becomes true.

<= Checks if the value of left (A <= B) is true.


operand is less than or equal to
the value of right operand, if yes
then condition becomes true.

Conditional Statement (if & if-else Statement):


 The “if statement” is used to execute a set of statements after testing a
condition.

 “if-else statement” have two part i.e. TRUE and FALSE

 If the condition is true then the TRUE part executes and if the condition is
false then the FALSE part executes.

Syntax:
If you have a single statement that needs to be executed if the condition is true
then the syntax is as follows:

if(condition)
statement;

If you have multiple statements that needs to be executed if the condition is


true then the { } are used to group the statements and the syntax becomes as
follows:

if(condition)
{
statement1;
statement2;


statement(n);
}
NUST Institute of Civil Engineering (NICE-SCEE)
National University of Sciences & Technology (NUST)

Example 1 – Nested if statements


The program simulates how a car insurance company determines your
premium. The user provides the requested information and the program
determines the insurance premium. The insurance premium is based on the
user's gender, age, and number of traffic tickets he has received (numTix).
The formula for determining the insurance premium is:

Gender
if(condition) Age Premium
{ Male Under 21 1500 + 200 *
statement1; numTix

Male 21 to 29 1200 + 100 *

statement(n); numTix
} Male Over 29 1000 + 100 *
else numTix
{ Female Under 21 1200 + 200 *
statement1; numTix
Female
… 21 and over 1000 + 100 *
… numTix
statement(n);
}

Given below is the decision making graph for the car insurance premium.
NUST Institute of Civil Engineering (NICE-SCEE)
National University of Sciences & Technology (NUST)

CODE
/* Program to calculate the car insurance premium based on the following
rules:
- (Gender); (Age); (Annual Premium)
- Male; Under 21; 1500 + 200 for every ticket on record
- Male; 21 to 29; 1200 + 100 for every ticket on record
- Male; 30 and older; 1000 + 100 for every ticket on record
- Female; Under 21; 1200 + 200 for every ticket on record
- Female; 21 and older; 1000 + 100 for every ticket on record
*/

#include <iostream>
using namespace std;

int main()
{

char gender; // The customer's gender


int age; // The customer's age

int numTix; // Number of tickets the customer has had


int premium; // Customer's insurance premium

// Get information from the user


// We will assume that the user always enters correct input

// Get the user's gender


cout << "Enter your gender (M/F): ";
NUST Institute of Civil Engineering (NICE-SCEE)
National University of Sciences & Technology (NUST)
cin >> gender;

// Get the user's age


cout << "Enter your age as an integer: ";
cin >> age;

// Get the number of tickets


cout << "How many tickets have you been issued? ";
cin >> numTix;

// Use nested if statements to calculate the insurance premium

// Outer if statement: Check the gender


if ( gender == 'M' )
{
// The inner series of if, else if, and else statements checks
// the age and determines the premium.
if ( age < 21 )
{
premium = 1500 + 200 * numTix;
}
else if ( age >= 21 && age < 30 )
{
premium = 1200 + 100 * numTix;
}
else // Only other option is 30 or older
{
premium = 1000 + 100 * numTix;
}
} // end if gender is male

// Matching else for the outer if statement


else // Use else because the only other option is gender == 'F'
{
// The inner series of if, else if, and else statements tests
// the age and determines the premium.
if ( age < 21 )
{
premium = 1200 + 200 * numTix;
}
else // Only other option is 21 or older
{
premium = 1000 + 100 * numTix;
}

} // end gender else

// Print the result


cout << "Your premium is $" << premium << endl;
}

The nested if...else statement allows you to execute a block code among
many alternatives.
NUST Institute of Civil Engineering (NICE-SCEE)
National University of Sciences & Technology (NUST)

Task 1:
You will write a simple medical diagnosis tool. Of course this is not a real
medical tool, it is just an example for our program. The possible scenarios
and outcomes are:

 Don't have a fever and don't have a stuffy nose: Hypocondriac


 Don't have a fever and have a stuffy nose: Head Cold
 Have a fever, don't have a rash, and ear hurts: Ear Infection
 Have a fever, don't have a rash, and ear doesn't hurd: Flu
 Have a fever and have a rash: Measles

(Decision graph given on the next page)

Task 2
NUST Institute of Civil Engineering (NICE-SCEE)
National University of Sciences & Technology (NUST)
Write a program to input a number and show that the input number is EVEN or ODD
(using if statement and conditional operation)

Task 3
Write a program to input two numbers and show the greater number (using
conditional operator).

Task 4
Write a program to input three integer values. Compare the numbers and show the
highest number or lowest number

Task 5
Write a program by taking two float values and show the average of two numbers.

Task 6
Write a program that takes first 6 natural numbers and prints the sum of even
numbers.

You might also like