You are on page 1of 4

Riphah International University Faculty of Computing

Programming Fundamentals Lab 05 If Conditions


Learning Objective
1. Learn basics of C++
2. Learn how to make decisions in C++
3. Learn how to use if else in C++

The if..else Statement


Quite often we need to control the flow of execution of a program; in particular, to decide if a
certain instruction or block of instructions should be executed, depending on a given
condition. For example, in a more advanced version of our invoice program, we may want to
take into account the fact that some products are taxable and some are not. The program
could ask the user whether the product is taxable or not, and depending on what the user
indicates, calculate things accordingly.

Syntax
The syntax of an if...else statement in C++ is −

if(boolean_expression) {
/ statement(s) will execute if the boolean expression is true }
else {
/ statement(s) will execute if the boolean expression is false
}

If the boolean expression evaluates to true, then the if block of code will be
executed, otherwise else block of code will be executed.

Flow Diagram

Programming Fundamentals Riphah International University


Lab 5: conditions Faculty of Computing
2

Example
#include <iostream>
using namespace std;

int main () {
/ local variable declaration: int

a = 100;

/ check the boolean condition


if( a < 20 ) {
/ if condition is true then print the following
cout << "a is less than 20;" << endl;
} else {
/ if condition is false then print the following cout
<< "a is not less than 20;" << endl;
}
cout << "value of a is : " << a << endl;

return 0;
}

When the above code is compiled and executed, it produces the following result


3

a is not less than 20;


value of a is : 100

Lab Tasks:
1. Write a program that asks the user to enter two numbers. The
program should use the if else to determine which number is
smaller and which is larger

2. The area of a rectangle is the rectangle`s length times its width.


Write a program that asks for the length and width of two
rectangles. The program should tell the users which rectangle has
the greater area.

3. Scientists measure an object mass in kilograms and its weight in


newtons. If you know the amount of mass that an object has,
you can calculate its weight in newtons.

weight = mass * 9.8

write a program that asks the user to enter an object mass and then
calculate the weight of an object. If its weight is more than 1000
newtons then display the message that it’s too heavy. If its weight is
less than 10 newtons then display message that it’s too light.

4. Write a program asks the user to enter three numbers. The program
then displays the greater and smaller numbers on the console.

5. Make Math tutor for the young students. Write a program that
displays two numbers to the user that are to be added. like
247
+129

the program should wait for the student to enter the answer. If the
answer is correct, a message of congratulations should be printed.
If it is incorrect then it should display “incorrect answer”.
4

You might also like