[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Impossible java problem
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 13
Thread images: 3
Help me with this java problem /g/. Please explain how this prints 5 times:


for (int j=1; j < 10 ; j = j+1 ) {
for (int i = j-2; ++i < j++; j = i+1)
System.out.println("INF1000");

}
>>
>>51487040
>j = j+1
 j = i+1
j = i+1
j-2
j < 10


why are you writing such a convoluted clusterfuck anon?
>>
>>51487040
Why the fuck don't you write j++?
>>
File: 80MQWY1E5S1U1407514824773.jpg (33 KB, 250x300) Image search: [Google]
80MQWY1E5S1U1407514824773.jpg
33 KB, 250x300
>>51488260
>>51487192

ask the professor who wrote the test.
>>
>>51488269
Wow that shit teaches you nothing but DON'T FUCKING WRITE YOUR LOOPS WITH THAT FUCKING MANY INCREMENTS IN THE LOOP DECLARATION
>>
>>51487040

just debug it interactively and step through it ya tard
>>
>>51487040
The inner for loop will always run once and after exiting the inner for loop j will be twice the amount of i when the inner loop starts (int i = j-2+1 or int i = j-1 if you consider the ++i. that will occur after). But really this question is not something that should be done in practice.
>>
int j = 1 
j < 10 (1 < 10) true
i = j - 2 (i = -1)
++i < j++ (0 < 1; i = 0; j = 2) true
print
j = i + 1 (j = 1)
++i < j++ (1 < 1; i = 1; j = 2) false
j = j +1 (j = 3)
j < 10 (3 < 10)
i = j - 2 (i = 1)
++i < j++ (2 < 3; i = 2; j = 4) true
print
j = i + 1 (j = 3)
++i < j++ (3 < 3; i = 3; j = 4) false
j = j + 1 (j = 5)
...
print
...
j = j + 1 (j = 7)
...
print
...
j = j + 1 (j = 9)
...
print
...
j = j + 1 (j = 11)
j < 10 (11 < 10) false
>>
>>51487040
Why don't you just debug you're code?
>>
>>51488670
it's not MY code, it's a kind of a puzzle on a test
>>
>>51488695
Then input the code to you'rear'e favoright debugger.
>>
>>51487040
Simple. Because "++i < j++" so you're incrementing j twice when "j = i+1". Finally the second loop will only run once each time, so the outer loop runs 5.
>>
File: 1387571364819.jpg (83 KB, 1159x783) Image search: [Google]
1387571364819.jpg
83 KB, 1159x783
>>51488670
>you are a code
Thread replies: 13
Thread images: 3

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.