You are on page 1of 21

Computer applications

Lec-3
Engr Memoona Saif
Compound condition
• A type of comparison in which more than one conditions are evaluated
is called compound condition.
• If logical operator is used in compound condition
Logical operators
• These are used to evaluate compound conditions . There three types of
logical operators
• AND operator(&&)
• OR operator(||)
• Not operator(!)
AND operator
• Symbol used is &&
• It is used to evaluate two conditions: It produces true result if both
conditions are true
• It produces false result if any one condition is false.
Example
• A=60, B=70
• (A>50 && B>50)
• Both conditions are true so whole compound condition is true
• \\\\\
• A=40,B=60
• One condition is false so whole compound statement is false
OR operator
• Symbol used is (||)
• It is used to evaluate two conditions: It results true if only one
condition is true, It produces false if both conditions are false
Example OR Operator
• A=20, B=40;
• (A>20 || B>30)
• One condition is true so whole compound condition is true
• \\\\\\
• A=10, B=15
• A>20 || B>30
• Both conditions are false so whole compound condition is false
Example
Example using logical operators
Not operator
• Symbol used for not operator is !;
• It is used to reverse the result of a condition. If condition is true it will
result false, if condition is false result will be true
Example not operator
Switch structure
• Switch structure compares the result of single expression with multiple cases. Expression
can be any valid expression that results in integer or character value. The expression is
evaluated at the top of switch statement and its result is compared with different cases.
each case represents one choice. If the result matches with any case, the corresponding set
of statements will be executed. Any number of cases can be used in a switch statement.
• The default label appears at the end of all blocks cases. It s executed only if result of
expression does not match with any case . It can be places before the first case or after the
last one
• The break statement in each case is used to exit from switch body. It is used at the end of
each case block. When the result of expression matches with a case block, the
corresponding statements are executed
• If break is not used then all case blocks which come after the case block will be executed.
Switch Structure
• It is another conditional structure , it is a good alternative of nested if
structure.
• It is easy to use when there are many choices and only one is needed
to select.
• Nested if becomes vey complicated in this situation
Example switch structure
Switch Structure

You might also like