Lesson 2: Conditional Expressions and Loops
2.1 Conditional Statements (if, else, elseif)
Conditional expressions allow you to run different blocks of code depending on whether a certain condition is true or false. Here is an example:
This program checks whether the variable $not is 50 or greater and prints the message "You passed!" or "You failed." depending on the case.
2.2 Loops (for, while, do-while)
Loops are used to repeat a certain block of code as long as a certain condition is met.
2.2.1
for
Loop
2.2.2
while
Loop
2.2.3 do-while
Loop
2.3 Logical Operators
Logical operators help you combine conditions. Commonly used ones are:
- && (AND): If both conditions are true.
- || (OR): If at least one of the conditions is true.
- ! (NOT): Takes the opposite of the condition.