Skip to content

Instantly share code, notes, and snippets.

@bhnascar
Created April 3, 2016 22:26
Show Gist options
  • Save bhnascar/d500123fd21d70766c1a0b0b6ae44586 to your computer and use it in GitHub Desktop.
Save bhnascar/d500123fd21d70766c1a0b0b6ae44586 to your computer and use it in GitHub Desktop.
For loop practice
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