Defined Compound Condition || Logical Operators || The switch Statement with example

Compound Condition

A type of comparison in which more than one conditions are evaluated is called compound condition. It is used to execute a statement or set of statements by testing many conditions.

Defined Compound Condition  || Logical Operators || The switch Statement with example

Example

Suppose a program inputs two numbers. It displays OK if one numbers is greater than 100 and second number is less than 100. The compound condition is executed by using logical operators.

Logical Operators

Logical operators are used to evaluate compound conditions or reverse the logic of an expression. 

There are three logical operators in C++ language

  • AND operator (&&)
  • OR operator ( | | )
  • NOT operator (!)

AND Operator (&&)

The symbol used for AND operator 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

pose we have two variables A 100 and B-50. The compound condition (A>10) && (B>10) is true. It contains two conditions and both are true. So the whole compound condition is also true.

The compound condition (A>50) && (B>50) is false. It contains two conditions. One condition (A>50) is true and second condition (B>50) is false. So the whole compound condition is false.

OR Operator ( | | )

The symbol used for OR operator is (||). It is used to evaluate two conditions. It produces true result if either condition is true. It produces false result if both conditions are false. 

Example

Suppose we have two variables A=100 and B=50. The compound condition (A>50) || (B>50) is true. It contains two conditions and one condition (A>50) is true So the whole compound condition is also true. The compound condition (A>500) || (B>500) is false because both conditions are false.

NOT Operator (!)

The symbol used for NOT operator is (!). It is used to reverse the result of a condition. It produces true result if the condition is false. It produces false result if the condition is true.

Example

Suppose we have two variables A = 100 and B = 50 The condition (AB) is true. The result of ( A ==B) is false but NOT operator converts it into true The condition ! (A > B) is false The condition (A > B) is true but NOT operator converts it into false.

switch Statement

The switch statement is another conditional structure. It can be used easily when there are many choices available and only one should be executed.

Working of 'switch' Statement

switch statement compares the result of a 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 block of statements is executed. Any number of cases can be used in one switch statement.

The default label appears at the end of all case blocks It is executed only when the result of expression does not match with any case label. Its use is optional. The position of default label is not fixed. It may be placed before the first case statement 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 the expression matches with a case block, the corresponding statements are executed. The break statement comes after these statements and the control exits from switch body. If break is not used, all case blocks that come after the matching case, will also be executed.

Post a Comment

0 Comments