-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdowhile.html
18 lines (14 loc) · 1.5 KB
/
dowhile.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>For While and Do While</title>
<link href="/QuestionsPretty.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1>What is the difference between a ‘for’, ‘while’ and ‘do while’ loop? Give an example of when you would use one versus another. What is the benefit of a ‘do while’ over a ‘while’?</h1>
<p>The "for" loop allows us to have a variable change values through the program, as long as we know how many times we want the loop to go. This contrasts with while and do-while loops, since with those, the program runs as long as the conditions are true. </p>
<p>A "while" loop serves to run a program and check whether a condition is true or not. As long as the condition is true, the while loop will cycle back up and run again until it is no longer true. This is useful because it allows the user to create a loop that will run until the function is no longer true. As used in the Java the Hard Way text, it allows the user to create a password-request program.</p>
<p>Like the while loop, the "do-while" loop runs and checks whether a condition is true. While loops run before the body of the program, checking the conditions before the body is executed, while do-while loops run no matter what, and check the condition after the program section is executed. The benefit of a do-while loop over a while loop is that the variables don't have to be initialized before the code in order for the code to run. </p>
</body>
</html>