Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

do-while loop problem

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I would like to ask for help in understanding this problem that I encountered in one of the tests in Enthuware Test Studio.

and when I tried to solve the program manually, I ended up with this:
sum--------count
0--------0
1--------1
3--------2
NA--------3
7--------4
12--------5
NA--------6
19--------7
27--------8
NA--------9
37--------10
__--------11?

I do not understand why the program continued to execute after the iteration count=10 and sum=37? After this loop iteration, count++ < 11 is the same as 11 < 11 which returns false and would exit the loop because 11 is not strictly less than 11.  Please help me understand. Thanks in advance!

 
Martin Briones
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to the post above, the program results to printing 48. I accidentally clicked Submit instead of Preview.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Martin Briones wrote:Please help me understand.


Let's add some fancy print statements to the above code snippet to see what's happening when the code snippet is executed And here's the corresponding output:
0 -> 0
1 -> 1
2 -> 3
3 -> 3
4 -> 7
5 -> 12
6 -> 12
7 -> 19
8 -> 27
9 -> 27
10 -> 37
11 -> 48
48


Now let's make one very minor change to this code snippet. But although it's a very minor change, it has a very big influence on program execution (and output)As promised both code snippets are almost the same, only one very minor change was made. If you can't spot this change, look very closely to the condition of the do-while loop... And here is the output of running this code snippet
0 -> 0
1 -> 1
2 -> 3
3 -> 3
4 -> 7
5 -> 12
6 -> 12
7 -> 19
8 -> 27
9 -> 27
10 -> 37
37


So there is a very significant difference between using a post-increment operator and using a pre-increment operator. In this topic you'll find an excellent explanation (with a bunch of code snippets) about the differences between these operators.

Hope it helps!
Kind regards,
Roel
 
Martin Briones
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel De Nijs,

Thank you very much! Yes, your reply and the reference to another thread helped a lot. Lastly, just wanted to clear something from the other topic(Order of Evaluation...), does the post-increment immediately take effect after or during when a Java Operator is used? For example:

In the code snippet, if I inserted another print statement after ln 3, e.g. System.out.println(i) it would still print 0. Here, I used no Java Operator, just a print statement. But if we consider ln 4, it prints 2 which involves the use of a Java Operator. Even if I print System.out.println(i) in an infinite loop, it would always return 0 because of post-increment "use the value" which is 0, "then increment".
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Martin Briones wrote:does the post-increment immediately take effect after or during when a Java Operator is used?


The post-increment operator can be considered as "first evaluate value, then increment value".

If you have this code snippet
 
Martin Briones
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
 
    Bookmark Topic Watch Topic
  • New Topic