+ 1

Can anyone help me solve this java question

Write a Java program start with an integer n, divide n by 2 if n is even or multiply by 3 and add 1 if n is odd, repeat the process until n = 1.

25th May 2019, 2:29 PM
Tzion
Tzion - avatar
4 Answers
25th May 2019, 3:21 PM
Mark V Zephyrus
Mark V Zephyrus - avatar
+ 1
I could've put for(<initializations>;<condition>;<increments>) But none of it was required so I skipped it. The semicolons are necessary and can't be skipped This converts the loop yo an infinite one because of no increments and conditions
26th May 2019, 11:22 AM
Mark V Zephyrus
Mark V Zephyrus - avatar
+ 1
The three parts of for loop: variable initialization, condition and variable update are optional. If the condition is absent, it is evaluated as true. The loop continues till something else in for loop block stops it. Since in your example for loop is empty, it is an infinite loop. Stack overflow.
26th May 2019, 11:24 AM
Mark V Zephyrus
Mark V Zephyrus - avatar
0
Mark V Zephurus what is for(;;) means?
26th May 2019, 5:34 AM
Tzion
Tzion - avatar