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

Manual For 4th Lab

Uploaded by

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

Manual For 4th Lab

Uploaded by

nineleven2241
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

1

University of Management and Technology

Lahore Campus

========================================================

Session: Fall 2024

Instructor: Somia Waseem

Email: [email protected]

Course: Programming Fundamentals Lab

Course Code: CC111L

Department of Computer Science

School of Systems and Technology

Department of Computer Science (SST), UMT.


2

Conditional Statements in C++

Objectives:
To understand and apply conditional statements in C++:
1. if statements.
2. if-else statements.
3. Nested if statements.
4. if-else-if Ladder or Multiple if-else-if statements.
5. switch statement.
6. Conditional operator.
By the end of this lab, students will be able to create decision-making logic in their
programs.

Scope:
The student should know the following at the end of this Lab:

1. Syntax of if statements.

2. Syntax of if-else statements.

3. Syntax of Nested if statements.

4. Syntax of if-else-if Ladder or Multiple if-else-if statements.

5. Syntax of switch statements.

6. Syntax of Conditional operator.

Department of Computer Science (SST), UMT.


3

Introduction:
The conditional statements (also known as decision control structures) such as if,
if else, switch, etc. are used for decision-making purposes in C ++ programs.
They are also known as Decision-Making Statements and are used to evaluate
one or more conditions and make the decision whether to execute a set of
statements or not. These decision-making statements in programming languages
decide the direction of the flow of program execution.

Types of Conditional Statements


Following are the decision-making statements available in C++:
1. if Statement.
2. if-else Statement.
3. Nested if Statement.
4. if-else-if Ladder.
5. switch Statement.
6. Conditional Operator.

1. if Statement:
This is the simplest decision-making statement. It is used to decide whether a
certain statement or block of statements will be executed or not i.e. if a certain
condition is true then a block of statements is executed otherwise not.

Department of Computer Science (SST), UMT.


4

Syntax:

If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by
default if statement will consider the first immediately below statement to be
inside its block.

Example 1:
You want to check the number is positive, negative or zero:

Department of Computer Science (SST), UMT.


5

Output:

Example 2:
You want to display a congratulatory message in case a student has passed a course.
The passing marks are 50%.

Department of Computer Science (SST), UMT.


6

Output:

2. if-else Statement:
The if-else statement provides an alternative block of code if the condition is
false. We can use else statement with if statement to execute a block of code
when the condition is false. The if-else statement consists of two blocks, one
for false expression and one for true expression.

Syntax:

Department of Computer Science (SST), UMT.


7

Example 1
You want to display a congratulatory message in case a student has passed a course
and sorry message if student failed. The passing marks are 50%.

Output:

Department of Computer Science (SST), UMT.


8

Example 2:
You want to check the number whether it is a positive or negative number.

Output:

Department of Computer Science (SST), UMT.


9

3. Nested if-else Statement:


Nested if statements are if statements placed inside another if or else block. A
nested if is an if statement that is the target of another if statement.

Syntax:

Example:
Write a program that determines if a student is eligible for a scholarship based
on their age and marks.

• The student is eligible for the scholarship if they are 18 years or older and
have marks of 60 or above.
• If the student is 18 or older but has marks below 60, they are not eligible
for the scholarship.
• If the student is under 18, they are considered underage for the scholarship.

Department of Computer Science (SST), UMT.


10

Output:

Department of Computer Science (SST), UMT.


11

4. if-else-if Ladder or Multiple if-else-if Statement:


The if-else-if statements are used when the user has to decide among multiple
options. The if-else-if ladder allows multiple conditions to be checked
sequentially. The if statements are executed from the top down. As soon as one
of the conditions controlling the if is true, the statement associated with that if
is executed, and the rest of the else-if ladder is bypassed. If none of the
conditions is true, then the final else statement will be executed. if-else-if ladder
is similar to the switch statement.

Syntax:

Example:
Input five values from user and display the maximum number from the list.

Department of Computer Science (SST), UMT.


12

Output:

Department of Computer Science (SST), UMT.


13

5. Switch Statement:
The switch statement is an alternative to the if-else-if ladder that can be used to
execute the conditional code based on the value of the variable specified in the
switch statement. Basically, it allows multiple choices based on the value of a
variable.
The switch block consists of cases to be executed based on the value of the
switch variable. But its behavior is different. This statement tests whether an
expression matches one of a number of constant integer values labelled as cases.
If one matches the expression, execution starts at that case.

Rules of the switch case statement


Following are some of the rules that we need to follow while using the switch
statement:
➢ In a switch statement, the “case value” must be of “char” and “int” type.
➢ There can be one or N number of cases.
➢ The values in the case must be unique.
➢ Each statement of the case can have a break statement. It is optional.
➢ The default Statement is also optional.

break: This keyword is used to stop the execution inside a switch block. It
helps to terminate the switch block and break out of it. When a break
statement is reached, the switch terminates, and the flow of control jumps to
the next line following the switch statement.
The break statement is optional. If omitted, execution will continue on into
the next case. The flow of control will fall through to subsequent cases until a

Department of Computer Science (SST), UMT.


14

break is reached.

default: The default keyword is used to specify the set of statements to


execute if there is no case match.
It is optional to use the default keyword in a switch case. Even if the switch
case statement does not have a default statement, it would run without any
problem.

Syntax:

Example1:
Write a C++ program using switch statement to display the name of the day of the
week based on a user-inputted integer (1 for Monday, 2 for Tuesday, etc.)

Department of Computer Science (SST), UMT.


15

Output:

Example2:
Demonstration of switch statement.

Department of Computer Science (SST), UMT.


16

Department of Computer Science (SST), UMT.


17

Output:

6. Conditional Operator:
The conditional operator is also known as a ternary operator. It is used to write
conditional operations provided by C++. The ‘?’ operator first checks the given
condition, if the condition is true then the first expression is executed otherwise
the second expression is executed. It is an alternative to an if-else condition in
C++.

Syntax:

Department of Computer Science (SST), UMT.


18

Example1:
Write a C++ program to demonstrate the use of ternary/conditional operator to
find the max from two numbers.

Department of Computer Science (SST), UMT.


19

Lab Tasks:
Task 1: Write a program that asks for the user's age and prints whether they are
eligible to vote (18 or older).
Task 2: Write a program to check if a given integer is even or odd. If even, print
"Even number"; otherwise, print "Odd number.
Task 3: Design a program that assigns a letter grade to a student's score using an if-
else-if statement. Use the grading scale:

• 90-100: A
• 80-89: B
• 70-79: C
• 60-69: D
• Below 60: F

Task 4: Write a C++ program that takes a month number as input and outputs the
corresponding season (winter, spring, summer and autumn). Solve by using switch
statement.

Task 5: Prompt the user to enter the salary and grade of an employee. If the
employee has a grade greater than 15 then add 50% bonus to the employee’s salary.
Otherwise, if the employee’s grade is less than 15 then add 25% bonus to the
employee’s salary.

Department of Computer Science (SST), UMT.


20

Home Tasks:
Task 1: Create a calculator that performs basic operations (addition, subtraction,
multiplication, and division) based on user input. Use a switch statement to handle
the operation selection.
Task 2: Prompt the user to input 5 values and display the minimum number amongst
them.
Task 3: Write a C++ program that uses a switch statement to determine if a number
is even or odd.
Task 4: Prompt the user to enter 3 values. For any equal values, the program should
display the numbers that are equal. (For example: user input 34,6,34 the program
should display the message that the 1st and 3rd values are equal). Solve by using
switch statement.
Task 5: Ask the user to enter marks obtained in a course and the total marks of the
course. Then display a menu as:

Press 1 to calculate Percentage.

Press 2 to display Grade.

If the user presses 1 then percentage should be displayed and if the user presses 2
the grade against the marks should be displayed.

Department of Computer Science (SST), UMT.

You might also like