File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Loops" Watch "Loops" New topic
Author

Loops

ShrutiRavi Gupta
Greenhorn

Joined: Jan 08, 2009
Posts: 8
when I test the following condition with variable it returns true. however, if I test it with a literal lt returns false.

int i=5; while(i<5) {//something in here}

while(5<5) {} //unreacheable code


The program was something like this. So I summarised the problem as above. In this piece of code why does the while loop run last iteration of i=5

public class Question59 {
public static void main(String[] args) {
int i=5;

while(i++<5){
System.out.println("while"+i);
switch(i%2){
//System.out.println("switch"+i);
default:System.out.print("default ");System.out.println("switch"+i);break;
case 1:System.out.print("1 ");System.out.println("switch"+i);break;
}}}}


Output: 1 defualt 1 defualt 1



Shruti
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Output: 1 defualt 1 defualt 1


I don't get any output when I run the code.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Shruti please use code tags to post code here.

Are you sure that you are getting the output as given by you. have you tested it?


The code given should not give any output as the while condition will not result in true.

HTH

My Blog SCJP 5 SCWCD 5
ShrutiRavi Gupta
Greenhorn

Joined: Jan 08, 2009
Posts: 8
Here's the exact piece of code:


public class Question56 {
public static void main(String[] args) {
int i=0;
while(i++<5){
switch(i%2){
default:System.out.print("default ");break;
case 1:System.out.print("1 ");break;
}
}
}
}


i have tried running it. returns:

1 default 1 default 1

i'm concerned with last 1 in the output.

Thanks
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi Shruti,

This is my understanding.



The value of i is 0 initially.
1st while test: 0<5...true....then the value of i is incremented to 1 and this incremented value goes inside the while loop...1%2...output=1
2nd while test: 1<5..true....then the value of i is incremented to 2 and the incremented value goes inside the while loop....2%2..output=default
3rd while test: 2<5...true...then the value of i is incremented to 3 and the incremented value goes inside the while loop..3%2....output=1
4th while test: 3<5...true...increment i to 4...4%2...output=default
5th while test: 4<5..true...increment i to 5...5%2...output=1
6th while test: 5<5...now the while test fails and the loop terminates....

so the output is
1 default 1 default 1


Regards
ShrutiRavi Gupta
Greenhorn

Joined: Jan 08, 2009
Posts: 8
Hi Matt,

This seems to be rt. logic. Thanks.

This means i++ is being used as post increment operator. When I change this code to ++i then the last loop isn't run.

Thanks Again

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loops
 
Similar Threads
Doubt about Switch statement (Compile Time Constant)
Want to convert my code in method
Problems with "while " loops
which break statement doulh I use