Loop Control Instructions

Introduction to Loops

Programs often need to repeat specific actions. The versatility of a computer lies in its ability to execute instructions repeatedly until a condition is met or a specified number of repetitions is completed. Loops provide this functionality and are a powerful tool for creating dynamic, repetitive processes in programming.

Types of Loops:

There are three primary types of loop control instructions in programming:

1.The while Loop

The while loop executes a block of code as long as the condition remains true. It’s suitable when you don’t know in advance how many times you need to repeat an action.

int count = 1;
while (count <= 3) { System.out.println("Count is: " + count); count++; }

This loop continues executing until the value of count exceeds 3.

Key Points about while loop:

  • The condition is tested before the loop body is executed.
  • The loop may run 0 times if the condition is false initially.
  • You can use logical and relational operators inside the condition, e.g., while (i >= 10 && j <= 15).

Example Program: Simple Interest Calculation

import java.util.*;
public class SimpleInterest {
public static void main(String[] args) {
float p, r, si;
int n, count = 1;
Scanner scn = new Scanner(System.in);

while (count <= 3) { System.out.println("Enter values of p, n and r:"); p = scn.nextFloat(); n = scn.nextInt(); r = scn.nextFloat(); si = p * n * r / 100; System.out.println("Simple interest = Rs. " + si); count++; } } }

2. The for Loop

The for loop provides a more compact form of looping by including initialization, condition, and increment/decrement in one line. It’s commonly used when the number of iterations is known in advance.

Syntax:

for (initialization; condition; increment) {
// loop body
}

for (int i = 1; i <= 3; i++) { System.out.println("Iteration: " + i); }

Example Program: Simple Interest Calculation with for

import java.util.*;
public class SimpleInterestFor {
public static void main(String[] args) {
float p, r, si;
int n;
Scanner scn = new Scanner(System.in);

for (int count = 1; count <= 3; count++) { System.out.println("Enter values of p, n and r:"); p = scn.nextFloat(); n = scn.nextInt(); r = scn.nextFloat(); si = p * n * r / 100; System.out.println("Simple Interest = Rs. " + si); } } }

Key Points about for loop:

  • Ideal for situations where the number of iterations is known.
  • You can initialize multiple variables in the initialization expression.
  • You can omit the initialization, condition, or increment parts for flexibility.

3. The do-while Loop

The do-while loop is similar to the while loop, but the condition is tested after the loop body is executed. This ensures that the loop body is executed at least once, regardless of the condition.

Syntax:

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

int count = 1;
do {
System.out.println(“Count is: ” + count);
count++;
} while (count <= 3);

Key Points about do-while loop:

  • The loop body executes at least once, even if the condition is false initially.
  • Useful when the loop needs to run at least once before the condition is checked.

Additional Loop Control Instructions

The break Statement

The break statement allows you to exit the loop prematurely, even if the loop condition is still true. It is often used with conditional statements to stop the loop based on a specific condition.

Example:

int i = 1;
while (i <= 10) { if (i == 5) { break; // Exits the loop when i equals 5 } System.out.println(i); i++; }

The continue Statement

The continue statement skips the remaining statements in the current iteration and jumps to the next iteration of the loop.

Example

for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skips the current iteration when i equals 3 } System.out.println(i); }

Nesting Loops

You can place one loop inside another, creating a nested loop. For every iteration of the outer loop, the inner loop will fully execute.

Example:

for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 2; j++) { System.out.println("i = " + i + ", j = " + j); } }

Multiple Initializations and Increments in for Loop

You can initialize and increment multiple variables within the for loop. For example:

for (int i = 1, j = 5; i <= 3; i++, j--) { System.out.println("i = " + i + ", j = " + j); }

Infinite Loops

If the condition of the loop is never false, it can result in an infinite loop, which continues indefinitely.

Example of Infinite Loop:

while (true) {
System.out.println(“This will run forever!”);
}