Iterative loops in java - ProgrammerTech
Language:

Search

Iterative loops in java

  • Share this:
Iterative loops in java

The concept of iterative loops in Java

Loops In Java, we use loops in programming in general to make it easier for us not to write the same code multiple times.

Repeat commands are used to repeat a specific command or to print a set of data once. For example, if we want to print the sentence "Programmer Tech" a hundred times, is it reasonable to write a hundred lines to print this sentence a hundred times? Of course, this is very tedious and tiring, for this matter when we want to print an amount of data or information. And he needs to show it in several lines in this case. In order to save time and effort, we will use repetition commands.

In the event that we want to use the code several times, we put it in an iterative loop, and it will, in turn, repeat it according to our request and the terms of the code. Commands are usually executed sequentially after each other, but iterative loops make the order execution arrow stop there. It executes the commands inside these loops several times, and after it exits the iterative loop, it returns and completes the implementation of the rest of the existing commands.

 

Types of loops in Java

  1. for loop.
  2. while loop.
  3. do while loop.

Each episode of this we use in a specific work in the program.

 

For Loop in Java

For loop, we use the for loop command to iterate a set of data or a set of lines at once. The for command will save time and effort.

The general form of a for loop in Java

for(initialization; Boolean_expression; inc)

The meaning of initialization is the beginning of the counter meaning from which number the iteration will start.

And the meaning of this Boolean_expression is a condition of when the iterator or counter will end. For example, in the first we start from 0, and then we put a condition that ends with 10.

The meaning of inc is the command to increase the counter. I mean, we want the counter to be increased by one or two in each cycle, and this is also an optional matter.

 

example 1
package javaapplication22;
public class JavaApplication22 {
    public static void main(String[] args) {
        int i;
        for(i=0;i<10;i++)
            System.out.println("Programmer Tech");
    }
}

We define the counter with the variable i, then we start the counter with the word for and after that i=0 means that the counter starts from zero and then we close it with simi-coln;. Then we set the condition i<10 that it repeats this thing 10 times and then close it;. Then i++ means the amount of increment is 1 per cycle. Then we set that it prints the word "Programmer Tech" in this example, and when you run the program, it will print it ten times.

 

example 2

If we want the program to repeat more than one sentence for printing, we will take the same previous example and add to it.

package javaapplication22;
public class JavaApplication22 {
    public static void main(String[] args) {
        int i;
        for(i=0;i<10;i++)
        {
            System.out.println("Programmer Tech");
            System.out.println("CS SAJO");
        }
    }
}

With the same explanation as the first example, we just added the commands that are printed within brackets {} to be considered like a block. If we do not press the block parenthesis, it will print only the first sentence below the recursion function and print the second sentence only once at the end.

 

example 3

Write a iterating loop that prints all numbers from 1 to 15 using the for loop.

package javaapplication22;
public class JavaApplication22 { 
    public static void main(String[] args) {
        for( int i=1; i<15; i++)
        {
            System.out.println( i );
        }
    }
}

We have created an iteration loop in which the counter starts at 1 and ends at 15, and in each cycle it increases by 1 until it ends. For more information about using the for loop, you can enter the Hermash website from the following link to be transferred to the for loop cycle in Java.

 

while Loop in Java

The while loop in Java is an iteration command, which is very simple and beautiful.

The general form of a while loop in Java

while (condition)
{
//
}

We write as we write the condition within the parentheses and the condition the stop condition means that the loop continues to rotate until the condition is fulfilled. And within the brackets we put the thing we want to repeat. It always means while reading java in many commands like reading an array or when searching in a specific command.

 

example 4

Write a program that prints the word Programmer Tech ten times using the while loop.

package javaapplication22;
public class JavaApplication22 { 
    public static void main(String[] args) {
        int count=0;
        while(count<10)
        {
            System.out.println("Programmer Tech");
            count++;
        }
    }
}

We created a variable count for the beginning of the counter that starts from 0 and we set the condition as long as the variable count is less than 10, do the print command for me. And the command to print is the word Programmer Tech, and in each cycle, add 1 to it until the condition is met.

 

example 5

Write a program that prints numbers from 1 to 5 through a while loop in Java.

package javaapplication22;
public class JavaApplication22 {
    public static void main(String[] args) {
        int i=1;
        while(i<=5)
        {
            System.out.println( i );
            i++;
        }
    }
}

We write a variable i for the start of the counter, which is 1 and within the loop we set the condition as long as i is less than 5, type i and increase 1 on the operation until it reaches the number 5 and the loop stops.

 

What is the meaning of Factorial in Java?

The Factorial means equal to the factorial, and it is a mathematical operation that multiplies the number by the numbers that are less than it until we reach 1. For example, we want to find the factorial of the number 6, we multiply it by itself, and by numbers less than it, we multiply it by itself. Then in 5, then in 4, then in 3, then in 2, then in 1, so we get the result of the example, which is 720 and the operation is.

6=6*5*4*3*2*1=720

 

example 6

Find the factorial of 5 in the while loop using Factorial in Java.

package javaapplication22;
public class JavaApplication22 {
    public static void main(String[] args) {
        int fac=5;
        int count=1;
        while(fac>1)
        {
            count=count*fac;
            fac--;
        }
        System.out.println("Factorial=" + " " + count);
    }
}

The first variable we defined the factorial fac = 5 and the second variable the definition of the counter starts from 1 and we entered the condition of the loop keep rotating as long as fac is greater than 1. Multiply the variable count by the variable fac and then move to the number before it -- ie decreasing the factorial in each cycle. until it reaches 1 and stores the result in count, and the print command prints the result after the word Factorial=.

 

do while loop in java

We will deal with one of the recurring commands used, which is do while, which is similar to the while command and similar to the for command. A while ago, we talked about while, and its task was to do the check and then the execution, and the do while task is the exact opposite, it performs and then checks.

The general form of a do while loop in Java

do{
    //the code we want
}while(stop condition)

Many of you may be wondering which one to use while or do while, and we said earlier that while is more used and loved by programmers, and do while is less used. We mentioned do while within this session as a matter of comprehensiveness only so that there is no shortage in the session, but we do not say that do while we do not use it. There are cases where do while should be used, such as cases that require execution and then checking, and in the case of checking and then executing, we use while. And you may need in some cases of programming or go through some cases and want to start executing before the examination. The error rate in while is 0%, while in do while it is at least 1% because it is not checked before execution, perhaps after execution an error occurs before execution.

 

example 7
package javaapplication22;
public class JavaApplication22 {
    public static void main(String[] args) {
        int count=10;
        do
        {
            System.out.println("Programmer Tech");
            count--;
        }while(count>0);
    }
}

We define a scalar variable count=10 with a value of 10. Then we enter the do while loop between parentheses. The do while will print the Programmer Tech statement and decrement the variable 10 times. Because we set the condition that it persists as long as the variable is greater than 0 and it will spin 10 times and each time it prints the sentence "Programmer Tech". It will decrement the variable until its value becomes 0 and the condition is satisfied and exits the loop.

 

example 8
package javaapplication22;
public class JavaApplication22 {
    public static void main(String[] args) {
        int i=1;
        do
        {
            System.out.println( i );
            i++;
        }while(i<=5);
    }
}

We create the variable i=1 to start the counter from this number. And we put it in the do while loop and print the numbers from 1 to 5. With each cycle, 1 increases if the variable i is less than or equal to 5 and the condition is fulfilled and stops.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com