File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loops

 
ShrutiRavi Gupta
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output: 1 defualt 1 defualt 1


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

Henry
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ShrutiRavi Gupta
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic