Академический Документы
Профессиональный Документы
Культура Документы
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