Foundations-of-Computer-Science practice question 10 of 15

How does an if statement help control the flow of a program?

Full set: 71 questions
Answer options
A:

A. It repeats code forever unless manually stopped

B:

B. It decides whether a block of code runs based on a condition

Correct answer
C:

C. It stores values permanently in memory

D:

D. It converts text into machine code

Correct Answer:
B. B. It decides whether a block of code runs based on a condition
Explanation

An if statement is a fundamental conditional control structure in programming. It evaluates a Boolean expression (a condition). If the condition is true, a specific block of code is executed. If the condition is false, that block is skipped, and the program's control flow proceeds to the next statement. This allows a program to make decisions and execute different code paths based on varying inputs or states, which is a core concept of controlling the flow of program execution.

Why Incorrect

A. This describes an infinite loop, a repetitive control structure, not a conditional one like an if statement.
C. Storing values is the function of variables and data structures, not control flow statements.
D. This is the function of a compiler or interpreter, which translates source code into machine-executable code.

References

1. Downey, A. B. (2015). Think Python: How to Think Like a Computer Scientist (2nd ed.). O'Reilly Media. Chapter 5, "Conditionals and Recursion," Section 5.1.

2. Guttag, J. V. (2016). Introduction to Computation and Programming Using Python (2nd ed.). MIT Press. Chapter 3, "Some Simple Numerical Programs," Section 3.1 Branching Programs.

3. MIT OpenCourseWare. (2016). 6.0001 Introduction to Computer Science and Programming in Python, Fall 2016. Lecture 2: Branching and Iteration.

Scroll to Top