Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Introduction to Computing

Lab 05
Topic Conditional Statement (if-else)
Objective Learning how to build logic by using conditional statement.

Selection:

In selection, the program executes particular statements depending on some conditions. There are multiple
selection statements:

if:

syntax:

if(expression)
{
Body of if(statement);
}
In this selection statement a set of instruction are dependent on the condition/expression of selection
statement if condition/expression is true body of selection statement will be executed. If
condition/expression is false body of selection statement will be skipped and next instructions which are
defined after selection statement will be executed.

if else:

syntax:

if(expression)
{
Body of if(statement);
}
else
{
Body of else(statements);
}
In this selection statement a set of instruction are dependent on the condition/expression of selection
statement if condition/expression is true body of selection statement will be executed. If
condition/expression is false body of selection statement will be skipped and body of else will be executed.
Else part doesn’t have any condition/expression.

if else if:

syntax:

if(expression)
{
Body of if(statement);
}
else if(expression)
{
Body of else(statements);
}
In this selection statement a set of instruction are dependent on the condition/expression of selection
statement if condition/expression is true body of selection statement will be executed. If
condition/expression is false, then next selection statement’s condition/expression will be tested and so on.
If any of the selection statement is tested as true than no further statement will be test and the control of
program will be shifted after those selection statement.

Task#1: Write C++ Program to print positive number entered by the user
Task#2: Write C++ program to find Largest Number in 3 numbers
Task#3:

Take a character input from the user and tell whether the number is a capital letter or a small letter.
Sample Input: A
Sample Output: Capital letter
Sample Input: g
Output:small letter
Sample Input: )
Sample Output: Non
Task#4:
Write and run a program that reads two integers and then uses the conditional expression operator to
print either “multiple” or “not” according to whether one of the integers is a multiple of the other.
Sample Input: 12 6
Output: 12 is the multiple of 6
Sample Input: 12 13
Output: NON
Task#5:
Write c++ program that reads two integers and then uses the conditional expression operator to print either
"multiple" or "not" according to whether one of the integers is multiple of the other.

Task#6:
Write and run a program that simulates a simple calculator. It reads two integers and a character. If the
character is a ‘+’, the sum is printed; if it is a ‘-‘, the difference is printed; if it is a ‘*’, the product is
printed; if it is a ‘/’, the quotient is printed; and if it is a ‘%’, the remainder is printed.
Sample Input: 12%7
Sample Output: 5
Sample Input: 19x10
Sample Output: 190

Task#7:
Write a program of a BMI calculator application that reads the user’s weight and height and then
calculates and displays the user’s body mass index. The formula for calculating BMI is:

BMI = Weight / (Height * Height)

Also, the application should display the following information from the scale provided below.

BMI VALUES

Underweight: less than 18.5


Normal: between 18.5 and 24.9
Overweight: between 25 and 29.9
Obese: 30 or greater
Note: You are allowed to use Logical Operators and If statements.

Task#8:
Write a program which takes as input a floating number and prints its ceiling Integer.
Sample Input: 5.5
Output: 6
Sample Input: -5.5
Output: -5
Sample Input: 5
Output: 5

Task#9:
Write a program which takes as input a floating number and prints its floor value.
Sample Input: 5.5
Output: 5
Sample Input: -5.5
Output: -6
Sample Input: 5
Output: 5

You might also like