Increment for Loop by 2 in Java

In this post, we will see how to increment for loop by 2 in Java.

There are several looping statements available in Java and one of them is for loop in java.

There are three parts of for loop.

  • Initialization
  • Condition
  • Increment or decrement

Initialization: Initialization statement executes at start of loop only one time.
condition:Condition gets evaluated in each iteration. For loop executes block of statements in loop unless condition returns false.
Increment/Decrement: These statements get executed in each iteration.

How to increment for loop by 2 in Java

To Increment for loop by 2 in Java, we are interested in Increment/Decrement part of for loop.

We need to use i+=2 in case we need to increment for loop by 2 in java.

Let’s say we want print all even numbers between 1 to 20 in Java.

We can write a for loop and start the loop from 2 by using i=2 in initialization part and increment the loop by 2 in each iteration by using i+=2.

Here is the program:

Output

How to increment for loop by n in Java

We can use similar logic in case you want to increment for loop by n i.e. 2,3,4,5. We need to put i+=n in increment/decrement part of the for loop.

Here is simple program where we will increment for loop by 3.

Output

That’s all about how to increment for loop by 2 in Java.

Was this post helpful?

Related Posts

  • 29 November

    How to Get Variable From Another Class in Java

    Table of ContentsClasses and Objects in JavaAccess Modifiers in JavaGet Variable From Another Class in JavaUsing the Default or Public Access Modifier of the Other ClassUsing the Static Member of Another ClassUsing the Inheritance Concept of JavaUsing the Getters and Setters of Another ClassUsing the Singleton Pattern Design for Declaring Global VariablesConclusion In this article, […]

  • 20 September

    Return ArrayList in Java

    Table of ContentsReturn ArrayList in Java From a Static MethodReturn ArrayList in Java From a Non-static MethodConclusion This article discusses cases of returning an ArrayList in Java from a method. An ArrayList in Java is a collection of elements of the same data type under a single variable name. In different cases, you can return […]

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusion An object in Java is an instance of a class. It is a real entity existing […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in Java In this post, we will see how to print multiple […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusion In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can be confused with […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.