Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

10. Loops in Java

 

Loops in Java

  • The looping can be defined as repeating the same process multiple times until a specific condition specifies.
  • The looping simplifies the complex problems into the easy ones.
  • It enables us to alter the flow of the program so that instead of writing the same code again and again.

Types of Loops

Generally, there are three types of loops in Java:

  1. while Loop
  2. do-while Loop
  3. for Loop

While Loop

  • A while loop allows a part of code to be executed multiple times depending upon a given Boolean condition.
  • The while loop is used in the case where the number of iterations is not known in advance.
  • In while loop, the condition is checked first then the code under while loop is executed.
  • If the condition never becomes false, the while loop keeps getting executed. Such a loop is known as an infinite loop.

Control flow for while loop

class WhileLoop {
    public static void main(String[] arr) {
        System.out.println("Printing numbers from 100 to 110.");
        int i = 100;
        while (i <= 110) {
            System.out.println(i);
            i++;
        }
        System.out.println("End of a WHILE loop at i = 111.");
        int num = 123;
        System.out.println("\nStarting of a WHILE loop.");
        while (num != 0) {
            System.out.println(num);
            num = num / 10;
        }
        System.out.println("Ending of a WHILE loop at num = 0.");
    }
}

Output :

Printing numbers from 100 to 110.
100
101
102
103
104
105
106
107
108
109
110
End of a WHILE loop at i = 111.

Starting of a WHILE loop.
123
12
1
Ending of a WHILE loop at num = 0.

Do-While Loop

  • Do-while loop is similar to a while loop expect for the fact that it is guaranteed to execute at least once.
  • Do-while loop is used when the exact number of iterations is unknown, but we need to execute a code block at least once.
  • After executing a part of a program for once, the rest of the code gets executed on the basis of a Boolean condition.
  • In do-while loop, first the code is executed at least once, then checks the condition.

Control flow for do-while loop

class DoWhileLoop {
    public static void main(String[] arr) {
        int i = 25;
        System.out.println("Starting of a WHILE loop");
        while (i < 16) {
            System.out.println(i);
            i++;
        }
        System.out.println("Ending of a WHILE loop");
        System.out.println("\nStarting of a DO-WHILE loop");
        do {
            System.out.println(i);
            i++;
        } while (i < 16);
        System.out.println("Ending of a DO-WHILE loop");
    }
}

Output :

Starting of a WHILE loop
Ending of a WHILE loop

Starting of a DO-WHILE loop
25
Ending of a DO-WHILE loop

For Loop

  • For loop is used when the exact number of iterations needed is already known to us.
  • Syntax of for loop consists of 3 expressions:
    1. Initializer: Initializes the value of a variable. This part is executed only once.
    2. Checking_Boolean_Expression: The code inside the for loop is executed only when this condition returns true.
    3. Update: Updates the value of the initial value.
Control flow for for loop


class ForLoop {
    public static void main(String[] arr) {
        System.out.println("Printing numbers from 100 to 110.");
        for (int i = 100; i <= 110; i = i + 1) {
            System.out.println(i);
        }
        System.out.println("End of a FOR loop at i = 111.");
        System.out.println("\nStarting of a FOR loop.");
        for (int i = 110; i >= 100; i = i - 1) {
            System.out.println(i);
        }
        System.out.println("Ending of a FOR loop at i = 99.");
    }
}

Output :

Printing numbers from 100 to 110.
100
101
102
103
104
105
106
107
108
109
110
End of a FOR loop at i = 111.

Starting of a FOR loop.
110
109
108
107
106
105
104
103
102
101
100
Ending of a FOR loop at i = 99.


Previous

Next

Comments