Loops in Java

Agenda

  • for loops

  • while loops

  • do-while loops

  • break statement

  • continue statement

For loop

Java for loop is used to execute a block a code a certain number of times.

It is recommended to use for loop when the exact number of iterations is known beforehand.

for (initialisation; condition; update) {
    // body of the loop
}

The syntax of for loop is:

Print numbers from 1 till 10

Print all integers from 1 to 10 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

class Main {
    public static void main(String[] args) {
        
    }
}

Print numbers from 1 till 10

Print all integers from 1 to 10 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

class Main {
    public static void main(String[] args) {
        for (<initialisation>; <condition> ; <update>) {
            ...
        }
    }
}

Print numbers from 1 till 10

Print all integers from 1 to 10 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

class Main {
    public static void main(String[] args) {
        for (int i = 1; <condition> ; <update>) {
            ...
        }
    }
}

Print numbers from 1 till 10

Print all integers from 1 to 10 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

class Main {
    public static void main(String[] args) {
        for (int i = 1; <condition> ; i++) {
            ...
        }
    }
}

Print numbers from 1 till 10

Print all integers from 1 to 10 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 10 ; i++) {
            ...
        }
    }
}

Print numbers from 1 till 10

Print all integers from 1 to 10 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

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

Largest of N numbers

Given an integer N i.e. the size of the input, followed by N more integers, find the largest of these N numbers.

 

Sample Input

5

10 -3 2 12 7

 

Sample Output

12

Largest of N numbers

Given an integer N i.e. the size of the input, followed by N more integers, find the largest of these N numbers.

 

Sample Input

5

10 -3 2 12 7

 

Sample Output

12

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

    }
}

Largest of N numbers

Given an integer N i.e. the size of the input, followed by N more integers, find the largest of these N numbers.

 

Sample Input

5

10 -3 2 12 7

 

Sample Output

12

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        for (int i = 1; i <= n; i++) {
            int current = sc.nextInt();
        }
    }
}

Largest of N numbers

Given an integer N i.e. the size of the input, followed by N more integers, find the largest of these N numbers.

 

Sample Input

5

10 -3 2 12 7

 

Sample Output

12

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int maxValue = Integer.MIN_VALUE;

        for (int i = 1; i <= n; i++) {
            int current = sc.nextInt();
        }

        System.out.println(maxValue);
    }
}

Largest of N numbers

Given an integer N i.e. the size of the input, followed by N more integers, find the largest of these N numbers.

 

Sample Input

5

10 -3 2 12 7

 

Sample Output

12

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int maxValue = Integer.MIN_VALUE;

        for (int i = 1; i <= n; i++) {
            int current = sc.nextInt();
            maxValue = Math.max(maxValue, current);
        }

        System.out.println(maxValue);
    }
}

The Java while loops is used to execute a specific block of code until a certain condition is met. 

The syntax of the while loop is:

While loops

while (condition) {
	// body of loop
}

Print all integers from 1 to 10 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

Print numbers from 1 till 10

class Main {
    public static void main(String[] args) {
        
    }
}

Print all integers from 1 to 10 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

Print numbers from 1 till 10

class Main {
    public static void main(String[] args) {
        int i = 1;   // Initialisation
        
    }
}

Print all integers from 1 to 10 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

Print numbers from 1 till 10

class Main {
    public static void main(String[] args) {
        int i = 1;   // Initialisation
        
        // Condition
        while (i <= 10) {
            // Body of the loop
        }
        
    }
}

Print all integers from 1 to 10 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

Print numbers from 1 till 10

class Main {
    public static void main(String[] args) {
        int i = 1;   // Initialisation
        
        // Condition
        while (i <= 10) {
            System.out.println(i);
        }
        
    }
}

Print all integers from 1 to 10 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

Print numbers from 1 till 10

class Main {
    public static void main(String[] args) {
        int i = 1;   // Initialisation
        
        // Condition
        while (i <= 10) {
            System.out.println(i);
            i++;    // Update
        }
        
    }
}

Count Digits of a Number

Given an integer N, count and print its number of digits

 

Sample Input

35493

 

Sample Output

5

Count Digits of a Number

Given an integer N, count and print its number of digits

 

Sample Input

35493

 

Sample Output

5

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        
    }
}

Count Digits of a Number

Given an integer N, count and print its number of digits

 

Sample Input

35493

 

Sample Output

5

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int digits = 0;

        while (/* condition */) {
            // body of loop
        }

        System.out.println(digits);
    }
}

Count Digits of a Number

Given an integer N, count and print its number of digits

 

Sample Input

35493

 

Sample Output

5

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int digits = 0;

        while (/* condition */) {
            num /= 10;
        }

        System.out.println(digits);
    }
}

Count Digits of a Number

Given an integer N, count and print its number of digits

 

Sample Input

35493

 

Sample Output

5

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int digits = 0;

        while (num > 0) {
            num /= 10;
        }

        System.out.println(digits);
    }
}

Count Digits of a Number

Given an integer N, count and print its number of digits

 

Sample Input

35493

 

Sample Output

5

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int digits = 0;

        while (num > 0) {
            num /= 10;
            digits++;
        }

        System.out.println(digits);
    }
}

The do-while loop is very similar to the while loop. However the body of do-while loop is executed once before the condition is checked.

 

The syntax of the do while loop is:

do-while loop

do {
    // body of loop
} while (condition);

Sum of positive numbers

Given a stream of numbers, read the numbers till you read a -ve integer and print their sum of numbers read so far.

 

Sample Input

5 3 2 -4 2 0 9 ...

Sample Output

6

 

Sum of positive numbers

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
    }
}

Given a stream of numbers, read the numbers till you read a -ve integer and print their sum of numbers read so far.

 

Sample Input

5 3 2 -4 2 0 9 ...

Sample Output

6

 

Sum of positive numbers

Given a stream of numbers, read the numbers till you read a -ve integer and print their sum of numbers read so far.

 

Sample Input

5 3 2 -4 2 0 9 ...

Sample Output

6

 

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        int num;
        
    }
}

Sum of positive numbers

Given a stream of numbers, read the numbers till you read a -ve integer and print their sum of numbers read so far.

 

Sample Input

5 3 2 -4 2 0 9 ...

Sample Output

6

 

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        int num;
        
        do {
            num = sc.nextInt();
        } while (/* condition */);
        
    }
}

Sum of positive numbers

Given a stream of numbers, read the numbers till you read a -ve integer and print their sum of numbers read so far.

 

Sample Input

5 3 2 -4 2 0 9 ...

Sample Output

6

 

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        int num;
        
        do {
            num = sc.nextInt();
        } while (num >= 0);
        
    }
}

Sum of positive numbers

Given a stream of numbers, read the numbers till you read a -ve integer and print their sum of numbers read so far.

 

Sample Input

5 3 2 -4 2 0 9 ...

Sample Output

6

 

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        int num;
        
        do {
            num = sc.nextInt();
            sum += num;
        } while (num >= 0);
        
    }
}

Sum of positive numbers

Given a stream of numbers, read the numbers till you read a -ve integer and print their sum of numbers read so far.

 

Sample Input

5 3 2 -4 2 0 9 ...

Sample Output

6

 

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        int num;
        
        do {
            num = sc.nextInt();
            sum += num;
        } while (num >= 0);
        
        System.out.println("Sum = " + sum);
    }
}

The break statement is used to terminate a loop it is enclosed inside. The program resumes control at the next statement following the loop.

break statement

class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i == 5)
                break;
            System.out.println(i);
        }
        System.out.println("After the loop");
    }
}

Output:

1
2
3
4
After the loop

The break statement is used to terminate a loop it is enclosed inside. The program resumes control at the next statement following the loop.

break statement

class Main {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 10) {
            if (i == 5)
                break;
            System.out.println(i);
            i++;
        }
        System.out.println("After the loop");
    }
}

Output:

1
2
3
4
After the loop

The continue statement is used in loop control to skip an iteration. It can be used in all types of loops.

continue statement

class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i % 3 == 0)
                continue;
            System.out.print(i + " ");
        }
    }
}

This program skips the print statement in every 3rd iteration.

Output:

1 2 4 5 7 8 10

 

Random Game

Write a program to generate random numbers between [1, 10] and print them to the output.

Terminate when you get 5.

 

Random Game

Write a program to generate random numbers between [1, 10] and print them to the output.

Terminate when you get 5.

 

class Main {
    public static void main(String[] args) {
        while (true) {
            int num = (int) (Math.random() * 10 + 1);
            System.out.print(num + " ");
        }
    }
}

Random Game

Write a program to generate random numbers between [1, 10] and print them to the output.

Terminate when you get 5.

 

class Main {
    public static void main(String[] args) {
        while (true) {
            int num = (int) (Math.random() * 10 + 1);
            if (num == 5)
                break;
            System.out.print(num + " ");
        }
    }
}

Random Game

Write a program to generate random numbers between [1, 10] and print them to the output.

Terminate when you get 5.

Skip all multiples of 4.

class Main {
    public static void main(String[] args) {
        while (true) {
            int num = (int) (Math.random() * 10 + 1);
            if (num == 5)
                break;
            System.out.print(num + " ");
        }
    }
}

Random Game

Write a program to generate random numbers between [1, 10] and print them to the output.

Terminate when you get 5.

Skip all multiples of 4.

class Main {
    public static void main(String[] args) {
        while (true) {
            int num = (int) (Math.random() * 10 + 1);
            if (num == 5)
                break;
            if (num % 4 == 0)
                continue;
            System.out.print(num + " ");
        }
    }
}
Made with Slides.com