on Leave a Comment

Java while Loop with Examples

Java while loop executes multiple statements repeatedly until certain conditions holds true.

General form of Java while loop:

initialization;
while(condition)
{
  //Body of loop
}

The condition is evaluated and if the condition is true, body of loop is executed. Control is transferred back to the loop condition after executing the body of loop and the condition is evaluated again and if the condition is true, body of loop is executed.

Body of loop may contain zero or more statements. If there is only statement, then it is not necessary to put braces.

Flow Diagram of Java while Loop
Java while loop
Java while loop
Example of Java while Loop

class JavaWhileLoop
{
 public static void main(String[] args) {
  char c = 'A';

  while(c<='E')
  {
   System.out.println(c);
   c++;
  }
 }
}

Output:

A
B
C
D
E
on Leave a Comment

Java for Loop With Examples

The java for loop is a entry controlled loop, that allows to execute specific block of code repeatedly until certain condition is false.

General Form of Java for Loop is:

for(initialization; condition; update)
{
  //Body of the loop
}

Execution of for loop statement is as follows:

(1) Initialization is done using assignment operator, for example i = 0. The variable which is initialized is called loop control variable. Initialization code executes once at the beginning of for loop statement. You can initialize zero or more than one variable in a single for loop statement.

Example:

class JavaForLoop
{
 public static void main(String[] args) {
  int i = 1;
  for( ; i<=5; i++)
  {
   System.out.println(i);
  }
 }
}

Output:

1
2
3
4
5

(2) The loop condition is a relational expression. This condition can be either true or false. If it is true, body of for loop executes and if the condition is false, body of for loop will skipped. This condition is checked every time after executing body of for loop

(3) After executing the last statement of for loop, control is transferred back to the for statement. Now the control variable will update (for example increment or decrement). New value of control variable is again tested will loop condition.

Flow Chart of Java for Loop
Java for Loop
Java for Loop
Example of Java for Loop

class JavaForLoop
{
 public static void main(String[] args) {
  int i = 2;
  for(int j = 1 ; j<=10; j++)
  {
   System.out.println(i+" * "+j+" = "+i*j);
  }
 }
}

Output:


2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

on Leave a Comment

Java Program to Find Largest Number in an Array

This java program finds the largest number in an array. First, enter the size of array, then enter the elements of array. Now, store the first array element into a variable, then compare this variable with each array element, if variable is smaller than any array elements, then store that array element into variable. 

Java Program to Find Largest Number in an Array

/* Java Program to Find Largest Element in Array */

import java.util.Scanner;

public class LargestNumber
{
   public static void main(String args[])
   {
       int size, largest, i;
       Scanner read = new Scanner(System.in);
     
       System.out.print("Enter the Size of Array : ");
       size = read.nextInt();
       int arr[] = new int[size];
          
       System.out.print("Enter Array Elements : ");
       for(i=0; i<size; i++)
       {
           arr[i] = read.nextInt();
       }
    
       /* Searching for the Largest Number*/
    
       largest = arr[0];
    
       for(i=0; i<size; i++)
       {
           if(largest < arr[i])
           {
               largest = arr[i];
           }
           
       }
    
       System.out.print("Largest Element in Array is " +largest); 
   }
}

Output:

Enter the Size of Array : 5
Enter Array Elements : 89
56
87
65
90
Largest Element in Array is 90
on Leave a Comment

Java Program for Selection Sort

This java program sorts an array using selection sort technique. First enter the length of array using keyboard, after that enter the elements of array. Now, compare the elements of array and if first element is greater than second element, then perform swapping.

Selection Sort in Java

import java.util.Scanner;
class SelectionSort
{
    public static void main(String[] args) {
       int length, i, j, temp;
       System.out.print("Enter the Length of Array : ");
       Scanner sc = new Scanner(System.in);
       length = sc.nextInt();
       int arr[] = new int[length];
 
       System.out.print("Enter Array Elements : ");
       for(i=0; i<length; i++)
       {
           arr[i] = sc.nextInt();
       }
    
       for(i=0; i<length; i++)
       {
           for(j=i+1; j<length; j++)
           {
               if(arr[i] > arr[j])
               {
                   temp = arr[i];
                   arr[i] = arr[j];
                   arr[j] = temp;
               }
           }
       }
    
       System.out.print("Sorted Array : ");
       for(i=0; i<length; i++)
       {
           System.out.print(arr[i]+ "  ");
       }
   }
}

Output:

Enter the Length of Array : 5
Enter Array Elements : 8 6 7 1 2
Sorted Array : 1  2  6  7  8
on Leave a Comment

Java Switch Case Statement

The switch statement is java built-in multi-way decision statement. The switch statement test an expression with a list of case values and if the expression matches with case value, the block of statements followed by that case will execute.

Syntax:

switch(expression) 
{
   case value1:
      block1
      break; 
   
   case value2:
      block2
      break; 
   ......
   ......
   default : 
      // Statements
}

Rules for implementing switch statement:

The expression should be integer expression or character expression.

The value1, value2 are also integral constant.

All values should be unique in a switch statement.

We can use any number of case statements and each case statement is following by value and colon.

Each case block may contain zero or more statements.

It is not necessary to put braces around the blocks.

The default statement is optional and if you write default statement, it must appear after all the case statements.

Flow Chart
Java Switch Statement
Java Switch Statement

Example of Switch Statement

import java.util.Scanner;
class SwitchStatement
{
 public static void main(String[] args) {
  System.out.print("Enter a number between 1 to 7 : ");
  Scanner sc = new Scanner(System.in);
  int x = sc.nextInt();
  String day;
  switch(x)
  {
   case 1:  
         day = "Monday";
         System.out.println(day);
                              break;
                        case 2:  
                              day = "Tuesday";
                              System.out.println(day);
                              break;
                        case 3:  
                              day = "Wednesday";
                              System.out.println(day);
                              break;
                        case 4:
                               day = "Thursday";
                               System.out.println(day);
                               break;
                        case 5:
                               day = "Friday";
                               System.out.println(day);
                               break;
                        case 6:
                               day = "Saturday";
                               System.out.println(day);
                               break;
                        case 7:
                               day = "Sunday";
                               System.out.println(day);
                               break;
                       default:
                               day = "Invalid day";
                               System.out.println(day);
                               break;
  }
 }
}

Output:

Enter a number between 1 to 7 : 6
Saturday

In this program value of x will compared with case values, if any match is found, that case block will execute, otherwise default statements will execute.