+ All Categories
Home > Documents > Java Programs

Java Programs

Date post: 22-Nov-2014
Category:
Upload: sethuraman-sundaresan
View: 113 times
Download: 1 times
Share this document with a friend
Popular Tags:
28
For Loop For loop executes group of Java statements as long as the boolean condition evaluates to true. For loop combines three elements which we generally use: initialization statement, boolean expression and increment or decrement statement. For loop syntax 1. for( <initialization> ; <condition> ; <statement> ){ 2. 3. <Block of statements>; 4. 5. } The initialization statement is executed before the loop starts. It is generally used to initialize the loop variable. Condition statement is evaluated before each time the block of statements are executed. Block of statements are executed only if the boolean condition evaluates to true. Statement is executed after the loop body is done. Generally it is being used to increment or decrement the loop variable. Following example shows use of simple for loop. 1. for(int i=0 ; i < 5 ; i++) 2. { 3. System.out.println(“i is : + i); 4. } It is possible to initialize multiple variable in the initialization block of the for loop by separating it by comma as given in the below example. 1. for(int i=0, j =5 ; i < 5 ; i++) It is also possible to have more than one increment or decrement section as well as given below. 1. for(int i=0; i < 5 ; i++, j--)
Transcript
Page 1: Java Programs

For Loop

For loop executes group of Java statements as long as the boolean condition evaluates to true.

For loop combines three elements which we generally use: initialization statement, boolean expression and increment or decrement statement.

For loop syntax

1. for( <initialization> ; <condition> ; <statement> ){2.  

3. <Block of statements>;

4.  

5. }

The initialization statement is executed before the loop starts. It is generally used to initialize the loop variable.

Condition statement is evaluated before each time the block of statements are executed. Block of statements are executed only if the boolean condition evaluates to true.

Statement is executed after the loop body is done. Generally it is being used to increment or decrement the loop variable.

Following example shows use of simple for loop.

1. for(int i=0 ; i < 5 ; i++)2. {

3. System.out.println(“i is : “ + i);

4. }

It is possible to initialize multiple variable in the initialization block of the for loop by separating it by comma as given in the below example.

1. for(int i=0, j =5 ; i < 5 ; i++)

It is also possible to have more than one increment or decrement section as well as given below.

1. for(int i=0; i < 5 ; i++, j--)

However it is not possible to include declaration and initialization in the initialization block of the for loop.

Also, having multiple conditions separated by comma also generates the compiler error. However, we can include multiple condition with && and || logical operators.

Calculate Average value of Array elements using Java Example

Page 2: Java Programs

1. /*2. Calculate Average value of Array elements using Java Example

3. This Java Example shows how to calculate average value of array

4. elements.

5. */

6. public class CalculateArrayAverageExample {

7.  

8. public static void main(String[] args) {

9.  

10. //define an array

11. int[] numbers = new int[]{10,20,15,25,16,60,100};

12.  

13. /*

14. * Average value of array elements would be

15. * sum of all elements/total number of elements

16. */

17.  

18. //calculate sum of all array elements

19. int sum = 0;

20.  

21. for(int i=0; i < numbers.length ; i++)

22. sum = sum + numbers[i];

23.  

24. //calculate average value

25. double average = sum / numbers.length;

26.  

27. System.out.println("Average value of array elements is : " + average);

28. }

29. }

30.  

Page 3: Java Programs

31. /*

32. Output of Calculate Average value of Array elements using Java Example would be

33. Average value of array elements is : 35.0

34. */

Declare multiple variables in for loop Example

1. /*2.   Declare multiple variables in for loop Example3.   This Java Example shows how to declare multiple variables in Java For loop

using4.   declaration block.5. */6.  7. public class DeclaringMultipleVariablesInForLoopExample {8.  9. public static void main(String[] args) {10.  11. /*12.   * Multiple variables can be declared in declaration block of for

loop.13.   */14.  15. for(int i=0, j=1, k=2 ; i<5 ; i++)16. System.out.println("I : " + i + ",j : "+ j + ", k : " + k);17.  18. /*19.   * Please note that the variables which are declared, should be of

same type20.   * as in this example int.21.   */ 22.  23. //THIS WILL NOT COMPILE24. //for(int i=0, float j; i < 5; i++);25. }26. }

Fibonacci Series Java Example

1. /*2. Fibonacci Series Java Example3. This Fibonacci Series Java Example shows how to create and print4. Fibonacci Series using Java.5. */6.  7. public class JavaFibonacciSeriesExample {8.  9. public static void main(String[] args) {10.  11. //number of elements to generate in a series12. int limit = 20;13.  14. long[] series = new long[limit];15.  

Page 4: Java Programs

16. //create first 2 series elements17. series[0] = 0;18. series[1] = 1;19.  20. //create the Fibonacci series and store it in an array21. for(int i=2; i < limit; i++){22. series[i] = series[i-1] + series[i-2];23. }24.  25. //print the Fibonacci series numbers26.  27. System.out.println("Fibonacci Series upto " + limit);28. for(int i=0; i< limit; i++){29. System.out.print(series[i] + " ");30. }31. }32. }33.  34. /*35. Output of the Fibonacci Series Java Example would be36. Fibonacci Series upto 2037. 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 38. */

Generate Pyramid For a Given Number Example

Submitted By: mhack_rances(viado)

1. /*2. Generate Pyramid For a Given Number Example3. This Java example shows how to generate a pyramid of numbers for given4. number using for loop example.5. */6.  7. import java.io.BufferedReader;8. import java.io.InputStreamReader;9.  10. public class GeneratePyramidExample {11.  12. public static void main (String[] args) throws Exception{13.  14. BufferedReader keyboard = new BufferedReader (new

InputStreamReader (System.in));15.  16. System.out.println("Enter Number:");17. int as= Integer.parseInt (keyboard.readLine());18. System.out.println("Enter X:");19. int x= Integer.parseInt (keyboard.readLine());20.  21. int y = 0;22.  23. for(int i=0; i<= as ;i++){24.  25. for(int j=1; j <= i ; j++){26. System.out.print(y + "\t");27. y = y + x;28. }

Page 5: Java Programs

29.  30. System.out.println("");31. }32. }33. }34.  35. /*36. Output of this example would be37.  38. Enter Number:39. 540. Enter X:41. 142.  43. 044. 1 245. 3 4 546. 6 7 8 947. 10 11 12 13 1448.  49. ----------------------------------------------50.  51. Enter Number:52. 553. Enter X:54. 255.  56. 057. 2 458. 6 8 1059. 12 14 16 1860. 20 22 24 26 2861.  62. ----------------------------------------------63.  64. Enter Number:65. 566. Enter X:67. 368.  69. 070. 3 671. 9 12 1572. 18 21 24 2773. 30 33 36 39 4274. */

java Palindrome Number Example

1. /*2. Java Palindrome Number Example3. This Java Palindrome Number Example shows how to find if the given4. number is palindrome number or not.5. */6.  7.  8. public class JavaPalindromeNumberExample {

Page 6: Java Programs

9.  10. public static void main(String[] args) {11.  12. //array of numbers to be checked13. int numbers[] = new int[]{121,13,34,11,22,54};14.  15. //iterate through the numbers16. for(int i=0; i < numbers.length; i++){17.  18. int number = numbers[i];19. int reversedNumber = 0;20. int temp=0;21.  22. /*23. * If the number is equal to it's reversed number,

then24. * the given number is a palindrome number.25. * 26. * For example, 121 is a palindrome number while 12

is not.27. */28.  29. //reverse the number30. while(number > 0){31. temp = number % 10;32. number = number / 10;33. reversedNumber = reversedNumber * 10 +

temp;34. }35.  36. if(numbers[i] == reversedNumber)37. System.out.println(numbers[i] + " is a

palindrome number");38. else 39. System.out.println(numbers[i] + " is not a

palindrome number");40. }41.  42. }43. }44.  45. /*46. Output of Java Palindrome Number Example would be47. 121 is a palindrome number48. 13 is not a palindrome number49. 34 is not a palindrome number50. 11 is a palindrome number51. 22 is a palindrome number52. 54 is not a palindrome number53. */

List Even Numbers Java Example

1. /*2. List Even Numbers Java Example3. This List Even Numbers Java Example shows how to find and list even4. numbers between 1 and any given number.

Page 7: Java Programs

5. */6.  7. public class ListEvenNumbers {8.  9. public static void main(String[] args) {10.  11. //define limit12. int limit = 50;13.  14. System.out.println("Printing Even numbers between 1 and " +

limit);15.  16. for(int i=1; i <= limit; i++){17.  18. // if the number is divisible by 2 then it is even19. if( i % 2 == 0){20. System.out.print(i + " ");21. }22. }23. }24. }25.  26. /*27. Output of List Even Numbers Java Example would be28. Printing Even numbers between 1 and 5029. 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 30. */

List Odd Numbers Java Example

1. /*2. List Odd Numbers Java Example3. This List Odd Numbers Java Example shows how to find and list odd4. numbers between 1 and any given number.5. */6.  7. public class ListOddNumbers {8.  9. public static void main(String[] args) {10.  11. //define the limit12. int limit = 50;13.  14. System.out.println("Printing Odd numbers between 1 and " +

limit);15.  16. for(int i=1; i <= limit; i++){17.  18. //if the number is not divisible by 2 then it is odd19. if( i % 2 != 0){20. System.out.print(i + " ");21. }22. }23. }

Page 8: Java Programs

24. }25.  26. /*27. Output of List Odd Numbers Java Example would be28. Printing Odd numbers between 1 and 5029. 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 30. */

Prime Numbers Java Example

1. /*2.   Prime Numbers Java Example3.   This Prime Numbers Java example shows how to generate prime numbers4.   between 1 and given number using for loop.5.  */6.  7. public class GeneratePrimeNumbersExample {8.  9. public static void main(String[] args) {10.  11. //define limit12. int limit = 100;13.  14. System.out.println("Prime numbers between 1 and " + limit);15.  16. //loop through the numbers one by one17. for(int i=1; i < 100; i++){18.  19. boolean isPrime = true;20.  21. //check to see if the number is prime22. for(int j=2; j < i ; j++){23.  24. if(i % j == 0){25. isPrime = false;26. break;27. }28. }29. // print the number 30. if(isPrime)31. System.out.print(i + " ");32. }33. }34. }35.  36. /*37. Output of Prime Numbers example would be38. Prime numbers between 1 and 10039. 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 9740. */

Java Pyramid 1 Example

1. /*2. Java Pyramid 1 Example3. This Java Pyramid example shows how to generate pyramid or triangle like4. given below using for loop.

Page 9: Java Programs

5.  6. *7. **8. ***9. ****10. *****11. */12.  13. public class JavaPyramid1 {14.  15. public static void main(String[] args) {16.  17. for(int i=1; i<= 5 ;i++){18.  19. for(int j=0; j < i; j++){20. System.out.print("*");21. }22.  23. //generate a new line24. System.out.println("");25. }26. }27. }28.  29. /*30. Output of the above program would be31. *32. **33. ***34. ****35. *****36. */

Java Pyramid 2 Example

1. /*2. Java Pyramid 2 Example3. This Java Pyramid example shows how to generate pyramid or triangle like4. given below using for loop.5.  6. *****7. ****8. ***9. **10. *11. */12.  13. public class JavaPyramid2 {14.  15. public static void main(String[] args) {16.  17. for(int i=5; i>0 ;i--){18.  19. for(int j=0; j < i; j++){20. System.out.print("*");21. }22.  

Page 10: Java Programs

23. //generate a new line24. System.out.println("");25. }26. }27. }28.  29. /*30.  31. Output of the example would be32. *****33. ****34. ***35. **36. *37.  38. */

Java Pyramid 3 Example

1. /*2. Java Pyramid 3 Example3. This Java Pyramid example shows how to generate pyramid or triangle like4. given below using for loop.5.  6. *7. **8. ***9. ****10. *****11. *****12. ****13. ***14. **15. *16. */17. public class JavaPyramid3 {18.  19. public static void main(String[] args) {20.  21. for(int i=1; i<= 5 ;i++){22.  23. for(int j=0; j < i; j++){24. System.out.print("*");25. }26.  27. //generate a new line28. System.out.println("");29. }30.  31. //create second half of pyramid32. for(int i=5; i>0 ;i--){33.  34. for(int j=0; j < i; j++){35. System.out.print("*");36. }37.  38. //generate a new line

Page 11: Java Programs

39. System.out.println("");40. }41.  42. }43. }44.  45. /*46.  47. Output of the example would be48. *49. **50. ***51. ****52. *****53. *****54. ****55. ***56. **57. *58.  59. */

Java Pyramid 4 Example

1. /*2. Java Pyramid 4 Example3. This Java Pyramid example shows how to generate pyramid or triangle like4. given below using for loop.5.  6. 17. 128. 1239. 123410. 1234511.  12. */13. public class JavaPyramid4 {14.  15. public static void main(String[] args) {16.  17. for(int i=1; i<= 5 ;i++){18.  19. for(int j=0; j < i; j++){20. System.out.print(j+1);21. }22.  23. System.out.println("");24. }25.  26. }27. }28.  29. /*30.  31. Output of the example would be32. 133. 12

Page 12: Java Programs

34. 12335. 123436. 1234537.  38. */

Java Pyramid 6 Example

1. /*2. Java Pyramid 6 Example3. This Java Pyramid example shows how to generate pyramid or triangle like4. given below using for loop.5.  6. *****7. ****8. ***9. **10. *11. *12. **13. ***14. ****15. *****16.  17. */18.  19. public class JavaPyramid6 {20.  21. public static void main(String[] args) {22.  23. //generate upper half of the pyramid24. for(int i=5; i>0 ;i--){25.  26. for(int j=0; j < i; j++){27. System.out.print("*");28. }29.  30. //create a new line31. System.out.println("");32. }33.  34. //generate bottom half of the pyramid35. for(int i=1; i<= 5 ;i++){36.  37. for(int j=0; j < i; j++){38. System.out.print("*");39. }40.  41. //create a new line42. System.out.println("");43. }44.  45. }46. }47.  48. /*49.  

Page 13: Java Programs

50. Output of the example would be51. *****52. ****53. ***54. **55. *56. *57. **58. ***59. ****60. *****61.  62. */

Read Number from Console and Check if it is a Palindrome Number

1. /*2.   Read Number from Console and Check if it is a Palindrome Number3.   This Java example shows how to input the number from console and4.   check if the number is a palindrome number or not.5.  */6.  7. import java.io.BufferedReader;8. import java.io.IOException;9. import java.io.InputStreamReader;10.  11. public class InputPalindromeNumberExample {12.  13. public static void main(String[] args) {14.  15. System.out.println("Enter the number to check..");16. int number = 0;17.  18. try19. {20. //take input from console21. BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));22. //parse the line into int23. number = Integer.parseInt(br.readLine());24.  25. }26. catch(NumberFormatException ne)27. {28. System.out.println("Invalid input: " + ne);29. System.exit(0);30. }31. catch(IOException ioe)32. {33. System.out.println("I/O Error: " + ioe);34. System.exit(0);35. }36.  37. System.out.println("Number is " + number);38. int n = number;39. int reversedNumber = 0;40. int temp=0;

Page 14: Java Programs

41.  42. //reverse the number43. while(n > 0){44. temp = n % 10;45. n = n / 10;46. reversedNumber = reversedNumber * 10 + temp;47. }48.  49. /*50. * if the number and it's reversed number are same, the

number is a 51. * palindrome number52. */53. if(number == reversedNumber)54. System.out.println(number + " is a palindrome

number");55. else 56. System.out.println(number + " is not a palindrome

number");57. }58.  59. }60.  61. /*62. Output of the program would be63. Enter the number to check..64. 12165. Number is 12166. 121 is a palindrome number67. */

Simple For loop Example

1. /*2.   Simple For loop Example3.   This Java Example shows how to use for loop to iterate in Java program.4. */5.  6. public class SimpleForLoopExample {7.  8. public static void main(String[] args) {9.  10. /* Syntax of for loop is11.   * 12.   * for(<initialization> ; <condition> ; <expression> )13.   * <loop body>14.   * 15.   * where initialization usually declares a loop variable, condition

is a 16.   * boolean expression such that if the condition is true, loop body

will be17.   * executed and after each iteration of loop body, expression is

executed which18.   * usually increase or decrease loop variable.19.   * 20.   * Initialization is executed only once.

Page 15: Java Programs

21.   */22.  23. for(int index = 0; index < 5 ; index++)24. System.out.println("Index is : " + index);25.  26. /*27.   * Loop body may contains more than one statement. In that case they

should 28.   * be in the block.29.   */30.  31. for(int index=0; index < 5 ; index++)32. {33. System.out.println("Index is : " + index);34. index++;35. }36.  37. /*38.   * Please note that in above loop, index is a local variable whose

scope39.   * is limited to the loop. It can not be referenced from outside

the loop.40.   */41. }42. }43.  44. /*45. Output would be46. Index is : 047. Index is : 148. Index is : 249. Index is : 350. Index is : 451. Index is : 052. Index is : 253. Index is : 454. */

sorting

Java Bubble Sort Descending Order Example

1. /*2. Java Bubble Sort Descending Order Example3. This Java bubble sort example shows how to sort an array of int in

descending4. order using bubble sort algorithm.5. */6.  7. public class BubbleSortDescendingOrder {8.  9. public static void main(String[] args) {10.  11. //create an int array we want to sort using bubble sort

algorithm12. int intArray[] = new int[]{5,90,35,45,150,3};13.  14. //print array before sorting using bubble sort algorithm

Page 16: Java Programs

15. System.out.println("Array Before Bubble Sort");16. for(int i=0; i < intArray.length; i++){17. System.out.print(intArray[i] + " ");18. }19.  20. //sort an array in descending order using bubble sort

algorithm21. bubbleSort(intArray);22.  23. System.out.println("");24.  25. //print array after sorting using bubble sort algorithm26. System.out.println("Array After Bubble Sort");27. for(int i=0; i < intArray.length; i++){28. System.out.print(intArray[i] + " ");29. }30.  31. }32.  33. private static void bubbleSort(int[] intArray) {34.  35. /*36. * In bubble sort, we basically traverse the array from

first 37. * to array_length - 1 position and compare the element with

the next one. 38. * Element is swapped with the next element if the next

element is smaller.39. * 40. * Bubble sort steps are as follows.41. * 42. * 1. Compare array[0] & array[1]43. * 2. If array[0] < array [1] swap it.44. * 3. Compare array[1] & array[2]45. * 4. If array[1] < array[2] swap it.46. * ...47. * 5. Compare array[n-1] & array[n]48. * 6. if [n-1] < array[n] then swap it.49. * 50. * After this step we will have smallest element at the last

index.51. * 52. * Repeat the same steps for array[1] to array[n-1]53. * 54. */55.  56. int n = intArray.length;57. int temp = 0;58.  59. for(int i=0; i < n; i++){60. for(int j=1; j < (n-i); j++){61.  62. if(intArray[j-1] < intArray[j]){63. //swap the elements!64. temp = intArray[j-1];65. intArray[j-1] = intArray[j];66. intArray[j] = temp;

Page 17: Java Programs

67. }68.  69. }70. }71.  72. }73. }74.  75. /*76. Output of the Bubble Sort Descending Order Example would be77.  78. Array Before Bubble Sort79. 5 90 35 45 150 3 80. Array After Bubble Sort81. 150 90 45 35 5 3 82.  83. */

Java Bubble Sort Example

1. /*2. Java Bubble Sort Example3. This Java bubble sort example shows how to sort an array of int using

bubble 4. sort algorithm. Bubble sort is the simplest sorting algorithm.5. */6.  7. public class BubbleSort {8.  9. public static void main(String[] args) {10.  11. //create an int array we want to sort using bubble sort

algorithm12. int intArray[] = new int[]{5,90,35,45,150,3};13.  14. //print array before sorting using bubble sort algorithm15. System.out.println("Array Before Bubble Sort");16. for(int i=0; i < intArray.length; i++){17. System.out.print(intArray[i] + " ");18. }19.  20. //sort an array using bubble sort algorithm21. bubbleSort(intArray);22.  23. System.out.println("");24.  25. //print array after sorting using bubble sort algorithm26. System.out.println("Array After Bubble Sort");27. for(int i=0; i < intArray.length; i++){28. System.out.print(intArray[i] + " ");29. }30.  31. }32.  33. private static void bubbleSort(int[] intArray) {34.  35. /*

Page 18: Java Programs

36. * In bubble sort, we basically traverse the array from first

37. * to array_length - 1 position and compare the element with the next one.

38. * Element is swapped with the next element if the next element is greater.

39. * 40. * Bubble sort steps are as follows.41. * 42. * 1. Compare array[0] & array[1]43. * 2. If array[0] > array [1] swap it.44. * 3. Compare array[1] & array[2]45. * 4. If array[1] > array[2] swap it.46. * ...47. * 5. Compare array[n-1] & array[n]48. * 6. if [n-1] > array[n] then swap it.49. * 50. * After this step we will have largest element at the last

index.51. * 52. * Repeat the same steps for array[1] to array[n-1]53. * 54. */55.  56. int n = intArray.length;57. int temp = 0;58.  59. for(int i=0; i < n; i++){60. for(int j=1; j < (n-i); j++){61.  62. if(intArray[j-1] > intArray[j]){63. //swap the elements!64. temp = intArray[j-1];65. intArray[j-1] = intArray[j];66. intArray[j] = temp;67. }68.  69. }70. }71.  72. }73. }74.  75. /*76. Output of the Bubble Sort Example would be77.  78. Array Before Bubble Sort79. 5 90 35 45 150 3 80. Array After Bubble Sort81. 3 5 35 45 90 15082.  83. */

Break Statement

Page 19: Java Programs

Break statement is one of the several control statements Java provide to control the flow of the program. As the name says, Break Statement is generally used to break the loop of switch statement.

Please note that Java does not provide Go To statement like other programming languages e.g. C, C++.

Break statement has two forms labeled and unlabeled.

Unlabeled Break statement

This form of break statement is used to jump out of the loop when specific condition occurs. This form of break statement is also used in switch statement.

For example,

1. for(int var =0; var < 5 ; var++)2. {3. System.out.println(“Var is : “ + var);4.  5. if(var == 3)6. break;7. }

In above break statement example, control will jump out of loop when var becomes 3.

Labeled Break Statement

The unlabeled version of the break statement is used when we want to jump out of a single loop or single case in switch statement. Labeled version of the break statement is used when we want to jump out of nested or multiple loops.

For example,

1. Outer:2. for(int var1=0; var1 < 5 ; var1++)3. {4. for(int var2 = 1; var2 < 5; var2++)5. {6. System.out.println(“var1:” + var1 + “, var2:” + var2);7.  8. if(var1 == 3)9. break Outer;10.  11. }12. }

Java break statement example

1. /*2.   Java break statement example.3.   This example shows how to use java break statement to terminate the loop. 4. */5. public class JavaBreakExample {6.  7. public static void main(String[] args) {8. /*9.   * break statement is used to terminate the loop in java. The following

example

Page 20: Java Programs

10.   * breaks the loop if the array element is equal to true.11.   * 12.   * After break statement is executed, the control goes to the

statement13.   * immediately after the loop containing break statement.14.   */15.  16. int intArray[] = new int[]{1,2,3,4,5};17.  18. System.out.println("Elements less than 3 are : ");19. for(int i=0; i < intArray.length ; i ++)20. { 21. if(intArray[i] == 3)22. break;23. else24. System.out.println(intArray[i]);25. }26.  27. }28. }29.  30. /*31. Output would be32. Elements less than 3 are : 33. 134. 235. */

Java break statement with label example

1. /*2.   Java break statement with label example.3.   This example shows how to use java break statement to terminate the labeled

loop.4.   The following example uses break to terminate the labeled loop while searching

two5.   dimensional int array. 6. */7.  8. public class JavaBreakWithLableExample {9.  10. public static void main(String[] args) {11.  12.  13. int[][] intArray = new int[][]{{1,2,3,4,5},{10,20,30,40,50}};14. boolean blnFound = false;15.  16. System.out.println("Searching 30 in two dimensional int array..");17.  18. Outer:19. for(int intOuter=0; intOuter < intArray.length ; intOuter++)20. {21. Inner:22. for(int intInner=0; intInner < intArray[intOuter].length;

intInner++)23. {24. if(intArray[intOuter][intInner] == 30)

Page 21: Java Programs

25. {26. blnFound = true;27. break Outer;28. } 29.  30. }31. }32.  33. if(blnFound == true)34. System.out.println("30 found in the array");35. else36. System.out.println("30 not found in the array");37. }38. }39.  40. /*41. Output would be42. Searching 30 in two dimensional int array..43. 30 found in the array44. */

Do While loop Example

1. /*2.   Do While loop Example3.   This Java Example shows how to use do while loop to iterate in Java program.4. */5.  6. public class DoWhileExample {7.  8. public static void main(String[] args) {9.  10. /*11.   * Do while loop executes statment until certain condition become

false.12.   * Syntax of do while loop is13.   * 14.   * do15.   * <loop body>16.   * while(<condition>);17.   * 18.   * where <condition> is a boolean expression.19.   * 20.   * Please not that the condition is evaluated after executing the

loop body.21.   * So loop will be executed at least once even if the condition is

false.22.   */23.  24. int i =0; 25.  26. do27. {28. System.out.println("i is : " + i);29. i++;30.  31. }while(i < 5);

Page 22: Java Programs

32.  33. }34. }35.  36. /*37. Output would be38. i is : 039. i is : 140. i is : 241. i is : 342. i is : 443. */

Determine If Year Is Leap Year Java Example

1. /*2. Determine If Year Is Leap Year Java Example3. This Determine If Year Is Leap Year Java Example shows how to 4. determine whether the given year is leap year or not.5. */6.  7. public class DetermineLeapYearExample {8.  9. public static void main(String[] args) {10.  11. //year we want to check12. int year = 2004;13.  14. //if year is divisible by 4, it is a leap year15.  16. if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 !

= 0)))17. System.out.println("Year " + year + " is a leap

year");18. else19. System.out.println("Year " + year + " is not a leap

year");20. }21. }22.  23. /*24. Output of the example would be25. Year 2004 is a leap year26. */

Compare Two Numbers Java Example

1. /*2. Compare Two Numbers Java Example3. This Compare Two Numbers Java Example shows how to compare two numbers4. using if else if statements.5. */6.  7. public class CompareTwoNumbers {8.  9. public static void main(String[] args) {10.  

Page 23: Java Programs

11. //declare two numbers to compare12. int num1 = 324;13. int num2 = 234;14.  15. if(num1 > num2){16. System.out.println(num1 + " is greater than " +

num2);17. }18. else if(num1 < num2){19. System.out.println(num1 + " is less than " + num2);20. }21. else{22. System.out.println(num1 + " is equal to " + num2);23. }24. }25. }26.  27. /*28. Output of Compare Two Numbers Java Example would be29. 324 is greater than 23430. */

Continue Statement

Continue statement is used when we want to skip the rest of the statement in the body of the loop and continue with the next iteration of the loop.There are two forms of continue statement in Java.

1. Unlabeled Continue Statement2. Labeled Continue Statement

Unlabeled Continue Statement

This form of statement causes skips the current iteration of innermost for, while or do while loop.

For example,

1. for(int var1 =0; var1 < 5 ; var1++)2. {

3.  

4. for(int var2=0 ; var2 < 5 ; var2++)

5. {

6. if(var2 == 2)

7. continue;

8.  

9. System.out.println(“var1:” + var1 + “, var2:”+ var2);

10.  

11. }

Page 24: Java Programs

12.  

13. }

In above example, when var2 becomes 2, the rest of the inner for loop body will be skipped.

Labeled Continue Statement

Labeled continue statement skips the current iteration of the loop marked with the specified label. This form is used with nested loops.

For example,

1. Outer:2. for(int var1 =0; var1 < 5 ; var1++)

3. {

4.  

5. for(int var2=0 ; var2 < 5 ; var2++)

6. {

7. if(var2 == 2)

8. continue Outer;

9.  

10. System.out.println(“var1:” + var1 + “, var2:”+ var2);

11.  

12. }

13.  

14. }

In the above example, when var2 becomes 2, rest of the statements in body of inner as well outer for loop will be skipped, and next iteration of the Outer loop will be execute

Java continue statement example

1. /*2.   Java continue statement example.3.   This example shows how to use java continue statement to skip the iteration of

the4.   loop. 5. */6.  7. public class JavaContinueExample {8.  9. public static void main(String[] args) {10.  11. /*12.   * Continue statement is used to skip a particular iteration of the

loop

Page 25: Java Programs

13.   */14. int intArray[] = new int[]{1,2,3,4,5};15.  16. System.out.println("All numbers except for 3 are :");17. for(int i=0; i < intArray.length; i++)18. {19. if(intArray[i] == 3)20. continue;21. else22. System.out.println(intArray[i]);23. }24. }25. }26.  27. /*28. Output would be29. All numbers except for 3 are :30. 131. 232. 433. 5

34. */0


Recommended