0% found this document useful (0 votes)
35 views3 pages

Nested Conditional Statment

The document introduces conditional statements (if-else) in computing. It provides the syntax for multiple if statements, where each expression is tested and executed accordingly if true. It also covers nested if statements, where there is an if statement within the body of another if statement. The tasks involve writing programs to: 1) perform unit conversions based on user input, 2) simulate a rock-paper-scissors game, 3) determine if one integer is a multiple of another, and 4) simulate a basic calculator for arithmetic operations.

Uploaded by

Anas Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

Nested Conditional Statment

The document introduces conditional statements (if-else) in computing. It provides the syntax for multiple if statements, where each expression is tested and executed accordingly if true. It also covers nested if statements, where there is an if statement within the body of another if statement. The tasks involve writing programs to: 1) perform unit conversions based on user input, 2) simulate a rock-paper-scissors game, 3) determine if one integer is a multiple of another, and 4) simulate a basic calculator for arithmetic operations.

Uploaded by

Anas Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to Computing

Topic Conditional Statement (if-else)


Objective Learning how to build logic by using conditional statement.

multiple if:

syntax:

if(expression)
{
Body of if(statement);
}
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. But it is quite
different from above mention statements. Because, if any selection statement is true it will not have
skipped any statement. Each and every selection statement will be tested and executed accordingly.

nested if:

syntax:

if(expression)
{
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 which is another
selection statement.
Task 1

Write a program that takes one value from the user, asks about the type of conversion and then
performs a conversion depending on the type of conversion.

If user enters:

 I -> convert from inches to centimeters. [1 inch = 2.54 centimeter]

 C -> convert from centimeters to inches. [1 centimeter = 0.393701 inch]

 G -> convert from gallons to liters. [1 gallon = 3.78541 liter]

 L -> convert from liters to gallons. [1 liter = 0.264172 gallon]

 M -> convert from mile to kilometer. [1 mile = 1.60934 kilometer]

 K -> convert from kilometer to mile. [1 kilometer = 0.621371 mile]

 P -> convert from pound to kilogram. [1 pound = 0.453592 kilogram]

 O -> convert from pound to ounce. [1 pound = 16 ounce]

 F -> convert from Fahrenheit to Celsius. [ c = (f – 32) * (5 / 9)]

 C -> convert from Celsius to Fahrenheit. [ f = c * (9 / 5) + 32]

Task 2

Write and run a program that plays the game of “Rock, paper, scissors.” In this game, two players
simultaneously say (or display a hand symbol representing) either “rock,” “paper,” or “scissors.” The
winner is the one whose choice dominates the other. The rules are: paper dominates rock, rock
dominates scissors, and scissors dominate paper. You can use 1=rock,2=paper,3=scissors

Sample Input: 1 1

Sample Output: Draw Sample Input: 1 2

Sample Output: 2nd player wins


Task 3

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 = 6 * 2
Sample Input: 18 8
Output: 18 = 8 * 2 + 2
Sample Input: 12 13
Output: 14 = 12 * 1 + 1
Task 4

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 Input: 12 $ 7


Sample Output: 5 Sample Output: Invalid operator Entered
Sample Input: 19x10 Sample Input: 10/3
Sample Output: 190 Sample Output: 3.33333

You might also like