Conditional logic lets a program make decisions by choosing different actions based on conditions being true or false.
Click to reveal answer
beginner
Why do programs need conditional logic?
Programs need conditional logic to react differently to different situations, like a traffic light changing colors based on time or cars waiting.
Click to reveal answer
beginner
Example: What does this code do?
if (temperature > 30) { console.log('It is hot'); } else { console.log('It is not hot'); }
It checks if the temperature is above 30. If yes, it prints 'It is hot'. Otherwise, it prints 'It is not hot'.
Click to reveal answer
beginner
How is conditional logic like choosing clothes for the weather?
Just like you pick clothes based on weather (raincoat if rainy, sunglasses if sunny), programs use conditional logic to pick actions based on conditions.
Click to reveal answer
beginner
What happens if a program has no conditional logic?
Without conditional logic, a program would do the same thing every time, no matter what. It can't adapt or respond to different situations.
Click to reveal answer
What does conditional logic allow a program to do?
AMake decisions based on conditions
BRun faster
CStore data permanently
DCreate graphics
✗ Incorrect
Conditional logic lets programs choose actions depending on whether conditions are true or false.
Which real-life example is similar to conditional logic?
ASleeping without waking up
BEating the same meal every day
CChoosing clothes based on weather
DDriving without steering
✗ Incorrect
Choosing clothes based on weather is like conditional logic because you decide based on conditions.
What happens if a condition in an if statement is false?
AThe else block runs if it exists
BThe program crashes
CThe program stops immediately
DThe if block runs anyway
✗ Incorrect
If the condition is false, the else block runs if it is provided.
Why is conditional logic important in programs?
ATo make programs shorter
BTo allow different actions for different situations
CTo store numbers
DTo print text only
✗ Incorrect
Conditional logic lets programs respond differently depending on the situation.
Which keyword is used to check a condition in JavaScript?
Afunction
Bfor
Cwhile
Dif
✗ Incorrect
The 'if' keyword is used to check conditions in JavaScript.
Explain in your own words why conditional logic is needed in programming.
Think about how you decide what to do based on what is happening around you.
You got /3 concepts.
Give a simple real-life example that shows how conditional logic works.
Try to think about choosing clothes or crossing the street.
You got /3 concepts.
Practice
(1/5)
1. Why do we use conditional logic like if and else in JavaScript?
easy
A. To make decisions and run different code based on conditions
B. To repeat the same code many times
C. To store multiple values in a list
D. To create new variables
Solution
Step 1: Understand the purpose of conditional logic
Conditional logic allows the program to choose between different actions depending on conditions.
Step 2: Compare options with this purpose
Only To make decisions and run different code based on conditions describes making decisions based on conditions, which matches the purpose of if and else.
Final Answer:
To make decisions and run different code based on conditions -> Option A
Quick Check:
Conditional logic = decision making [OK]
Hint: Think: Does it help choose between actions? [OK]
Common Mistakes:
Confusing conditional logic with loops
Thinking it stores data
Believing it only creates variables
2. Which of the following is the correct syntax to check if a variable age is greater than 18 in JavaScript?
easy
A. if (age > 18) { }
B. if (age > 18) then { }
C. if age > 18 then { }
D. if age > 18 { }
Solution
Step 1: Recall JavaScript if statement syntax
JavaScript requires parentheses around the condition and curly braces for the code block: if (condition) { }.
Step 2: Check each option
if (age > 18) { } matches the correct syntax. Options A and C miss parentheses or use invalid keywords like 'then'. if (age > 18) then { } incorrectly uses 'then'.
Final Answer:
if (age > 18) { } -> Option A
Quick Check:
Correct if syntax = parentheses + braces [OK]
Hint: Remember: condition in ( ), code in { } [OK]
Common Mistakes:
Omitting parentheses around condition
Using 'then' keyword (not in JavaScript)
Missing curly braces
3. What will be the output of this code?
let score = 75;
if (score >= 60) {
console.log('Pass');
} else {
console.log('Fail');
}
medium
A. Fail
B. No output
C. Error
D. Pass
Solution
Step 1: Check the condition score >= 60
The variable score is 75, which is greater than or equal to 60, so the condition is true.
Step 2: Determine which block runs
Since the condition is true, the code inside the if block runs, printing 'Pass'.
Final Answer:
Pass -> Option D
Quick Check:
75 >= 60 is true, so output = Pass [OK]
Hint: Check if condition is true or false to pick output [OK]
Common Mistakes:
Confusing which block runs on true condition
Thinking output is 'Fail' for true condition
Assuming syntax error
4. Find the error in this code snippet:
let temperature = 30;
if temperature > 25 {
console.log('Hot');
} else {
console.log('Cold');
}
medium
A. Using else without if
B. Missing semicolon after variable declaration
C. Missing parentheses around the condition in if statement
D. Incorrect variable name
Solution
Step 1: Check the if statement syntax
JavaScript requires parentheses around the condition in an if statement, but here they are missing.
Step 2: Verify other parts
The variable declaration is correct with semicolon, else is correctly used, and variable name matches.
Final Answer:
Missing parentheses around the condition in if statement -> Option C
Quick Check:
if needs (condition) [OK]
Hint: Always put condition inside ( ) after if [OK]
Common Mistakes:
Forgetting parentheses in if condition
Thinking semicolon is mandatory after if
Confusing else usage
5. You want to write a program that prints 'Good morning' if the hour is less than 12, 'Good afternoon' if the hour is between 12 and 18 (inclusive), and 'Good evening' otherwise. Which code correctly uses conditional logic to do this?
hard
A. if (hour < 12) {
console.log('Good morning');
} else if (hour < 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
}
B. if (hour < 12) {
console.log('Good morning');
} else if (hour <= 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
}
C. if (hour <= 12) {
console.log('Good morning');
} else if (hour <= 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
}
D. if (hour < 12) {
console.log('Good morning');
} else if (hour > 12 && hour < 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
}
Solution
Step 1: Understand the conditions for each greeting
'Good morning' if hour < 12, 'Good afternoon' if hour between 12 and 18 inclusive, else 'Good evening'.
Step 2: Check each option's condition ranges
if (hour < 12) {
console.log('Good morning');
} else if (hour <= 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
} correctly uses hour < 12 for morning, hour <= 18 for afternoon (including 12 and 18), else evening. Others miss inclusive 12 or 18 or have wrong ranges.
Final Answer:
if (hour < 12) {
console.log('Good morning');
} else if (hour <= 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
} -> Option B
Quick Check:
Inclusive afternoon means <= 18, so if (hour < 12) {
console.log('Good morning');
} else if (hour <= 18) {
console.log('Good afternoon');
} else {
console.log('Good evening');
} fits [OK]
Hint: Check if conditions cover all cases without gaps [OK]
Common Mistakes:
Using < instead of <= for inclusive ranges
Overlapping or missing ranges
Wrong logical operators in conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.