if (condition) { What you want to do if true }
You can also set the processing when false. In that case,
if (condition) { What you want to do if true }else{ Process you want to execute when false }
for (initial value; condition range; change expression) { Content you want to process repeatedly }
I wrote the following code in the process of the task of squared only even numbers. ↓ for(i = 0; i <= 20; i++){ if(i % 2 == 0){ System.out.printf("%d * %d = %d", i, i, i*i).println(); }} 0 * 0 = 0 2 * 2 = 4 4 * 4 = 16 6 * 6 = 36 8 * 8 = 64 10 * 10 = 100 12 * 12 = 144 14 * 14 = 196 16 * 16 = 256 18 * 18 = 324 20 * 20 = 400
Originally, the following contents are sufficient for(i = 0; i <= 20; i+=2){ System.out.printf("%d * %d = %d", i, i, i*i).println(); } 0 * 0 = 0 2 * 2 = 4 4 * 4 = 16 6 * 6 = 36 8 * 8 = 64 10 * 10 = 100 12 * 12 = 144 14 * 14 = 196 16 * 16 = 256 18 * 18 = 324 20 * 20 = 400
Allows you to write with optimized code.
Recommended Posts