Allaka Yamuna lakshmi’s Post

🚀 Day 5/100 — Loops in Java 🔁 Loops allow a program to repeat a block of code multiple times without writing the same code again and again. They are one of the most important concepts in programming. Java mainly provides three types of loops: 🔹 1. for Loop Used when the number of iterations is known. Syntax: for(initialization; condition; update){ // code to run } Example: for(int i = 1; i <= 5; i++){ System.out.println(i); } Output: 1 2 3 4 5 Here: i = 1 → start value i <= 5 → condition check i++ → increment after each iteration 🔹 2. while Loop Used when the number of iterations is unknown and depends on a condition. Example: int i = 1; while(i <= 5){ System.out.println(i); i++; } The loop runs as long as the condition is true. 🔹 3. do-while Loop Runs the block at least once, even if the condition is false. Example: int i = 1; do{ System.out.println(i); i++; }while(i <= 5); Difference: while → condition checked before execution do-while → condition checked after execution 🔹 Nested Loops A loop inside another loop is called a nested loop. Commonly used for pattern printing problems. Example: Triangle pattern for(int i = 1; i <= 5; i++){ for(int j = 1; j <= i; j++){ System.out.print("* "); } System.out.println(); } Output: * * * * * * * * * * * * * * * 🔴 Live Example — Inverted Triangle Pattern int rows = 5; for(int i = rows; i >= 1; i--){ for(int j = 1; j <= i; j++){ System.out.print("* "); } System.out.println(); } Output: * * * * * * * * * * * * * * * 🎯 Challenge: Write a program to print an inverted triangle pattern using nested loops. Drop your code in the comments 👇 #Java #CoreJava #100DaysOfCode #JavaLoops #ProgrammingJourney

  • graphical user interface

To view or add a comment, sign in

Explore content categories