[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Extra juicy! | Home]

Impossible java problem

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

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++?
>>
>>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 (83KB, 1159x783px) Image search: [iqdb] [SauceNao] [Google]
1387571364819.jpg
83KB, 1159x783px
>>51488670
>you are a code
Thread posts: 13
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]
Please support this website by donating Bitcoins to 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
If a post contains copyrighted or illegal content, please click on that post's [Report] button and fill out a post removal request
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.
This is a 4chan archive - all of the content originated from that site. This means that 4Archive shows an archive of their content. If you need information for a Poster - contact them.