Post: need help understanding increments in for loops
12-22-2017, 10:34 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); why does this code skip the number 2 when counting?
    public class Program
{
public static void main(String[] args) {
int result = 0;
for (int i = 0; i < 5; i++) {
if (i == 3) {
result += 10;
} else {
result += i;
}
System.out.println(result);
}
}
}
12-29-2017, 09:28 AM #2
Sloth
Banned
Originally posted by mudville209 View Post
why does this code skip the number 2 when counting?
    public class Program
{
public static void main(String[] args) {
int result = 0;
for (int i = 0; i < 5; i++) {
if (i == 3) {
result += 10;
} else {
result += i;
}
System.out.println(result);
}
}
}

Your code doesn't skip 2 at all take a look at this I compiled it online and just printed out i and the result through each iteration Smile
You must login or register to view this content.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo