Conditional Statements (if, else if, else, switch)
Appearance
3.3.1 Conditional Statements (if, else if, else, switch)
Imagine you're at a crossroads, and you need to decide which way to go. Your decision depends on a condition, like "If the sign says 'Beach', go left. Otherwise, go straight."
Conditional Statements are programming instructions that allow your program to make decisions based on whether a certain condition is true or false. They let your code choose different paths of execution.
Here are the main types of conditional statements:
if
statement:- This is the simplest decision-maker. It says: "IF a condition is true, THEN do this specific action."
- If the condition is false, the action is skipped.
- Example: