Created
April 3, 2016 22:26
-
-
Save bhnascar/d500123fd21d70766c1a0b0b6ae44586 to your computer and use it in GitHub Desktop.
For loop practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Practice | |
{ | |
public static void main(String[] args) { | |
// How can you replace the hard coded numbers | |
// with a variable? | |
System.out.println(1); | |
System.out.println(2); | |
System.out.println(3); | |
System.out.println(4); | |
System.out.println(5); | |
System.out.println(6); | |
System.out.println(7); | |
System.out.println(8); | |
System.out.println(9); | |
// How can you replace this code with a while loop? | |
int i = 1; | |
System.out.println(i); | |
i++; | |
System.out.println(i); | |
i++; | |
System.out.println(i); | |
i++; | |
System.out.println(i); | |
i++; | |
System.out.println(i); | |
i++; | |
System.out.println(i); | |
i++; | |
System.out.println(i); | |
i++; | |
System.out.println(i); | |
// Now suppose I want to go from 10-20 instead of 1-9. | |
// What do you need to change? | |
i = 1; | |
while(i < 9) { | |
System.out.println(i); | |
i++; | |
} | |
// How can you replace this code with a for loop? | |
int[][] arr = new int[3][3]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment