Lec04 - CS1101Y: Programming Methodology Control...

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

View Full Document Right Arrow Icon
Control Statements. Loops Lecture 4 — September 1, 2008 CS1101Y: Programming Methodology
Background image of page 1

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

View Full DocumentRight Arrow Icon
September 1, 2008 CS1101Y--group2, Lecture 4 2 Outline while Loop do Loop for Logical Operator boolean Variables • Testing and Debugging
Background image of page 2
September 1, 2008 CS1101Y--group2, Lecture 4 3 while Loop pseudocode syntax while <condition> <statement(s)> Java syntax while ( <condition> ) { <statement(s)> } ± Use a loop statement if you need to do the same thing repeatedly.
Background image of page 3

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

View Full DocumentRight Arrow Icon
September 1, 2008 CS1101Y--group2, Lecture 4 4 while Loop •W r i t e a main method that finds the sum of user-entered integers where -99999 is a sentinel value. public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); int sum = 0; // sum of user-entered values int x; // a user-entered value System.out.print("Enter an integer (-99999 to quit): "); x = stdIn.nextInt(); while (x != -99999) { sum = sum + x; System.out.print("Enter an integer (-99999 to quit): "); x = stdIn.nextInt(); } System.out.println("The sum is " + sum); } // end main
Background image of page 4
September 1, 2008 CS1101Y--group2, Lecture 4 5 do Loop When to use a do loop: – If you know that the repeated thing will always have to be done at least one time. Syntax: do { <statement(s)> } while ( <condition> ); Note: – The condition is at the bottom of the loop (in contrast to the while loop, where the condition is at the top of the loop). – The compiler requires putting a ";" after the ")" . – Proper style dictates putting the "while" part on the same line as the "}"
Background image of page 5

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

View Full DocumentRight Arrow Icon
September 1, 2008 CS1101Y--group2, Lecture 4 6 do Loop do loop example: – As part of an architectural design program, write a main method that prompts the user to enter length and width dimensions for each room in a proposed house so that total floor space can be calculated for the entire house. After each length/width entry, ask the user if there are any more rooms. Print the total floor space.
Background image of page 6
September 1, 2008 CS1101Y--group2, Lecture 4 7 for Loop • When to use a for loop: – If you know the exact number of loop iterations before the loop begins. • For example, use a for loop to: – Print this countdown from 10. Sample session : 10 9 8 7 6 5 4 3 2 1 Liftoff! – Find the factorial of a user-entered number . Sample session : Enter a whole number: 4 4! = 24
Background image of page 7

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

View Full DocumentRight Arrow Icon
September 1, 2008 CS1101Y--group2, Lecture 4 8 for Loop for loop syntax for ( <initialization> ; <condition> ; <update> ) { <statement(s)> } for loop example for (int i=10; i>0; i--) { System.out.print(i + " "); } System.out.println(“Liftoff!”); ± for loop semantics: ± Before the start of the first loop iteration, execute the initialization component. ± At the top of each loop iteration, evaluate the condition component: ± If the condition is true, execute the body of the loop. ± If the condition is false, terminate the loop (jump to the statement below the loop's closing brace). ± At the bottom of each loop iteration, execute the update component and then jump to the top of the loop.
Background image of page 8
September 1, 2008 CS1101Y--group2, Lecture 4 9 for Loop • Trace this code fragment with an input value of 3.
Background image of page 9

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

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

This note was uploaded on 03/19/2012 for the course SOC 1101 taught by Professor A.tan during the Fall '08 term at National University of Singapore.

Page1 / 39

Lec04 - CS1101Y: Programming Methodology Control...

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

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