Java Control Structures - Loops Introduction

java loops

Loops are control structures that allow you to execute a piece or pieces of code repeatedly.

Thorugh loops you can cycle through a section of code multiple times as long as a condition is true.

Importance of Loops

Loops are a vital part of imperative programming and one of the fundamental aspects of programming a computer.

No. Advantage
1. Avoidance of repetitive coding and redundancy. Loops allow us execute sections of code repeatedly without having to retype those sections of code multiple times. Imagine if you wanted to print out numbers from 1 to 1000 and have to type a thousand statements to print each number in that range, it would be hectic isn't it. Well with loops we don't have to do so.The end result of this code easy to maintain.
2. Flexible Loops allow us specify the condition that has to be met and stop automatically if that condition evaluates to false. This shows they are flexible enough for almost anything we want.
3. Easy Loops are easy to write and only require one or two lines of code.

Types of Loops

These are the types of loops in java:

  1. For Loop
  2. Enhanced For Loop.
  3. While Loop.
  4. Do While Loop.

1. For Loop

A for loop is a type of loop that executes a block of code over a range of values.

For loops typically use an index to keep track of the loop.

######## For Loop Syntax Here's the syntax of a for loop:

for (/*Initialization*/; /*Termination*/; /*Increment*/)
    /*execute these statements*/
}
Examples

######## For Loop with an Array Here's an example:

/
 * Simple For Loop example
 */
public class ForLoop {

    public static void main(String[] args) {
        String[] nebulae={"Horse Head Nebula","Orion Nebula","Black Widow Nebula","Ant Nebula","Witch Head Nebula","Own Nebula","Ring Nebula"};

        for(int i=0;i<nebulae.length;i++)
        {
            System.out.println(nebulae[i]);
        }
    }

}

Result:

Horse Head Nebula
Orion Nebula
Black Widow Nebula
Ant Nebula
Witch Head Nebula
Own Nebula
Ring Nebula

2. Enhance For Loop

An enhanced for loop is a type of for loop that was introduced to be used with iterable objects. These include:

  1. Arrays.
  2. ArrayLists.
  3. Collections etc.
Difference between For loop and Enhanced For loop
  • Standard For loop uses an index to keep track of an array while Enhanced For loop uses an iterator.

The iterator doesn't require the initializer, termination or increment sections.

Here's an example:

public class EnhancedForLoop {

    public static void main(String[] args) {
        String[] nebulae={"Horse Head Nebula","Orion Nebula","Black Widow Nebula","Ant Nebula","Witch Head Nebula","Own Nebula","Ring Nebula"};

        for(String nebular:nebulae)
        {
            System.out.println(nebular);
        }
    }
}

Result

Horse Head Nebula
Orion Nebula
Black Widow Nebula
Ant Nebula
Witch Head Nebula
Own Nebula
Ring Nebula

3. While Loops

A while loop is based on meeting a certain codition rather than iterating a number of times.

While Loop Syntax

Here's the syntax for while loop:

while (/*conditional expression*/) {
    /*execute these statements*/
}

While loops first check to ensure the conditional expression evaluates to true before it proceeds with each iteration. It does this check after each iteration.

While Loop Example
/
 * While Loop example
 */
public class WhileLoop {

    public static void main(String[] args) {
        String[] nebulae={"Horse Head Nebula","Orion Nebula","Black Widow Nebula","Ant Nebula","Witch Head Nebula","Own Nebula","Ring Nebula"};
        int i=0;
        while (i < nebulae.length) {
            System.out.println(nebulae[i]);
            i++;
        }
    }
}

RESULT

Horse Head Nebula
Orion Nebula
Black Widow Nebula
Ant Nebula
Witch Head Nebula
Own Nebula
Ring Nebula

4. Do While Loop

This is an alternate to While Loop. It's similar to while loop however, unlike while loops which evaluate the conditional expression first, the do while loop executes the block of code first before checking the conditional expression.

If the conditional expression executes to true, it repeats, then checks again and so on.

So a Do While Loop will atleast execute the code block at least once unlike a while loop.

Do While Syntax

Here's the do while loop syntax:

do {
    /*execute these statements*/
} while (/*conditional expression*/);
Do-While Loop Example

Here's an example:

/
 * Do-While Loop example
 */
public class DoWhileLoop {

    public static void main(String[] args) {
        String[] nebulae={"Horse Head Nebula","Orion Nebula","Black Widow Nebula","Ant Nebula","Witch Head Nebula","Own Nebula","Ring Nebula"};

        int i=0;

        do {
            System.out.println(nebulae[i]);
            i++;
        } while (i < nebulae.length);
    }

}

Result:

Horse Head Nebula
Orion Nebula
Black Widow Nebula
Ant Nebula
Witch Head Nebula
Own Nebula
Ring Nebula

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post