0% found this document useful (0 votes)
2 views2 pages

Key While Loop

Uploaded by

haemin.dfh2026
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Key While Loop

Uploaded by

haemin.dfh2026
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Here are the answers to the multiple choice questions about While loops in VBA

Word:

1. Answer: (b) A For...Next loop repeats a specific number of times, while a


While loop repeats based on a condition. While loops are useful when you
don't know beforehand how many times you need to repeat a block of code.
2. Answer: (c) A predefined condition that must be true. The While loop
continues to execute the code block as long as the specified condition
evaluates to True. Once the condition becomes False, the loop terminates.
3. Answer: (d) Modify the condition within the loop to eventually become false.
An infinite loop occurs when the condition in the While statement never
becomes False. Make sure the code within the loop modifies the condition or
introduces another factor that will eventually cause it to evaluate to False.
4. Answer: (b) Repeating a task until a user enters a specific value. While loops
are ideal for scenarios where the number of iterations depends on an external
factor, like user input or a changing variable.
5. Answer: (b) They offer more flexibility for handling unknown iteration
counts. While loops shine when you need to repeat a code block an unknown
number of times based on a condition. They provide more flexibility compared
to For...Next loops which require a predetermined number of iterations.

The answers to the additional MCQ questions about While loops are:

1. Answer: (c) The loop might run infinitely. If the condition in the While loop
never becomes False, the loop will continue to repeat indefinitely, leading to
an infinite loop.
2. Answer: (c) ```vba While True ' This is generally not recommended ' Code to
be executed Exit While ' Use Exit While to terminate Wend

While technically valid, using While True creates a loop that continues forever
unless terminated with Exit While within the loop. This can be error-prone if not
handled carefully. It's generally better to write a clear condition that will eventually
become False.

3. Answer: (a) The collection size remains constant within the loop. When
processing elements in a collection with a While loop, it's crucial to ensure the
collection size doesn't change within the loop itself. Modifying the collection
while iterating through it can lead to unexpected behavior or errors.

Here are the answers to the multiple choice questions about While loops:

1. b) The code inside the loop executes repeatedly as long as a condition is


true.
2. b) The loop will execute an infinite number of times (infinite loop).
3. b) The condition must eventually evaluate to false.
4. d) x = x + 1 (assignment statement, not a condition)
5. c) It marks the end of the loop.
6. b) When the number of iterations depends on a condition.
7. c) 5 times (The loop decrements the number variable until it reaches 0)
8. b) Use a Break statement within the loop body.
9. c) Not initializing variables used in the loop condition. (Uninitialised
variables can lead to unexpected behavior)
10. a) True (While loops can be nested with other control flow statements for
complex logic)

Here are the answers to the advanced While loop questions:

1. b) 1

The loop keeps dividing even numbers by 2 and multiplying odd numbers by 3 and
adding 1. Eventually, it will reach an odd number (1 in this case) that cannot be
further modified and the loop terminates.

2. b) Keep track of visited nodes using a separate set.

By storing visited nodes in a set, the loop can check if it's encountering the same node
again, indicating a circular list and allowing for loop termination.

3. a) Add a special case outside the loop to handle 0.

Factorial of 0 is a special case defined as 1. The loop calculates factorials for positive
numbers, so a separate check outside the loop is needed to handle 0.

4. a) Print the value of the loop condition after each iteration.

Printing the loop condition after each iteration helps visualize how the condition
changes and identify if it's ever getting stuck or not updating as intended, potentially
leading to an infinite loop.

5. a) The loop iterates through the list, swapping adjacent elements if


they're in the wrong order.

Bubble sort uses nested loops. The outer loop controls the number of passes, while the
inner loop (often implemented with a While loop) iterates through the list. An If
statement within the inner loop checks if adjacent elements are in the wrong order and
swaps them if necessary. This process continues until the entire list is sorted.

You might also like