Saturday, October 29, 2011

Do While Loop

 The do-while Statement

The do-while statement is like the while statement, except that the associated block always gets
executed at least onc Its syntax is as follows:

do {
    statement (s)
while (booleanExpression);
Just like the while statement, you can omit the braces if there is only one statement within them.
However, always use braces for the sake of clarity.

public class MainClass {

  public static void main(String[] args) {
    int i = 0;
    do {
      System.out.println(i);
      i++;
    while (i < 3);
  }

}
This prints the following to the console:
0
1
2
 
 
The following do-while demonstrates that at least the code in the do block will be executed once
even though the initial value of j used to test the expression j < 3 evaluates to false.
public class MainClass {

  public static void main(String[] args) {
    int j = 4;
    do {
        System.out.println(j);
        j++;
    while (j < 3);
  }

}
This prints the following on the console.
4
 
 
The do while loop in action 
do {
  // statements
while (expression);
public class MainClass {
  public static void main(String[] args) {
    int limit = 20;
    int sum = 0;
    int i = 1;

    do {
      sum += i;
      i++;
    while (i <= limit);

    System.out.println("sum = " + sum);
  }
}
sum = 210
 
 
http://www.java2s.com/Tutorial/Java/
0080__Statement-Control/Catalog0080__Statement-Control.htm 
 
»»  READMORE...

Saturday, October 22, 2011

While Loop

The while Statement

  1. One way to create a loop is by using the while statement.
  2. Another way is to use the for statement
The while statement has the following syntax.
while (booleanExpression) {
    statement (s)
}
  1. statement(s) will be executed as long as booleanExpression evaluates to true.
  2. If there is only a single statement inside the braces, you may omit the braces.
  3. For clarity, you should always use braces even when there is only one statement.

As an example of the while statement, the following code prints integer numbers that are
less than three.

public class MainClass {

  public static void main(String[] args) {
    int i = 0;
    while (i < 3) {
      System.out.println(i);
      i++;
    }
  }

}
To produce three beeps with an interval of 500 milliseconds, use this code:
public class MainClass {

  public static void main(String[] args) {
    int j = 0;
    while (j < 3) {
      java.awt.Toolkit.getDefaultToolkit().beep();
      try {
        Thread.currentThread().sleep(500);
      catch (Exception e) {
      }
      j++;
    }
  }

}

Sometimes, you use an expression that always evaluates to true
(such as the boolean literal true) but relies on the break statement to escape from the loop.

public class MainClass {

  public static void main(String[] args) {
    int k = 0;
    while (true) {
      System.out.println(k);
      k++;
      if (k > 2) {
        break;
      }
    }
  }

}
 
 
Using the while loop to calculate sum


public class MainClass {
  public static void main(String[] args) {
    int limit = 20;
    int sum = 0;
    int i = 1;

    while (i <= limit) {
      sum += i++;
    }
    System.out.println("sum = " + sum);
  }
}
 
Compile
sum = 210

While loop with double value
 
public class MainClass {
  public static void main(String[] args) {
    double r = 0;
    while(r < 0.99d) {
      r = Math.random();
      System.out.println(r);
    }
  }
}
 
Compile
0.4377278997305387
0.2688654455422754
0.36392537297385574
0.15254413511361042
0.6688621030424611
0.143156733550304
0.3867695752401421
0.6348496031126075
0.8262243996358971
0.057290108917235516
0.48887104413122007
0.9181002018325309
0.03580026338719999
0.6915245271034702
0.06281957479185263
0.7484170566879976
0.08309381253696246
0.7708638307798187
0.6586973690207539
0.3606321940413979
0.17404632324149583
0.1007512703731156
0.7971297767971545
0.4461084890266951
0.8627269068871244
0.04984737498714209
0.2597921665157922
0.5410470601800582
0.5779541886682865
0.1847556794086489
0.32127067165147705
0.12266607697656651
0.62729812639505
0.12191275953517977
0.72308829958122
0.5766115831015479
0.03444891261877858
0.3034596140603594
0.058872682884219985
0.85618745813345
0.07472991871327217
0.32676415639620604
0.6390549075075913
0.09246846051500635
0.9135703200463736
0.661365290176903
0.834153688687532
0.6340862529502882
0.3240420706550172
0.07246065490188025
0.35761285323105796
0.9964556875646731
 
 
Java's 'labeled while' loop 
 
public class MainClass {
  public static void main(String[] args) {
    int i = 0;
    outer: while (true) {
      System.out.println("Outer while loop");
      while (true) {
        i++;
        System.out.println("i = " + i);
        if (i == 1) {
          System.out.println("continue");
          continue;
        }
        if (i == 3) {
          System.out.println("continue outer");
          continue outer;
        }
        if (i == 5) {
          System.out.println("break");
          break;
        }
        if (i == 7) {
          System.out.println("break outer");
          break outer;
        }
      }
    }
  }
}
 
Compile
Outer while loop
i = 1
continue
i = 2
i = 3
continue outer
Outer while loop
i = 4
i = 5
break
Outer while loop
i = 6
i = 7
break outer
 

http://www.java2s.com/Tutorial/Java/0080__Statement-Control/Catalog0080__Statement-Control.htm

















»»  READMORE...

The switch Statement

  1. An alternative to a series of else if is the switch statement.
  2. The switch statement allows you to choose a block of statements to run from a selection of code, based on the return value of an expression.
  3. The expression used in the switch statement must return an int or an enumerated value.
The syntax of the switch statement is as follows.
switch (expression) {
case value_1 :

     statement (s);
     break;
case value_2 :
     statement (s);
     break;
  .
  .
  .
case value_n :
     statement (s);
     break;
default:
     statement (s);
}
Failure to add a break statement after a case will not generate a compile error but may have more serious consequences because the statements on the next case will be executed.
Here is an example of the switch statement:
public class MainClass {

  public static void main(String[] args) {
    int i = 1;
    switch (i) {
    case :
        System.out.println("One.");
        break;
    case :
        System.out.println("Two.");
        break;
    case :
        System.out.println("Three.");
        break;
    default:
        System.out.println("You did not enter a valid value.");
    }
  }

}
The switch Statement: a demo 

public class MainClass {
  public static void main(String[] args) {
    int choice = 2;

    switch (choice) {
    case 1:
      System.out.println("Choice 1 selected");
      break;
    case 2:
      System.out.println("Choice 2 selected");
      break;
    case 3:
      System.out.println("Choice 3 selected");
      break;
    default:
      System.out.println("Default");
      break;
    }
  }
}
compile
Choice 2 selected
 
 
 
Execute the same statements for several different case labels

public class MainClass {
  
  public static void main(String[] args) {
    char yesNo = 'N';

    switch(yesNo) {
      case 'n'case 'N':
           System.out.println("No selected");
           break;
      case 'y'case 'Y':
           System.out.println("Yes selected");
           break;
    }
  }
}
compile
No selected
 
 
 
 Free Flowing Switch Statement Example

public class Main {

  public static void main(String[] args) {
    int i = 0;
    switch (i) {

    case 0:
      System.out.println("i is 0");
    case 1:
      System.out.println("i is 1");
    case 2:
      System.out.println("i is 2");
    default:
      System.out.println("Free flowing switch example!");
    }
  }
}
/*
i is 0
i is 1
i is 2
Free flowing switch example!
*/
 
 
 
 
Nested Switch Statements Example

public class Main {

  public static void main(String[] args) {
    int i = 0;
    switch (i) {
    case 0:
      int j = 1;
      switch (j) {
      case 0:
        System.out.println("i is 0, j is 0");
        break;
      case 1:
        System.out.println("i is 0, j is 1");
        break;
      default:
        System.out.println("nested default case!!");
      }
      break;
    default:
      System.out.println("No matching case found!!");
    }
  }
}
//i is 0, j is 1
Switch statement with enum

public class MainClass {
  enum Choice Choice1, Choice2, Choice3 }
  public static void main(String[] args) {
    Choice ch = Choice.Choice1;

    switch(ch) {
      case Choice1:
        System.out.println("Choice1 selected");
        break;
     case Choice2:
       System.out.println("Choice2 selected");
       break;
     case Choice3:
       System.out.println("Choice3 selected");
       break;
    }
  }
}
Choice1 selected
 http://www.java2s.com/Tutorial/Java/0080
__Statement-Control/Catalog0080__Statement-Control.htm
 
 
 
 
 
 
 
 
 
 
 
 
 
»»  READMORE...

Thursday, October 20, 2011

If Statement

1. The if statement syntax

      The if statement is a conditional branch statement. The syntax of the
if statement is either one of these two:
if (booleanExpression) {
    statement (s)
}
or
if (booleanExpression) {
    statement (s)
else {
    statement (s)
}
For example, in the following if statement, the if block will be executed if x is greater than 4.
public class MainClass {

  public static void main(String[] args) {
    int x = 9;
    if (x > 4) {
      // statements
    }
  }

}
In the following example, the if block will be executed if a is greater than 3.
Otherwise, the else block will be executed.
public class MainClass {

  public static void main(String[] args) {
    int a = 3;
    if (a > 3) {
      // statements
    else {
      // statements
    }
  }

}
2. Expression indentation for if statement

If the expression is too long, you can use two units of indentation for subsequent lines.
public class MainClass {

  public static void main(String[] args) {
    int numberOfLoginAttempts = 10;
    int numberOfMinimumLoginAttempts = 12;
    int numberOfMaximumLoginAttempts = 13;
    int y = 3;

    if (numberOfLoginAttempts < numberOfMaximumLoginAttempts
        || numberOfMinimumLoginAttempts > y) {
      y++;
    }
  }

}
3. Using braces makes your 'if' statement clearer

If there is only one statement in an if or else block, the braces are optional.
public class MainClass {

  public static void main(String[] args) {
    int a = 3;
    if (a > 3)
      a++;
    else
      a = 3;
  }

}
Consider the following example:
public class MainClass {

  public static void main(String[] args) {
    int a = 3, b = 1;

    if (a > || b < 5)
      if (a > 2)
        System.out.println("a > 2");
      else
        System.out.println("a < 2");
  }

}
  1. It is hard to tell which if statement the else statement is associated with.
  2. Actually, An else statement is always associated with the immediately preceding if.
Using braces makes your code clearer.
public class MainClass {

  public static void main(String[] args) {
    int a = 3, b = 1;

    if (a > || b < 5) {
      if (a > 2) {
        System.out.println("a > 2");
      else {
        System.out.println("a < 2");
      }
    }
  }

}
4. Multiple selections
If there are multiple selections, you can also use if with a series of else statements.
if (booleanExpression1) {
    // statements
else if (booleanExpression2) {
    // statements
}
...
else {
    // statements
}
For example:
public class MainClass {

  public static void main(String[] args) {
    int a = 0;

    if (a == 1) {
      System.out.println("one");
    else if (a == 2) {
      System.out.println("two");
    else if (a == 3) {
      System.out.println("three");
    else {
      System.out.println("invalid");
    }
  }

}
5. The if Statement in action


public class MainClass {

  public static void main(String[] arg) {
    int a = 0;

    if (a == 0) {
      System.out.println("a is 0");
    }
  }
6. The else Clause

public class MainClass {

  public static void main(String[] arg) {
    int a = 0;
    if (a == 0) {
      System.out.println("in the block");
      System.out.println("in the block");
    else {
      System.out.println("a is not 0");
    }
  }
}

compile results
in the block
in the block
7.Nested if Statements
public class MainClass {

  public static void main(String[] arg) {
    int a = 2;
    if (a == 0) {
      System.out.println("in the block");
      if (a == 2) {
        System.out.println("a is 0");
      else {
        System.out.println("a is not 2");
      }

    else {
      System.out.println("a is not 0");
    }
  }
}
compile results
a is not 0
8.Using && in if statement

public class MainClass {

  public static void main(String[] arg) {
    int value = 8;
    int count = 10;
    int limit = 11;

    if (++value % == && ++count < limit) {
      System.out.println("here");
      System.out.println(value);
      System.out.println(count);
    }
    System.out.println("there");
    System.out.println(value);
    System.out.println(count);
  }
}


compile results
there
9
10
 
9.Using || (or operator) in if statement 
 
public class MainClass {

  public static void main(String[] arg) {
    int value = 8;
    int count = 10;
    int limit = 11;

    if (++value % != || ++count < limit) {
      System.out.println("here");
      System.out.println(value);
      System.out.println(count);
    }
    System.out.println("there");
    System.out.println(value);
    System.out.println(count);
  }
}


compile results
here 
10 
»»  READMORE...