Java Bitwise AND and OR Exclusive OR and shift operators



To understand the necessity of a while loop consider that we wish to print a table of inch to centimeter from 1 inches to 10 inches. Here is a program that does it.

  1. /*
  2.   ReferenceDesigner.com Tutorial for beginners
  3.   Inch to cm conversion
  4.  */
  5. class inchcm{
  6. public static void main (String args[]) {
  7. int i;
  8. double cm ;
  9. // Print a Table of Inch Centimeter
  10. i =1 ;
  11. System.out.println("Inch cm ");
  12. cm = i * 2.54 ;
  13. System.out.print( i ); System.out.println(" "+cm );
  14. i++; cm = i*2.54; System.out.print( i ); System.out.println(" "+cm );
  15. i++; cm = i*2.54; System.out.print( i ); System.out.println(" "+cm );
  16. i++; cm = i*2.54; System.out.print( i ); System.out.println(" "+cm );
  17. i++; cm = i*2.54; System.out.print( i ); System.out.println(" "+cm );
  18. i++; cm = i*2.54; System.out.print( i ); System.out.println(" "+cm );
  19. i++; cm = i*2.54; System.out.print( i ); System.out.println(" "+cm );
  20. i++; cm = i*2.54; System.out.print( i ); System.out.println(" "+cm );
  21. i++; cm = i*2.54; System.out.print( i ); System.out.println(" "+cm );
  22. i++; cm = i*2.54; System.out.print( i ); System.out.println(" "+cm );
  23.  
  24.  
  25.  
  26. }
  27. }


If you compile and run this program, you will get the output as follows


C:\Program Files\Java\jdk1.7.0_17\bin>java inchcm
Inch cm
1 2.54
2 5.08
3 7.62
4 10.16
5 12.7
6 15.24
7 17.78
8 20.32
9 22.86
10 25.4




Here is the same thing accomplished using while loop.

  1. /*
  2.   ReferenceDesigner.com Tutorial for beginners
  3.   while loop example
  4.  */
  5. class whileexample{
  6. public static void main (String args[]) {
  7. int i;
  8. double cm ;
  9. // Print a Table of Inch Centimeter
  10. i =1 ;
  11. System.out.println("Inch cm ");
  12. while (i<=10)
  13. {
  14. cm = i*2.54 ;
  15. System.out.print( i );
  16. System.out.println(" "+cm );
  17. i++;
  18. }
  19. }
  20. }


If you compile and run this program, you will get the output as follows


C:\Program Files\Java\jdk1.7.0_17\bin>java whileexample
Inch cm
1 2.54
2 5.08
3 7.62
4 10.16
5 12.7
6 15.24
7 17.78
8 20.32
9 22.86
10 25.4




Formally, a while construct is defined as

  1. while (expression) {
  2. statement(s)
  3. }


The expression is evaluated and if not found true, the list of the statements in curly brackets are executed. Typically, within the curly bracket one of variables is changed and is evaluated again in the next iteration of the while loop.

A do while loop is another similar construct in which the expression is evaluated at the end of the statements. We present here the same example with do while loop.

  1. /*
  2.   ReferenceDesigner.com Tutorial for beginners
  3.   do while loop example
  4.   */
  5. class dowhileexample{
  6. public static void main (String args[]) {
  7. int i;
  8. double cm ;
  9. // Print a Table of Inch Centimeter
  10. i =1 ;
  11. System.out.println("Inch cm ");
  12. do
  13. {
  14. cm = i*2.54 ;
  15. System.out.print( i );
  16. System.out.println(" "+cm );
  17. i++;
  18. }
  19. while (i<=11) ;
  20. }
  21. }


In the do-while loop the the expression is evaluated at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once. The while loop and the do loop are equivalent in their expressive power. You can rewrite a while loop using a do loop, and vice versa.

Formally a do while loop is defined as

  1. do {
  2. statement(s)
  3. } while (expression);
  4.  


Special Considerations - while loop with increment decrement operators


Care should be taken to understand the number of times a while loop executes when used with increment operators. Take this example

  1. /*
  2.   ReferenceDesigner.com Tutorial for beginners
  3.   do while with increment decrement operator
  4.   */
  5. class dowhileexample1{
  6. public static void main (String args[]) {
  7. int count = 0;
  8. while (count++ <= 3) {
  9. System.out.println("Welcome to Java");
  10. }
  11. }
  12. }


How many times the statement Welcome to Java printed ?

The first time the expression

while (count++ <= 3)

is evaluated,

the count value is incremented AFTER the comparison. So the first time, the we are comparing 0 with 3. Therefore the loop is executed 4 times.

Exercises


Exercise 1
Write Java code using while loop to print a table of degree centrigrade to degree fahrenheit. The table should list degree centrigrade from 0 to 20 in steps of 2.

Exercise 2

Repeat the above code using do while


Exercise 3
How many times is the while looop executed in the following code

br />
  1. class dowhileexample1{
  2. public static void main (String args[]) {
  3. int count = 4;
  4. do
  5. {
  6. System.out.println("Welcome to Java");
  7. }
  8. while (count-- >= 1);
  9. }
  10. }


Answer : 5