Look at this C program. What will it print when run?
#include <stdio.h> int main() { int temperature = 30; if (temperature > 25) { printf("It's hot today!\n"); } else { printf("It's cool today!\n"); } return 0; }
Check the value of temperature and the condition in the if statement.
The variable temperature is 30, which is greater than 25, so the if block runs and prints "It's hot today!".
Which of these best explains why conditional logic is important in programming?
Think about how a program can behave differently depending on what it sees.
Conditional logic lets a program decide what to do next based on conditions, like checking if a number is big or small.
Look at this code snippet. What error will it cause when compiled?
#include <stdio.h> int main() { int x = 10; if x > 5 { printf("x is greater than 5\n"); } return 0; }
Check the syntax of the if statement condition.
In C, the condition in an if statement must be inside parentheses. Missing them causes a syntax error.
What will this program print?
#include <stdio.h> int main() { int score = 75; if (score >= 90) { printf("Grade A\n"); } else if (score >= 70) { printf("Grade B\n"); } else { printf("Grade C\n"); } return 0; }
Check which condition matches the value of score.
The score is 75, which is not >= 90 but is >= 70, so it prints "Grade B".
Which statement best describes how conditional logic helps programs be flexible?
Think about how programs can change what they do based on what happens.
Conditional logic allows programs to choose different paths, making them adaptable to different inputs or conditions.