Loops in Java

Kandadi ManasaKandadi Manasa
3 min read

Loops are used to repeatedly execute a block of code as long as a specified condition is true.

Adaina pani manam repeated ga cheyali ankuna dani loop use cheskoni chestham.

Example: thinadam, padukovadam

Java loops 4 types, e article lo 3 basic types chudham manam:

  1. For Loop

  2. While Loop

  3. Do-While Loop

1. For Loop

The for loop is used when you know in advance how many times you want to execute a statement or a block of statements.

Bigboss laga udhayam nunchi m panlu chesthamo telusthe for loop use chestham.

To be precise mana loop varaible ki starting point inka ending point telisinapudu manaki for loop best ga use avthadi.

Syntax:

for (initialization; condition; update) { 
    // code to be executed
}

Example: son -> Mummy nuvu 1st class lo numbers adguthe chepale, epudu oka line lo anni chepestha

public class Main {
    public static void main(String[] args) {
        // Print numbers from 1 to 5
        for (int i = 1; i <= 5; i++) {
            System.out.println("Number: " + i);
        }
        System.out.println("Very good..10 rupees thisko");
    }
}

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Very good..10 rupees thisko

2. While Loop

The while loop is used when you do not know how many times the loop will run, and you want to execute a block of code as long as the condition is true.

Mana Loop lo either manaki start ledha end point adaina okate telusu, and based on condition manam e loop ni use chestham. example code chudandi..

Syntax:

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

Example: Dora bujji going from home to destination, without knowing how far is destination point.

๐Ÿ  -> park๐Ÿž๏ธ -> pond ๐ŸŒŠ-> gunta nakka ๐ŸฆŠ -> nanama ellu

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String location = "home";
        // focus on while loop
        while(!location.equals("nanama house")){
            System.out.print("next point map ni adigi telusukundam: ");
            location = sc.nextLine();
        }
        System.out.println("manam vachesam!");
    }
}

Output:

home
next point map ni adigi telusukundam: park
next point map ni adigi telusukundam: pond
next point map ni adigi telusukundam: fox
next point map ni adigi telusukundam: nanama house
manam vachesam!

3. Do-While Loop

The do-while loop is similar to the while loop, but it guarantees that the code block is executed at least once since the condition is checked after the code block is executed.

E loop lo statement execute chesi, condition check chestham so e loop oka sari aina execute avuthundi even the condition is false.

Syntax:

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

Example:

public class Main {
    public static void main(String[] args) {
        int i = 1;
        do {
            System.out.println("Number: " + 1);
            i++;
        } while (i <= 0);
    }
}

Output:

Number: 1

Thank you..! Article ni like chesi vellandi.

2
Subscribe to my newsletter

Read articles from Kandadi Manasa directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kandadi Manasa
Kandadi Manasa