COMPTIA-SK0-005 practice question 29 of 35

Which of the following should a server administrator use when writing a script with a function that needs to be run ten times?

Full set: 493 questions
Answer options
A: Loop Correct answer
B: Variable
C: Comparator
D: Conditional
Correct Answer:
A. Loop
Explanation

A loop is a control flow statement used in scripting and programming to execute a block of code repeatedly. To perform an action a specific number of times, such as running a function ten times, a for loop is the ideal construct. The administrator can define the loop to iterate from one to ten, calling the function within the loop's body on each iteration. This automates the repetitive task efficiently and is a fundamental concept in scripting for server administration.

Why Incorrect

B. Variable: A variable is a named storage location for data; it does not, by itself, cause code to be executed repeatedly.
C. Comparator: A comparator (e.g., ==, >, <) is an operator used to compare two values, typically within a conditional or loop, but it is not the structure that performs the repetition.
D. Conditional: A conditional statement (e.g., if-else) executes a block of code based on whether a condition is true or false; it does not inherently repeat the execution. ---

References

1. Guttag, J. V. (2016). Lecture 2: Branching and Iteration. MIT OpenCourseWare, 6.0001 Introduction to Computer Science and Programming in Python. Retrieved from https://ocw.mit.edu/courses/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/resources/lecture-2-branching-and-iteration/. The lecture notes explicitly define iteration (loops) as a mechanism for repeated execution of a block of code.

2. Microsoft. (2023, November 13). aboutFor - PowerShell. Microsoft Learn. Retrieved from https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/aboutfor. The documentation states, "The For statement (also known as a For loop) is a language construct you can use to create a loop that runs commands in a command block while a specified condition evaluates to $true."

3. Free Software Foundation. (2022, December 29). Bash Reference Manual. Section 3.2.4.1 Looping Constructs. Retrieved from https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Looping-Constructs. This section describes the for loop, which is used to "execute a sequence of commands for each member in a list of items."

Scroll to Top