You are tasked with writing a program that outputs a customized message based on the time of day. Which code snippet correctly uses branching to output 'Good morning!' when the variable 'currentTime' is less than 12?
if (currentTime = 12) { print('Good morning!'); }
if (currentTime < 12) { print('Good morning!'); }
if (currentTime > 12) { print('Good morning!'); }
if (currentTime == 12) { print('Good morning!'); }
The correct snippet compares the variable 'currentTime' with the value 12 using the less-than (<) comparison operator inside an if statement. When the condition evaluates to true, the code in the block runs and prints "Good morning!". The snippet that uses the single equals sign (=) performs assignment rather than comparison; therefore, it does not reliably test the condition. The snippet that uses the double equals sign (==) checks only for equality with 12, not for values less than 12. The snippet that uses the greater-than (>) operator tests the wrong relationship, printing the message when the value is greater than 12 instead of less.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What is branching in programming?
Open an interactive chat with Bash
What is the difference between '=' and '==' in programming?