Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Messed up for loop  RSS feed

 
Jeremy Graham
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what's wrong with the following code:



but the only output I get is:

b = 0

before the program terminates. Any ideas why?
 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be carefully observing the for loop statement- There's something due to which the statements within the block will not be executed as part of the loop.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a tough typo to spot. In a for loop, the code that iterates is immediately after the parentheses. Typically, this code is enclosed inside braces...

So what if you had this instead...?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!