BARANI INSTITUTE OF SCIENCE SAHIWAL
ASSIGNMENT # 01
Subjects:
ICT
Submitted by:
Hira Mehmood
Submitted to:
Hina Umbreen
Registration No:
20-BIS-2431
“Department of CS”
2
Contents
2. Conditional Statements in C++ or if Statements................................................................4
2.1. The if statement..........................................................................................................4
Syntax..................................................................................................................................4
2.2. The if-else statement...................................................................................................4
Syntax..................................................................................................................................5
Example...............................................................................................................................5
2.3. Nested if-else Statement.............................................................................................5
Syntax..................................................................................................................................5
Example...............................................................................................................................6
3
1. Conditional Statements in C++ or if Statements
Conditional statements, also known as selection statements, are used to make decisions
based on a given condition. If the condition evaluates to True, a set of statements is
executed, otherwise another set of statements is executed.
1.1. The if statement
The if statement selects and executes the statement(s) based on a given condition. If the
condition evaluates to True then a given set of statement(s) is executed. However, if the
condition evaluates to False, then the given set of statements is skipped and the program
control passes to the statement following the if statement.
Syntax
if (condition) {
statement 1;
statement 2;
statement 3;
1.2. The if-else statement
The if – else statement causes one of the two possible statement( s) to execute, depending
upon the outcome of the condition.
Here, the if-else statement comprises two parts, namely, if and else. If the condition is True
the if part is executed. However, if the condition is False, the else part is executed.
Syntax
The syntax of the if-else statements is
1 if (condition) // if part {
4
2 statement1;
3 statement2;
4 }
5 else // else part
6 statement3;
Example
A code segment to determine the greater of two numbers
1.3. Nested if-else Statement
A nested if-else statement contains one or more if-else statements. The if else can be nested
in three different ways, which are discussed here.
• The if – else statement is nested within the if part.
Syntax
if (condition1) {
statement1;
if(condition2)
statement2;
else
statement3; }
else
statement4;
Example
A code segment to determine the largest of three numbers