Day:10- JS Conditional Decisions!
Conditional statements allow your program to act like a decision tree.
Instead of running every line of code blindly from top to bottom,
our JavaScript code can ask questions, evaluate whether conditions
are true or false, and choose different execution paths.
Conditional Decision is a structure that controls code execution
based on specific logic checks.
Structuring Code Logic
- if (The Initial Check):
The mandatory starting point. Runs its code block only if its condition
evaluates to true.
- if-else statements: Execute different blocks of code
based on a condition.
- ternary operator: A concise way to write simple
if-else logic.
- else (The Final Fallback): The catch-all branch at the end.
It takes no condition and runs automatically only if every single
preceding check failed (false).
Summary
♦ Keyword: if
Purpose: Starts the decision chain
Requires Condition? Yes, if (condition)
When does it execute? When the condition is true.
♦ Keyword: else if
Purpose: Tests alternative conditions
Requires Condition? Yes, else if (condition)
When does it execute? When previous checks were
false AND its condition is true.