Every programming language has features that allow the programmer to control the execution of programs. These statements are known as control statements. Using control statements, you can execute or ignore a set of statements. Similarly you can execute a set of statements repeatedly.
- Conditional Statements.
- Looping statements.
We can discuss the Looping statement in next article. In this article, we can discuss only Conditional Statements in very detail.
The statements that are used to execute or ignore a statement or a set of statement of the program on the basis of some condition is known as conditional statements or decision-making statements or selection statements. A condition must be give with conditional statement. The computer tests the given condition, if it is true, the set of statements under the selection statement is executed otherwise it is ignored.
- If selection structure.
- If-else selection structure.
- Switch selection structure.
|==||Equal to||Returns true if both values are equal; otherwise return false.||A==B returns false|
|===||Exactly equal to||Returns true if both values are exactly equal with respect to value and type.||B===5 is true while
B===”5” is false
|!=||Not Equal to||Returns true if both values are different; otherwise returns false.||A!=B returns true|
|>||Greater than||Returns true if first value is greater than second; otherwise returns false||A>B returns true|
|<||Less than||Returns true if first value is less than second; otherwise returns false.||A<B returns false|
|>=||Greater than or equal to||Returns true if first value is greater than or equal to second; otherwise returns false.||A>= C returns true|
|<=||Less than or equal to||Returns true if first value is less than or equal to second; otherwise returns false||A<=C returns true|
For example, a>b is a relational expression, it returns true if the value of ‘a’ is greater than ‘b’, otherwise it returns false.
Suppose a=6, b=9 and c=6, then the relational expressions and their output values are shown in the following table:
|a > c||False|
|a >= c||True|
|a < b||True|
|a <= c||True|
|A >= c||True|
|a < b||True|
|a <= c||True|
|a == b||False|
|a === 6||True|
|a === ”6”||False|
|A != c||False|
It is used to combine two relational expressions and returns true if both expression are true. In this case, if first expression is false, the evaluation process is stopped at that point and second expression is not evaluated.
It is used to combine two relational expressions and returns true of either of the expressions is true. In this case, if fires expression is false, the evaluation process will continue and second expression is evaluated.
It is used to reverse the result of the relational expression or compound expression.
- a && b
- c || d
- ! (a < b)
For example, if a=10, b=5 and c=10, the table below shows the output of the compound expressions as:
|(a > b ) && (a > c)||False|
|(a>b) || (b=c)||True|
If ‘total’ , ‘math’, ‘eng’ and ‘computer’ are four integer variables. The variable ‘total’ store the sum of the values of variable ‘math’, ‘eng’ and ‘computer’.
- To find out if the value in variable ‘total’ is greater than or equal to 700 and the value in variable ‘computer’ is greater than 90, the compound expression is written as:
Total >=700 && computer >90
- To find out if value of ‘total’ is greater than 600 or value in ‘eng’ is greater than 70, the compound expression is written as:
Total > 600 || eng > 70
- To find out if value in ‘computer’ variable is not greater than 33, the expression is:
!(computer > 33)
(condition) ? exp1 : exp2
Represents the condition. It may be a relational or logical expression. if it is true then computer will take action on “exp1 and “exp2” is ignored. It condition is false, the computer will take action on exp2 and exp1 is ignored.
It may be noted that the conditional operator takes three operands (i.e. condition, exp1 and exp2). It is a ternary operator.