Header Ads Widget

Ticker

6/recent/ticker-posts

Java For Loop with Simple Example - Simple Way to Learn

Java For Loop with Simple Example - Simple Way to Learn

Friends, I wanted to teach pattern programs from Java Loops. But most people don't know much about loops. They just remember the loops and after a while forget the steps of the loops in them. You should read this post thoroughly, only then the problems in your for loop will disappear and you will become an expert.

Even if you don't know about this for loop, you can get complete information by reading the post. Here you will learn how to do Dry-Run.

Java Loop Overview

The concept of loops is very old in programming. If you have learned c and c ++, you have seen different loops in them too. The only difference is that, with the slightest change in the concept of loops, new loops came in the latest programming languages.

In programming, the loops concept is included in the control statement. This includes more if-else statements, break statements, continue statements. The goto statement has been removed from java. Control statements are used to change the flow of a program or to achieve something.

What is a loop in java?

Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true.

Types of loops in java?

In Java, there are four kinds of loops which are – the for loop, the while loop,  the do-while loop, and the for-each loop. All these four-loop constructs of Java execute a set of repeated statements as long as a specified condition remains true. This particular condition is generally known as loop control.

In this post, you will only see the for loop with a simple example. The next post will contain information about the remaining 1 loop (for-each loop java) with examples. As well as. You can learn the java-do-while loop by clicking here.

4 Easy Ways to Become an Expert in Java Loops?
  1. To know the syntax of loops.
  2. Understanding the flow of the loop.
  3. To do Dry Run.
  4. To practice.
I am going to teach these four options below. Read everything carefully and practice. Comment if you have any problems.

Java For Loop Overview

The for loop is also the same as the do-while or while loop, but it is more compact syntactically. The for loop executes a group of statements as long as the condition is true.  


Syntax of a for loop in java
for(expression 1; expression 2; expression 3 ){
    statements;
}

What are the 4 steps in the  Java for loop?

Loops have 4 steps, the loops are made up of steps. The structure of the steps in each loop is different. Next, we will see the design of the for loop with an example.

1. Variable Initialization (Expression 1):
From where you want to start your program. Take the datatype and initialize that value.

2. Condition (Expression 2):
You will need to set boundaries for where your program will end. These boundaries are written with the help of the relational operator in JAVA.

3. Statements:
When your loop starts, all the operations you want to do have to be written in the statement block. We will see that in the next example.

4. Increment / Decrement (Expression 3):
The value given to the starting point is taken from the end to the end by the Increment and Decrement operators. Suppose starting number is given as 1 and it has to be increased to 10, then it can be done with the help of the increment operator.


The first variable initialization occurs in a for a loop. After that the condition is checked, then the statements are executed. And lastly, increment, decrement.

Java For Loop with Simple Example

To understand the preceding syntax, let us take an example.
 class ForLoopDemo{ 
    
    public static void main(String[] args) {    
                          
        for(int i=1; i<=5; i++) {    

            System.out.println(i);    
        }

    }
}
Output :                                                                                                                                                       
Java For Loop with Simple Example - Simple Way to Learn


Please compare the expressions in the preceding for loop example. The first expression represents an initialization expression ( int i=1). The second one is a conditional expression ( i<=5). As long as this condition is true, the statements inside for loop are executed. The third expression is a modifying expression (i++). It may increment or decrement the value of the variable. 

Java For Loop Program Dry Run

If the program in the loop does Dry Run, then the concept of your loop will become more solid. Dry running is considered to be a good habit of programmers.

First I will write the steps and show how the number changed. Then I will add a photo from which you will know how to dry run on the book.

Step 1:
The condition i<=5 is tested. As the condition is true, then the flow of execution will go under the for loop. And the value of i, i.e. 1 so it is displayed the first time. Then the i value is incremented and becomes 2.

Step 2:
The condition i<=5 is tested. As the condition is true, then the flow of execution will go under the for loop. And the value of i, i.e. 2 so it is displayed the second time. Then the i value is incremented and becomes 3.

Step 3:
The condition i<=5 is tested. As the condition is true, then the flow of execution will go under the for loop. And the value of i, i.e. 3 so it is displayed the third time. Then the i value is incremented and becomes 4.

Step 4:
The condition i<=5 is tested. As the condition is true, then the flow of execution will go under the for loop. And the value of i, i.e. 4 so it is displayed the fourth time. Then the i value is incremented and becomes 5.

Step 5:
The condition i<=5 is tested. As the condition is true, then the flow of execution will go under the for loop. And the value of i, i.e. 5 so it is displayed the fifth time. Then the i value is incremented and becomes 6.

Step 6:
Then the condition i<=5 is tested. As the condition is false (value of i, i.e. 6), then the flow of execution will break it.

Program Dry Run:


NOTE - 
  • If you are going to write a single line after a for loop, it works even if no curly brace is given.

Java For Loop Examples for Practice

Now your Java For Loop problem will be solved. If there is still any problem, read the post carefully.

Now you want to practice For Loop. Here are some  For Loop examples, you want to solve. Many of these examples also ask the interviewer. Download the following file for its solution.

  1. Write a program to print numbers from 1 to 10.
  2. Write a program to calculate the sum of the first 20 natural numbers.
  3. Write a program to print numbers from 10 to 1.
  4. Write a program to find the given no is even or odd.
  5. Write a program to print the table of 5.




Yorum Gönder

0 Yorumlar