Lecture Manual: Java Loops
1. Introduction to Loops
In programming, loops are used to execute a block of code multiple times until a specified condition is met.
Java provides three types of loops:
- For Loop
- While Loop
- Do-While Loop
Loops help in reducing redundancy and making programs efficient.
2. Types of Loops in Java
2.1 For Loop
The for loop is used when the number of iterations is known beforehand.
Syntax:
for(initialization; condition; update) {
// Code to execute
}
Example: 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("Number: " + i);
}
}
}
MIS 210: Introduction to Computer Programming
2.2 While Loop
The while loop is used when the number of iterations is not known and depends on a condition.
Syntax:
while(condition) {
// Code to execute
}
2.3 Do-While Loop
The do-while loop executes at least once before checking the condition.
Syntax:
while(condition) {
// Code to execute
}
3. Nested Loops
A loop inside another loop is called a nested loop.
Example: Print a multiplication table using nested loops.
public class MultiplicationTable {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print(i * j + "\t");
}
System.out.println();
}
}
}
MIS 210: Introduction to Computer Programming
4. Break Statement
The break statement is used to exit a loop prematurely when a specific condition is met.
Example: Stop printing numbers when i equals 3.
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println("Number: " + i);
}
}
}
5. Pattern Printing and Simple Series Using For Loops
5.1 Sum of First N Natural Numbers
public class SumOfNumbers {
public static void main(String[] args) {
int n = 10, sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
System.out.println("Sum of first " + n + " natural
numbers: " + sum);
}
}
5.2 Product of First N Natural Numbers (Factorial)
public class Factorial {
public static void main(String[] args) {
int n = 5, product = 1;
for (int i = 1; i <= n; i++) {
product *= i;
}
System.out.println("Factorial of " + n + " is: " +
product);
}
}
MIS 210: Introduction to Computer Programming
5.3 Print a Right-Angled Triangle Pattern
public class TrianglePattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
5.4 Sum of Even Numbers Divisible by 3 up to a Given Limit
import java.util.Scanner;
public class SumEvenDivisibleByThree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the limit N: ");
int N = scanner.nextInt();
int sum = 0;
for (int i = 2; i <= N; i += 2) { // Loop through even numbers only
if (i % 3 == 0) { // Check divisibility by 3
sum += i;
}
}
System.out.println("Sum of even numbers divisible by 3 up to " + N + " is: " + sum);
scanner.close();
}
}
MIS 210: Introduction to Computer Programming