How to use for statement in Java

3 Answers

0 votes
public class Example {
    public static void main(String[] args) {
             
         for (int i = 1; i <= 10; i++) {
             System.out.println(i);
         }
    }
}
 
 
 
/*
run:
  
1
2
3
4
5
6
7
8
9
10
  
*/

 



answered Jan 13, 2016 by avibootz
edited Aug 13, 2022 by avibootz
0 votes
public class Example {
    public static void main(String[] args) {
             
        for (int i = 10; i > 0; i--) {
              System.out.println(i);
        }
    }
}
 
 
 
/*
run:
  
10
9
8
7
6
5
4
3
2
1
  
*/

 



answered Jan 13, 2016 by avibootz
edited Aug 13, 2022 by avibootz
0 votes
public class Example {
    public static void main(String[] args) {
             
        for (int i = 0, j = 10; i < 10; i++, j--) {
            System.out.println(i + " " + j);
        }
    }
}
 
 
 
/*
run:
  
0 10
1 9
2 8
3 7
4 6
5 5
6 4
7 3
8 2
9 1
  
*/

 



answered Jan 13, 2016 by avibootz
edited Aug 13, 2022 by avibootz

Related questions

1 answer 157 views
1 answer 154 views
1 answer 208 views
1 answer 87 views
2 answers 236 views
2 answers 286 views
...