In this article we will discuss how to use for loop with two variables.

For loop is basic feature we use in programming. In it we use a variable and keep on increasing or decreasing it till a condition is matched.

But many times a scenario comes where we want to increment or decrement two variables instead of one.

For example, I want to do for loop with 2 variable i.e.

  • i that will increment from 0 to 9
  • j that will decrement from 10 to 1

Here condition till which loop will run is (i < 10 && j > 0)

Let’s see how to do that,

For Loop with two variables in C++

 

For Loop with two variables in Java

Output for the above programs is,

 

Click Here to Subscribe for more Articles / Tutorials like this.