Java - Loop Structure


  Java -  Loop Structure

  • Java code within a loop lets the user for multiple execution of a single/group of statements. They are categorized into three types:

1.Decision making statements


Fig1. If statements


Fig2.  Switch Case
  •  if statement 
  • Switch Case

2. Repetition statements :


Fig3. While Loop

Fig4. Do While Loop



Fig5. For loop
  • While Loop
  • Do-While Loop
  • For Loop

3.Branching statements :

Branching Keywords

  • break
  • continue
  • return

Sample Code:

public class WorkingWithLoops
{   
    public static void main(String[] args)
    {
        System.out.println("\n\nWikiTechy - Working With Loops\n");  
        int a = 20, b = 10, c = 30;
        System.out.println("Input Values: a=20, b=10, c=30");
        // if else statements
        System.out.println("\n  If Statement");
        System.out.println("  ------------");
        if(a > b)
        {
            if (a > c)
                System.out.println("\na is greater than b and c");
            else
                System.out.println("\nc is greater than a and b");
        }
        else if (b > c)
            System.out.println("\nb is greater than a and c");
        else
            System.
out.println("\nc is greater than a and b");
        // switch case
        System.out.println("\n Switch Case Statement");
        System.out.println("  ---------------------");
        switch(a)
        {
            case 10:
                System.out.println("\na is the lowest value");
                break;
            case 30:
                System.out.println("\na is the highest value");
                break;
            default:
                System.out.println("\na is the middle value");
                break;
        }
        // while loop         System.out.println("\n While Loop");         System.out.println(" ----------");         int d = a;         while (d%3 != 0)         {             d = d - 2;         }         System.out.println("\nValue of d: " + d);              // for loop         System.out.println("\n For Loop");         System.out.println(" --------");         my_for:         for (int i=1; i<=5; i++)         {             if (i == 3)             break my_for;             System.out.println("\nValue of i: " + i);         }     } }

Code Explanation:


    Nested If statement to check for the greater value implemented with the code

if(a > b)
    {
    if (a > c)
System.out.println("\na is greater than b and c");
else
System
.out.println("\nc is greater than a and b");
}
else if (b > c)
System.out.println("\nb is greater than a and c");
else
 System
.out.println("\nc is greater than a and b");



    Switch case implemented to identify the state of the integer value a.
	    switch(a)
   {
    case 10:
    System.out.println("\na is the lowest value");
    break;
    case 30:
    System.out.println("\na is the highest value");
    break;
default:
System.out.println("\na is the middle value");
    break;
   }
    While loop with a condition to print the value for d is implemented with the code:
	    int d = a;
   while (d%3 != 0)
   {
    d = d - 2;
   }
   System.out.println("\nValue of d: " + d);


    For loop with a label to print values of variable i.

	    my_for:
   for (int i=1; i<=5; i++)
   {
    if (i == 3)
    break my_for;
    System.out.println("\nValue of i: " + i);
 }


    Break statement calling for loop label as break my_for;

Output:


Hence the given while loop condition the code has been executed successfully.