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

Is the out 0 2 or 0 2 4  RSS feed

 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Form the below code the should be only 0 2...i guess...because i<4(so the value of i can never be 4.) am i right..

public class Box {


public static void main (String[] args) {

int i = 0;
for (; i <4; i += 2) {
System.out.print(i + "");
}
System.out.println(i);
}
}
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why dont you execute the code and see it for yourself
 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result would be:



Reason:



(1). Prints 0 and 2. Since i < 4 the loop exits.
(2). Prints i's current value which is 4.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!