0% found this document useful (0 votes)
14 views1 page

A. What Is A While Loop A While Loop Iterates Over A Code Block If A Given - 20241029 - 190047 - 0000

Uploaded by

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

A. What Is A While Loop A While Loop Iterates Over A Code Block If A Given - 20241029 - 190047 - 0000

Uploaded by

falosantiago5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Santiago Apollo Daniell B

BSIS 104
Answer the following questions:

Define and Differentiate:


a. What is a While Loop?
b. What is a Do...While Loop?
c. What is a For Loop?
d. Compare and contrast these three types of loops and explain when it's best to use each type.

a. What is a While Loop?


A while loop iterates over a code block if a given condition is true each time. It checks the
condition before each repetition; if it is false, the loop stops and does not execute the code. This
kind of loop is extremely beneficial when you don't know how many occurrences the loop should
run, yet you have the important condition that specifies the running for loop.

b. What is a Do...While Loop?


A do...while loop is in most aspects similar to a while loop, except for the fact that it always runs
the block of code at least once before checking the condition. Following the first run, the
condition is verified; if it is true, the loop goes on, otherwise, it is stopped. It is a good idea to use
it in the situation where you want the loop to be executed at least once regardless if the
condition is met, for example, when you ask the user for input at least once and then you validate
it.

c. What is a For Loop?


Generally, the for loop is used when the number of iterations can be predicted in advance. It
does allow you to define an initializing, a condition, and an increment (or decrement) in the same
line, making it specially competent in the context of iterating through lists of specific numbers or
numbering a range of cases.

d. When to Use Each and Their Differences While Loop is good when you want to loop until a
condition is no longer true, like reading data from a file until you reach the end. It checks the
condition before each loop so it may not run at all if the condition is false to begin with.
Do...While Loop is good when you want the code to run at least once before checking the
condition. For example if you need to show a prompt for user input and then validate it, the
do...while loop will make sure the prompt is shown at least once. For Loop is used when you know
the exact number of iterations. For example it’s great for iterating over elements in an array or
doing something a specific number of times. It simplifies loops with a known range, as all parts of
the loop (initialization, condition and increment) are in one place.

You might also like