Lab 04. Relational, Logical and Conditional Operators in C++
Lab 04. Relational, Logical and Conditional Operators in C++
The aim of this lab is to cover some other basic operators of C++
programming.
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++
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(condition)
{
statement1;
statement2;
…
…
statement(n);
}
NUST Institute of Civil Engineering (NICE-SCEE)
National University of Sciences & Technology (NUST)
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()
{
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:
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.