CS 121 Lecture Notes - Lecture 29: Infinite Loop

19 views4 pages

Document Summary

Allow us to execute a statement multiple times. Java has three kinds of repetition statements: while, do, for. The while statement: has the following syntax: while (condition) statement; If the condition is true, the statement is executed. If it is still true, the statement is executed again. The statement is executed repeatedly until the condition becomes false. Logic of a while loop: ex: int count = 1; while (count <= 5) Output is: 1 2 3 4 5: q: what if count=6 initially, a: answer would be false; the statement is never executed, therefore, the body of a while loop will execute zero or more times. Q: what if the condition of a while loop is always true? int count = 1; while (count >= 1) A: this is called an infinite loop, which will keep running until the user interrupts the program; this is a common logical error.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents