CS Fundamentals

Module: Logic Flow & Decision Making

1. Review: The Function Machine

Before we dive into conditions, recall that a Function is like a machine. It takes an input (parameter), processes it, and returns an output.

Try the "Doubler" Function

function doubleValue(x) { return x * 2; }

Waiting for input...

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.

2. Types of Conditions

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.

Compound Conditions

Sometimes one check isn't enough. We use logical operators to combine conditions:

3. Application: The Logic Gatekeeper

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.

Incoming Packet

ADMIN Lvl 5

Select the correct logic:

Goal: Allow access ONLY if the user is an 'ADMIN' AND their level is greater than 3.