Conditionals and If Statements — Intro to Programming 练习题
A conditional lets a program make decisions. An if statement runs a block of code only when its condition is True; otherwise the block is skipped. This is how programs react differently to different inputs.
Conditions are Boolean expressions — they evaluate to True or False. Comparison operators produce Booleans: == (equal), != (not equal), < (less than), > (greater than), <= (less than or equal), >= (greater than or equal). Remember that == compares values while = assigns a value.
Boolean operators combine conditions: and is True only when both sides are True, or is True when at least one side is True, and not flips True to False. Parentheses can clarify the order of evaluation.
An if/elif/else chain checks conditions from top to bottom and runs only the first block whose condition is True. The else block is the catch-all that runs when no earlier condition matched. In Python, indentation defines which lines belong to each block.
练习的技能
- Writing if, elif, and else statements
- Using comparison operators correctly
- Combining conditions with and, or, not
- Tracing code output through branches
- Distinguishing = (assignment) from == (comparison)
练习题:Conditionals and If Statements
说明: 仔细解答每一道题。清晰地展示所有解题过程。在指定位置或按指示在另一张纸上写出最终答案。
-
1.What is the purpose of an if statement in a program?
- A. To repeat a block of code multiple times
- B. To make a decision and execute code only when a condition is true
- C. To store multiple values in a single variable
- D. To display output to the user
Conditionals and If Statements — 练习题(续)
-
2.In the following code snippet, what will be printed to the screen? x = 10 if x > 5: print("Large") else: print("Small")
Conditionals and If Statements — 练习题(续)
-
3.Write an if statement that checks if a variable called age is greater than or equal to 18. If it is, print "Adult". Otherwise, print "Minor".
Conditionals and If Statements — 练习题(续)
-
4.Which of the following is the correct syntax for an if-else statement in Python?
- A. if condition { } else { }
- B. if condition: // code else: // code
- C. if (condition) then { } else { }
- D. if condition then code else code
Conditionals and If Statements — 练习题(续)
-
5.What is the output of the following code? num = 7 if num print("Even") else: print("Odd")
Conditionals and If Statements — 练习题(续)
-
6.Write a program segment that assigns the string "Positive" to a variable called result if a number stored in n is greater than 0. If n is less than 0, assign "Negative". If n equals 0, assign "Zero".
Conditionals and If Statements — 练习题(续)
-
7.What will the following code print? score = 75 if score >= 90: print("A") elif score >= 80: print("B") elif score >= 70: print("C") else: print("F")
- A. A
- B. B
- C. C
- D. F
Conditionals and If Statements — 练习题(续)
-
8.Explain the difference between if and elif in a conditional statement.
-
9.Write a conditional statement that checks if a variable temp is greater than 100. If it is, print "Hot". If temp is between 50 and 100 (inclusive), print "Warm". Otherwise, print "Cold".
Conditionals and If Statements — 练习题(续)
-
10.Which of the following conditions checks if x is NOT equal to 5?
- A. if x == 5:
- B. if x != 5:
- C. if x <> 5:
- D. if x =! 5:
参考答案
-
1.
An if statement makes a decision and executes code only when a condition is true.最终答案: B
-
2.
Since x = 10 and 10 > 5 is true, the if branch executes and prints "Large".最终答案: Large
-
3.
最终答案: if age >= 18: print("Adult") else: print("Minor")
-
4.
In Python, an if statement uses a colon after the condition and indented code blocks.最终答案: B
-
5.
Since 7最终答案: Odd
参考答案(续)
-
6.
最终答案: if n > 0: result = "Positive" elif n < 0: result = "Negative" else: result = "Zero"
-
7.
Since 75 ≥ 70 is true and the earlier conditions (≥ 90, ≥ 80) are false, the elif score >= 70 branch executes and prints "C".最终答案: C
-
8.
最终答案: if checks a condition and executes its block if true. elif (short for "else if") is used after an if to check an additional condition only if the previous conditions were false. This allows multiple mutually exclusive conditions to be checked in sequence.
参考答案(续)
-
9.
最终答案: if temp > 100: print("Hot") elif temp >= 50: print("Warm") else: print("Cold")
-
10.
The != operator checks if two values are not equal in Python.最终答案: B
需要避免的常见错误
- Using = instead of == in a condition
- Forgetting the colon after if, elif, or else
- Incorrect indentation of the block body
- Assuming every elif runs instead of only the first True branch
- Mixing up and with or when combining conditions
生成您自己的练习表
想要一套难度自选的 conditionals and if statements 新题?使用 WorksheetSmith 在几秒内编译一份可打印的自定义 PDF。
最后更新:2026