2. Conditional Statements
Decision Making
Conditional statements are used to perform different
actions based on different conditions.
The if statement is one of the most frequently used
conditional statements.
3. If the if statement’s condition expression
evaluates to true, the block of code inside the if
statement is executed. If the expression is found
to be false, the code within the if statement is
ignored and the first set of code after the end of
the if statement (after the closing curly brace) is
executed.
6. Any of the following comparison operators may be
used to form the condition:
• Left Bracket (<) - less than
• Right Bracket (>) - greater than
• Exclamation Mark and Equal Sign (!=) - not equal to
• Double Equal Sign (==) - equal to
• Left Bracket and Equal Sign (<=) - less than or equal to
• Right Bracket and Equal Sign (>=) - greater than or
equal to
14. else if Statements
The Math operators
• Instead of using nested if-else statements, you can use the else if
statement to check multiple conditions.
• For example:
19. The OR Operator
• The OR operator, denoted by two (2) vertical bars ( ||),
checks if any one of the conditions is true.
• The condition becomes true, if any one of the operands
evaluates to true.
21. The NOT Operator
• The NOT logical operator, denoted by a single
exclamation point (!), is used to reverse the logical
state of its operand. If a condition is true, the NOT
logical operator will make it false.
23. The switch Statement
A switch statement tests a variable for equality
against a list of values. Each value is called a
case, and the variable being switched on is
checked for each case.
25. When the variable being switched on is equal to a
case, the statements following that case will
execute unit a break statement is reached.
When a break statement is reached, the switch
terminates, and the flow of control jumps to the
next line after the switch statement.
26. Not every case needs to contain a break. If no
break appears, the flow of control will fall
through to subsequent cases until a break is
reached.
The example tests day against a set of values
and prints a corresponding message:
27. The default Statement
A switch statement can have an option default
case.
The default case can be used for performing a
task when none of the cases is matched.