Вы находитесь на странице: 1из 12

Programming Fundamentals

C++

Computer Science
INU Peshawar
Conditional Statements
• Normal computer programs execute sequentially
(statement by statement)
• The sequential order of execution can be altered by
conditional statements
• Conditional statements execute or ignore a set of
statements after testing a condition (test is passed or
failed, satisfied or not satisfied) e.g. “relational
expression”
• Relational expression: expression made of constants,
variables and/or arithmetic values combined (operated) by
a relational operator returning true or false value e.g. 5>1,
a==b
Relational Operators
• The operators that make a relation between two
values or expressions are called relational
operator
• True value is reflected/resulted if the relation is
satisfied otherwise false
• Relational operators are >, <, >=, <=, == and !=
• For example: if x=3, y=6, z=9
Expression Output Expression Output
x>y 0 x<=y 1
x<y 1 x==y 0
x>=y 0 x!=y 1
The “if” Statement
• The “if” statement evaluates a condition and a statement or a
set of statements is executed if the condition is true, otherwise
the statement or the set of statements is ignored (in case the
condition is false)
• For example, when the condition if followed by only one
statement then the syntax is:
if (condition)
statement1;
statement2;
• Statement1 will be executed if the condition is true then
control will come to statement2
• The control will come direct to execute statement2 and ignore
statement1 if the “if” condition is false
The “if” Statement …
• when the condition if followed by more than one
statements then the syntax is:
if (condition)
{ statement1;
statement2;
…;
statementn; }
statementk;
• Set of statements (1-n) will be executed if the
condition is true then control will come to statementk
• The control will come direct to execute statementk
and ignore the set of statements (1-n) if the “if”
condition is false
The “if” statement

Refer to program “ifcond.cpp” for examples


The “if else” Conditional Statement
• if else is the another form of if statement through which
two way decision can be made
• In if else, one condition has two blocks of statements and
either of them will be executed through if else statement
• After condition checking, the first block is executed if the
condition is true else the second block
if (condition)
{first block of statements}
else
{second block of statements}
“if else” … Conditional Statement

Refer to program ifelse.cpp for example


Nested “if” statement
• An “if” statement used within another “if” statement
is called nested “if” statement
• Nested “if” statement is used for multi way decision
making
• Syntax:
if (condition_1)
if (condition_2)
statement_1;
statement_2;
• First condition will be checked, if true, the second will
be checked, if true statement 1 will be executed
Nested “if” Statement

For example refer to program nestedif.cpp


Nested if else Statements
• An if else statement placed inside another if else
statement is called nested if else statement
• Nested if else statement are used for multiple selections
if (condition_1)
statement1;
else if (condition 2)
Statement2;


else if (condition n)
statementn;
Lab Task

• Write a C++ program for finding and displaying


the maximum value among three variables

Вам также может понравиться