Hello developers!!
Today I practiced Java loops with the help of my senior, Neelakandan, who gave me tricky logic-based questions. These questions were not from textbooks - they were based on real-time thinking. That helped me understand the loop concept very clearly.
So in this blog, I’m going to share 4 Java loop codes with real-world scenarios, clear logic, and code explanation. Let’s dive in!
Scenario 1: Count Repeated Value (Like Survey Feedback)
Inspired by Neelakandan brother's question to test repetition counting using while loop.
Real-world Scenario:
Imagine a survey where users selected different values. We want to count how many times a particular value (like "70") was selected.
Code
:
public class Main {
public static void main(String[] args) {
int key = 70;
int[] num = {1, 2, 70, 3, 70, 4, 70};
int count = 0;
int i = 0;
while (i < num.length) {
if (num[i] == key) {
count++;
}
i++;
}
System.out.println(count);
}
}
Output:
3
Explanation:
The loop checks each value in the array. If the number matches the key
(70), it increases the count
. We used a while loop
to scan the array one by one.
Scenario 2: Find Big Number in a List (Like Scoreboard Highest Score)
Real-world Scenario:
You have a list of scores. You want to identify the highest score while the list is being read one by one.
Code
:
class Main {
public static void main(String[] args) {
int[] a = {2, 4, 30, 1, 40, 70, 100, 10};
int big = a[0];
int i = 0;
while (i < a.length) {
if (a[i] > big) {
big = a[i];
System.out.println(big);
}
i++;
}
}
}
Output:
4
30
40
70
100
Explanation:
We start with the first number as the biggest. Then, in the loop, we compare and update it if we find a bigger one. It prints whenever a new big value is found.
Scenario 3: Dosa Offer Balance Calculation (Like Cashback System)
This idea was a fun one given by Neelakandan - a dosa shop balance increasing by offer logic.
You have ₹12 and you go to a dosa shop that gives 50% cashback every time you eat. Do this 3 times. What is your final balance?
Code
:
class Main {
public static void main(String[] args) {
int balance = 12;
int i = 1;
int temp;
while (i <= 3) {
temp = balance / 2;
balance = balance + temp;
i++;
}
System.out.print(balance);
}
}
Output:
42
Explanation:
Each time we divide the current balance by 2 and add it back. This repeats 3 times. Perfect use of while loop
to simulate limited offer cycles.
Scenario 4: Gold Value Cutting Down (Like Daily Price Halving)
Simulate a gold value decreasing over time.
Gold value starts at ₹3000. Each day, the value reduces to half of the previous day's value. Do this for 7 days.
Code
:
public class Main {
public static void main(String[] args) {
int value = 3000;
int i = 1;
int temp;
while (i <= 7) {
temp = value / 2;
value = temp;
System.out.println(value);
i++;
}
}
}
Output:
1500
750
375
187
93
46
23
Explanation:
This code uses a while loop
to cut the value into half each day. It's a great example of progressive reduction using loops.
Key Points:
These 4 examples helped me truly understand how powerful loops are when solving real-time problems. Special thanks to Neelakandan Brother for pushing me to think like a developer instead of just writing code.
Every loop has a story:
-
for loop
= fixed steps. -
while loop
= unknown steps. - Logic = apply your brain , not just your fingers.
Every line of code teaches something new!.
Top comments (0)