CompTIA Tech+ (FC0-U71)Software Development ConceptsEasy
A developer is creating a program where specific blocks of code need to be executed only if certain conditions are met. For example, a discount is applied only if the total purchase is over $100. Which type of control structure is used for this conditional execution?
- AFunction
- BLoop
- CVariable
- DBranching
Show answer & explanationAnswer & explanation
Correct answer: D. Branching
Branching (also known as conditional statements like if-else or switch-case) allows the program to choose different paths of execution based on whether a specified condition evaluates to true or false. This directly addresses the need for 'code to be executed only if certain conditions are met'.
Why the other options are wrong
- A. A function is a reusable block of code, but doesn't inherently handle conditional execution paths.
- B. A loop is for repeated execution, not conditional execution.
- C. A variable stores data, it's not a control structure.
Branching (Conditional Statements)
Branching, or conditional statements, are control structures that allow a program to execute different blocks of code based on whether specific conditions are true or false. Common examples include if-else statements and switch-case statements.
- Alters program flow based on conditions
- Uses keywords like 'if', 'else if', 'else', 'switch', 'case'
- Essential for decision-making in programs
Memory trick: Conditions met? Branch off and go!