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

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?