Built with Tutify.io

    AIStudyLabs

    Loops in Java: For, While, Do-While

    Loops in Java: For, While, Do-While

    Introduction

    In this lesson, we will delve into the fundamental concept of loops in Java: for, while, and do-while loops. Loops are essential for repetitive tasks, allowing you to execute a block of code multiple times based on certain conditions. We will explore the syntax and usage of each loop type, compare their differences, and apply them to solve practical programming problems.

    For Loops in Java

    For loops are commonly used when the number of iterations is known beforehand. The syntax of a for loop is structured as follows:

    for (initialization; condition; increment/decrement) {
        // Code to be executed
    }
    

    Example 1: Using a For Loop

    Let’s take a look at a simple program that uses a for loop to print numbers from 1 to 5:

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

    Explanation of the Program:

    Output:

    1  
    2  
    3  
    4  
    5  
    

    While Loops in Java

    The while loop is used when the number of iterations is not predetermined and continues until a specific condition evaluates to false. Its syntax is as follows:

    while (condition) {
        // Code to be executed
    }
    

    Example 2: Using a While Loop

    Below is an example of a while loop that achieves the same task as our previous for loop:

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

    Explanation of the Program:

    Output:

    1  
    2  
    3  
    4  
    5  
    

    Do-While Loops in Java

    A do-while loop is similar to a while loop, but it guarantees that the block of code will execute at least once, even if the condition is false from the start. The syntax is:

    do {
        // Code to be executed
    } while (condition);
    

    Example 3: Using a Do-While Loop

    Here’s an example using a do-while loop:

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

    Explanation of the Program:

    Output:

    1  
    2  
    3  
    4  
    5  
    

    Differentiating Between For, While, and Do-While Loops

    ControlStatments.png

    Practical Problem Solving with Loops

    Loops can be applied to various scenarios. For example, let’s create a program that calculates the factorial of a number using a for loop:

    Example 4: Factorial Calculation with For Loop

    public class FactorialExample {
        public static void main(String[] args) {
            int num = 5;
            int factorial = 1;
            for (int i = 1; i <= num; i++) {
                factorial *= i;
            }
            System.out.println("Factorial of " + num + " is: " + factorial);
        }
    }
    

    Explanation of the Program:

    Output:

    Factorial of 5 is: 120  
    

    Summary / Key Takeaways

    In this lesson on Loops in Java: For, While, Do-While, you have learned:

    By mastering loops, you will enhance your capabilities to write efficient and effective Java programs.

    Java Fundamentals For Beginners — Java Basics

    Introduction to Java

    Java Basics

    Java Basic Programs

    Understanding Java Classes and Methods

    Working with Strings

    Object-Oriented Programming (OOP) Concepts

    Exception & Error Handling

    File Handling (I/O Operations)

    Java Packages and Libraries

    Best Practices in Java Programming

    Building Simple Console-Based Applications

    Moving Forward: Advanced Topics

    Course Review and Final Project

    Tutorial Not Found