• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while( )

 
Shilpa Shanbhag
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone !
This is a question from Ashoka Mock Exam http://www.geocities.com/ashok_12_66/exam_applet.htm
What will happen if you try to compile and run this code ?
1. public class Condition {
2. static boolean condition = true;
3. public static void main ( String [ ] args) {
4. int i = 0 ;
5. if (condition) i++ ;
6. while (condition) {
7. i++ ;
8. if (i>4) break ;
9. }
10. System.out.println(i) ;
11. }
A. The code does not compile.
B. If condition in line 5 is replaced by false the code will not compile.
C. If condition in line 6 is replaced by false the code will not compile.
D. If break in line 8 is replaced by return the output will be 5.
E. The output of this program will be 5.
The answer given is C and E.
My answer was E. I tried compiling the code using
while(!condition) {.....} and the code compile.
Do they have questions like these in the real exam ?
Shilpa
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dont replace it with !condition .
replace it with false :
while(false).
then it wont compile and C and E are cthe correct answers.
 
Shilpa Shanbhag
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just got the answer....anyways THANX for the reply Roy !
Shilpa
 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here !condition will be false so what is the diiference?
I tried the code it gives unreachable statement with false
but works well with !condition and prints 1.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by swati gupta:
here !condition will be false so what is the diiference?
I tried the code it gives unreachable statement with false
but works well with !condition and prints 1.

condition is a variable. false is a literal. It is quite possible that !condition would return true if some assignment had taken place earlier that would have set condition to false. However, there is no case where the literal false could be true. Therefore, that will give you a compile-time error.
I hope that helps,
Corey
 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! Now i understand this.
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by swati gupta:
here !condition will be false so what is the diiference?
I tried the code it gives unreachable statement with false
but works well with !condition and prints 1.

While (!condition) is evaluated at runtime, there is no guarantee that condition won't be changed by some assignment
while (false) is evaluated at compile time, so you know from the beginning that your code will be unreachable.
I hope it helped you!
 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
I remember reading something similar (where hardcoding the value causes the compiler to check the value) in Khalid's Mugahl book but don't remember it was in which topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic