Site Search:

Create and use while loops

Back OCAJP



let's study while loop with an example:
A simple example

OCAJP>cat test.java 
class test{
  public static void main(String...args) {
    int m = 0, n = 4;
    while(m < (n = 3)) {
      m++;
      n--;
      System.out.println(m+","+n);
    }
  }
}
OCAJP>javac test.java 
OCAJP>java test
1,2
2,2
3,2

A mixed example

OCAJP>cat test.java 
class test{
  public static void main(String...args) {
    int i = 10;
    boolean b = false;
    while(b=-i++ < i -- ) { 
      i -= (i*=20%i++);
      System.out.print(i+":");  
      if(i%2==0) i--;i=(b=i<10)?i--:i; 
      System.out.println(i+",");
    }
    switch(0) {
    default: System.out.println(-0<0);
    case 10: i = 1; System.out.print(-i++ < i--); i = -1; System.out.print(-i++ < i--); i = 0; System.out.println(-i++ < i--);
    case 2: i = 5; System.out.println(i=i--);  //5
    case 3: i = 5; System.out.println(i - (i = 0));  //5
    case -4: i = 10; i = i - (i = i * (20%i++));  //i won't change if i is even
            System.out.println(i);
    case 5: i = 9; i = i - (i = i * (20%i++)); System.out.println(i); //i = 9 - (9 * 2) = -9 
    }
  }
}
OCAJP>javac test.java 
OCAJP>java test
10:9,
-9:-9,
false
truefalsetrue
5
5
10
-9
Back OCAJP