Chapter IV Part II

Chapter IV Part II - Loops, Part II IT108 George Mason...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Loops, Part II IT108 George Mason University Indefinite Loop Dont always have access to the number of iterations ahead of time If a condition (user-response, limit reached, ) controls the number of iterations indefinite loop is appropriate While Loop Using the while Loop while Loop- To execute a body of statements continually as long as the Boolean expression continues to be true Consists of the keyword while followed by a Boolean expression within parentheses followed by the body of the loop Use when you need to perform a task a undetermined number of times while Loop while (loop-continuation-condition) { // loop-body; Statement(s); } int count = 0; while (count < 100) { System.out.println("Welcome to Java!"); count++; } Loop Continuation Condition? true Statement(s) (loop body) false (count < 100)? true System.out.println("Welcome to Java!"); count++; false ( A ) ( B ) count = 0; Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Initialize count int count MEMORY CONSOLE Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } (count < 2) is true int count MEMORY CONSOLE Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print Welcome to Java int count MEMORY CONSOLE Welcome to Java! Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 count is 1 now int count MEMORY CONSOLE Welcome to Java! 1 Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } (count < 2) is still true since count is 1 int count MEMORY CONSOLE Welcome to Java! 1 Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Print Welcome to Java int count MEMORY CONSOLE Welcome to Java! 1 Welcome to Java! Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Increase count by 1 count is 2 now int count MEMORY CONSOLE 1 Welcome to Java! Welcome to Java! 2 Trace while Loop, cont. int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } (count < 2) is false since count is 2 now int count MEMORY CONSOLE Welcome to Java! Welcome to Java! 2 Trace while Loop int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } The loop exits. Execute the next statement after the loop. int count MEMORY CONSOLE Welcome to Java! Welcome to Java! 2 Example: Sum of integers Write a program that reads and calculates the sum of an unspecified number of integers. The input 0 signifies the end of the input....
View Full Document

Page1 / 50

Chapter IV Part II - Loops, Part II IT108 George Mason...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online