Loop Statements

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 24:- Loop Statements

In Java, loop statements allow you to execute a block of code repeatedly based on a certain condition. There are three types of loop statements in Java:

  1. while loop:

    • The while loop repeatedly executes a block of code as long as the given condition is true. It checks the condition before each iteration.

    javaCopy code

    while (condition) {    // Code to be executed }

  2. do-while loop:

    • The do-while loop is similar to the while loop, but it checks the condition after each iteration. This guarantees that the block of code is executed at least once.

    javaCopy code

    do {    // Code to be executed } while (condition);

  3. for loop:

    • The for loop is typically used when you know the number of iterations in advance. It consists of three parts: initialization, condition, and iteration expression, separated by semicolons.

    javaCopy code

    for (initialization; condition; iteration) {    // Code to be executed }

Here are some examples to demonstrate each loop:

  1. while loop example:

javaCopy code

int count = 1; while (count <= 5) {    System.out.println("Iteration " + count);    count++; }

Output:

Copy code

Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5

  1. do-while loop example:

javaCopy code

int count = 1; do {    System.out.println("Iteration " + count);    count++; } while (count <= 5);

Output:

Copy code

Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5

  1. for loop example:

javaCopy code

for (int i = 1; i <= 5; i++) {    System.out.println("Iteration " + i); }

Output:

Copy code

Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5

Loop statements are powerful tools for automating repetitive tasks and iterating over data structures such as arrays and collections. They are commonly used in programming to make code more efficient and concise.

6. Loops

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support