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

Lab 5 - if else

The document provides an overview of conditional statements in programming, specifically focusing on if-else statements and switch statements. It explains the syntax and usage of one-condition, two-conditions, and multiple conditions through nested if or else-if statements. Additionally, it introduces the conditional operator (?:) and its functionality in evaluating expressions based on boolean conditions.

Uploaded by

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

Lab 5 - if else

The document provides an overview of conditional statements in programming, specifically focusing on if-else statements and switch statements. It explains the syntax and usage of one-condition, two-conditions, and multiple conditions through nested if or else-if statements. Additionally, it introduces the conditional operator (?:) and its functionality in evaluating expressions based on boolean conditions.

Uploaded by

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

SPRING 2024 – DE 45 MTS

CS – 114
Fundamentals of Programming
(LAB - 5)
Instructors:
Dr. Ayesha Zeb
Dr. Tahir Habib Nawaz
LE Hamza Sohail

Department of Mechatronics Engineering, National University of


Sciences and Technology, College of E&ME
Conditional Statements
Conditional Statements in computer programming are those which
perform different computations or actions depending on whether a
programmer specified boolean condition turns out to be true or false.
There are two different types of conditional statements:
• If-else statements
• switch statements
Cases
• One-Condition(if statement)
• Two-Conditions(if-else statements)
• Multiple Conditions: Nested if or elseif
If Statement
• For only one condition just the “if” statement is required. The syntax
of one-way selection is:
if(expression)
statement
• The statement is executed if the value of the expression is true
• The statement is bypassed if the value is false; program goes to the
next statement
• “If” is a reserved word.
If Statement
If Statement Example
If-else Statement
• For two conditions we use if-else statements. The syntax for such
statements is:
if(expression)
statement1
else
statement2
• If expression is true, statement1 is executed; otherwise, statement2 is
executed – statement1 and statement2 are any C++ statements
• else is a reserved word
If-else Statement
If-else Statement
Multiple Conditions
• To cater with multiple conditions, there can be two ways:
• 1. Nesting: one control statement in another Nested if-else
statements can be used
• 2. Another and better way can be the use of else-if statements
Conditional Operator ?:
• Conditional operator (?:) takes three arguments –
• Ternary operator
• Syntax for using the conditional operator: expression1 ? expression2 :
expression3
• If expression1 is true, the result of the conditional expression is
expression2 –
• Otherwise, the result is expression3
Conditional Operator ?:

You might also like