Core Java · Chapter 6 of 48
Loops
By the end of this chapter you can repeat work with for, while and do-while, steer a loop with break and continue, and run one loop inside another. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: First steps
- 6 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
The 6 am rush at SBC needs numbers fast. Ravi wants to know how many trains today's stock can serve before the urn runs short. Anjali wants a price table for the glass, so nobody does sums in the queue, and a stack of kulhads drawn on the display. Meera wants the till to find a chai and bun combo that fits a customer's budget exactly.
So far every line of your code runs once. A price table for three cups means three println lines; for ten cups, ten lines, and the number is fixed when you write the code. A stock of 100 cups or 10,000 cups needs a different number of steps, and you cannot know which when you write it.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
RushHour.java: count the trains the stock can serve
In RushHour.java, make trainsServed(stock, perTrain) return how many full trains the stock can serve, with a while loop, when each train takes perTrain cups (perTrain is more than 0). A train is served only in full, so stop when fewer than perTrain cups are left. No stock, or too little for one train, serves 0.
For example, trainsServed(100, 30) returns 3, trainsServed(90, 30) returns 3, trainsServed(20, 30) returns 0 and trainsServed(0, 30) returns 0.
- Task 2
RushHour.java: count the digits of a token number
In RushHour.java, make digits(token) return how many digits the token number has, with a do-while loop. The token is 0 or more, and 0 has one digit.
For example, digits(0) returns 1, digits(7) returns 1, digits(42) returns 2, digits(4096) returns 4 and digits(2147483647), the biggest int, returns 10.
- Task 3
RushHour.java: build a price table
In RushHour.java, make priceTable(price, upTo) return a price table with a for loop: one line for each quantity from 1 up to upTo, in order. A line is the quantity, then " x ₹", the price, " = ₹" and the quantity times the price. Every line ends with a newline, the last one too. upTo 0 returns the empty string.
For example, priceTable(40, 3) returns 1 x ₹40 = ₹40, 2 x ₹40 = ₹80 and 3 x ₹40 = ₹120, each followed by a newline; priceTable(25, 1) returns 1 x ₹25 = ₹25 and a newline.
- Task 4
RushHour.java: draw a stack of kulhads
In RushHour.java, make cupStack(rows) return a stack of cups with nested loops, one row per line. Row r, counting from 1, is rows minus r spaces, then r letters o with one space between them and no space at the end. Every line ends with a newline. rows 0 returns the empty string.
For example, cupStack(3) returns two spaces and o, then one space and o o, then o o o, each followed by a newline; cupStack(1) returns o and a newline.
- Task 5
RushHour.java: find the first combo for a budget
In RushHour.java, make comboFor(budget) return the first combo of masala chai at ₹40 each and bun maska at ₹50 each that costs exactly the budget, with 1 to 10 of each. Count chai from 1 to 10 in the outer loop and bun maska from 1 to 10 in the inner loop, and stop both loops at the first match with a labeled break. Return M chai + B bun maska, or No combo when nothing fits.
For example, comboFor(130) returns 2 chai + 1 bun maska and comboFor(400) returns 5 chai + 4 bun maska. 290 can be 1 chai + 5 bun maska or 6 chai + 1 bun maska: return the first found, 1 chai + 5 bun maska. comboFor(30) and comboFor(95) return No combo.
- Task 6 · interview question
Interview: RushHour.java, FizzBuzz Tapri style
In RushHour.java, make callOut(n) print the numbers from 1 to n, one per line, except that multiples of 15 print Chai Bun, other multiples of 3 print Chai and other multiples of 5 print Bun. Every line ends with a line break. n 0 prints nothing.
For example, callOut(5) prints 1, 2, Chai, 4 and Bun on five lines, and the 15th line of a longer call-out is Chai Bun.
What you will learn
- for: count from a start to an end, one round per number.
- while and do-while: repeat as long as a condition holds; do-while always runs once.
- break and continue: leave a loop, or skip the rest of one round.
- Nested loops and labels: a loop inside a loop, and a break that leaves both.
More in First steps
Build "Loops" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.