For loops are iterative statements that repeat a certain block of code , on the other had, if statements are the conditional statements that perform decision making situations. You cannot go from one to other as just as you can’t go from repeating stuff and making decisions in most cases.
However, if you wanna repeat the code block, there are two most obivious things that you wanna do while repeating:
- either repeat and check for different years; getting the years
either from the user or from some other source. - or repeat the code and check for each consecutive year
In the case 2, you can actually merge some stuff into the for loop
Case 1:
for(int counter = 0; counter < n; counter++){
// get year data from the console or from other sources
if (year%4==0 || year%100==0 && !not)
System.out.println (year + " is a leap year");
else
System.out.println (year + " is not a leap year");
}
}
Case 2:
for (int year = 2000; year <= 2100; year++) {
//checking each year from 2000 to 2100
if (year%4==0 || year%100==0 && !not)
System.out.println (year + " is a leap year");
else
System.out.println (year + " is not a leap year");
}
}
There are other possible things to do with loops and conditions as well
solved How do you convert an if else statement into a for loop? [closed]