JAVA PROGRAMMING
Lab Manual — Loops & Iteration
Subject while loops, for loops, nested loops, do-while, iteration
Level Beginner to Intermediate
Tasks 6 Fully Solved Tasks with Corrected Code & Output
Concepts Scanner, while, for, nested loops, sentinel values, Random
Language Java
Format Study Reference / Lab Guide
Disclaimer: This document is intended as a study reference and learning guide only. All solutions are original
work. Please use responsibly for educational purposes.
Original Work | For Educational Use Only
, Java Programming Lab Manual — Loops & Iteration Page 2 | Original Work | For Educational Use Only
Introduction to Java — Loops & Iteration
This lab manual covers Java loop constructs — the backbone of repetitive logic in programs. You will practice
while loops for condition-driven repetition, for loops for counted iteration, nested loops for 2-D patterns, and
sentinel-controlled loops that exit on a special value. Each task is fully solved with corrected code, expected
output, and a step-by-step explanation.
Core Concepts at a Glance
Construct Syntax Use Case
while loop while(condition){ ... } Repeat while condition is true
for loop for(int i=0; i<n; i++){ ... } Counted / indexed iteration
nested loop for loop inside for loop Grids, triangles, patterns
do-while do{ ... }while(condition); Execute at least once
sentinel while(val != STOP){ ... } User-controlled loop exit
Random new Random(); rand.nextInt(n)+1 Generate random integers
What You Will Learn
■ Use while loops to repeat a sum operation until the user says no.
■ Build a password-checker with limited attempts and account-lock logic.
■ Print rectangular grids using nested for loops.
■ Print number-triangle patterns using nested loops and row counters.
■ Simulate a dice-guessing game with Random and while loop.
■ Accumulate a running score total using a sentinel-controlled while loop.
Original Work | For Educational Use Only