Download presentation
Presentation is loading. Please wait.
Published bySamuel Fields Modified over 9 years ago
1
Intro to Java Day 3 / Loops 1
2
DAY 3 Java Language Specifications Conditional Loops while do for References: JavaNotes7 pdf bookJavaNotes7 pdf book
3
WHILE STATEMENT 3 … while ( ) … The is called the “Continuation Condition” … while ( ) … The is called the “Continuation Condition”
4
WHILE EXAMPLE Java Code: … int i; i = 0; while ( i < 10 ) { System.out.println(i); i = i + 1; } … Java Code: … int i; i = 0; while ( i < 10 ) { System.out.println(i); i = i + 1; } … 4 Output: 0 1 2 3 4 5 6 7 8 9 Output: 0 1 2 3 4 5 6 7 8 9
5
WHILE LOOP NESTING Java Code: … int i, j; i = 0; j = 0; while ( i < 10 ) { j = 0; System.out.printf("i=%d:", i ); while ( j < 2 ) { System.out.printf(" j=%d", j); j = j + 1; } System.out.printf(".\n"); i = i + 1; } … Java Code: … int i, j; i = 0; j = 0; while ( i < 10 ) { j = 0; System.out.printf("i=%d:", i ); while ( j < 2 ) { System.out.printf(" j=%d", j); j = j + 1; } System.out.printf(".\n"); i = i + 1; } … 5 Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1. Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1.
6
DO WHILE STATEMENT 6 … do { } while ( condition ); … int i = 0; do { i = i + 1; } while ( i < 10 ); // What is the value of i … do { } while ( condition ); … int i = 0; do { i = i + 1; } while ( i < 10 ); // What is the value of i …
7
DO WHILE LOOP NESTING Java Code: … int i, j; i = 0; j = 0; do { j = 0; System.out.printf("i=%d:", i); do { System.out.printf(" j=%d", j); j = j + 1; } while (j < 2); System.out.printf(".\n"); i = i + 1; } while (i < 10); … Java Code: … int i, j; i = 0; j = 0; do { j = 0; System.out.printf("i=%d:", i); do { System.out.printf(" j=%d", j); j = j + 1; } while (j < 2); System.out.printf(".\n"); i = i + 1; } while (i < 10); … 7 Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1. Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1.
8
FOR STATEMENT 8 … for ( ; ; ){ }; … int myvar = 10; for (int i=0; i<10; i++) { myvar = myvar + i; } // what is the value of myvar ? … for ( ; ; ){ }; … int myvar = 10; for (int i=0; i<10; i++) { myvar = myvar + i; } // what is the value of myvar ?
9
FOR LOOP NESTING #1 Java Code: … for ( int i=0; i<10; i++ ) { System.out.printf("i=%d:", i); for ( int j=0; j<2; j++ ) { System.out.printf(" j=%d", j); } System.out.printf(".\n"); } … Java Code: … for ( int i=0; i<10; i++ ) { System.out.printf("i=%d:", i); for ( int j=0; j<2; j++ ) { System.out.printf(" j=%d", j); } System.out.printf(".\n"); } … 9 Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1. Output: i=0: j=0 j=1. i=1: j=0 j=1. i=2: j=0 j=1. i=3: j=0 j=1. i=4: j=0 j=1. i=5: j=0 j=1. i=6: j=0 j=1. i=7: j=0 j=1. i=8: j=0 j=1. i=9: j=0 j=1.
10
FOR LOOP NESTING #2 Java Code: … for ( int i=0; i<10; i++ ) { System.out.printf("i=%d:", i); for ( int j=3; j>0; j-- ) { System.out.printf(" j=%d", j); } System.out.printf(".\n"); } … Java Code: … for ( int i=0; i<10; i++ ) { System.out.printf("i=%d:", i); for ( int j=3; j>0; j-- ) { System.out.printf(" j=%d", j); } System.out.printf(".\n"); } … 10 Output: i=0: j=3 j=2 j=1. i=1: j=3 j=2 j=1. i=2: j=3 j=2 j=1. i=3: j=3 j=2 j=1. i=4: j=3 j=2 j=1. i=5: j=3 j=2 j=1. i=6: j=3 j=2 j=1. i=7: j=3 j=2 j=1. i=8: j=3 j=2 j=1. i=9: j=3 j=2 j=1. Output: i=0: j=3 j=2 j=1. i=1: j=3 j=2 j=1. i=2: j=3 j=2 j=1. i=3: j=3 j=2 j=1. i=4: j=3 j=2 j=1. i=5: j=3 j=2 j=1. i=6: j=3 j=2 j=1. i=7: j=3 j=2 j=1. i=8: j=3 j=2 j=1. i=9: j=3 j=2 j=1.
11
ASSIGNMENT Create 1 or 2 programs using while and for loop, that print only the odd numbers between 0 and 100. example: 1 3 5 7 9 … 99 Advanced: Print an asterisk “*”, next to the numbers that are divisible by 5 example: 1 3 5* 7 9 … 99
12
LINKS & REFERENCES Intro to Java Language Web Page http://jimcarter.us/intro-to-java-language/ http://jimcarter.us/intro-to-java-language/ Oracle Java Language Specifications http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf JavaNotes-7 pdf book http://math.hws.edu/eck/cs124/downloads/javanotes7- linked.pdf http://math.hws.edu/eck/cs124/downloads/javanotes7- linked.pdf 12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.