Module: Logic Flow & Decision Making
Before we dive into conditions, recall that a Function is like a machine. It takes an input (parameter), processes it, and returns an output.
function doubleValue(x) { return x * 2; }
But what if the machine needs to make a choice? What if it should double small numbers but triple big ones? That is where Conditions come in.
Conditions allow our code to branch. They rely on Boolean Logic (True or False). Here are the three primary structures used in programming:
| Type | Syntax Structure | Real World Analogy |
|---|---|---|
| Simple If | if (condition) { action } |
If it is raining, take an umbrella. |
| If / Else | if (A) { ... } else { ... } |
If the light is green, go. Otherwise, stop. |
| Chained (Else If) | if (A) {} else if (B) {} else {} |
If size is S, cost is $5. If M, cost is $10. Else, cost is $15. |
Sometimes one check isn't enough. We use logical operators to combine conditions:
&&): Both sides must be true. (e.g., hasTicket && gateIsOpen)||): Only one side needs to be true. (e.g., hasCash || hasCreditCard)Mission: You are programming a security bot. Analyze the incoming "Data Packet" on the left. Select the correct code block on the right that will allow the packet to pass based on its attributes.
Goal: Allow access ONLY if the user is an 'ADMIN' AND their level is greater than 3.