• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding multiple terms to the for statement (Java OCA 8 Programmer I Study Guide, Sybex)

 
Nathan Wimberley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 82 in Adding multiple terms to the for statement you have:


I know that you already have remove the last line in the Errata but you state that the output of this statement is
0 1 2 3 4
Wouldn't it be 0 1 2 3 4 5 6 7 8 9 because it needs to complete both sides of the statement.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nathan Wimberley,

First of all, a warm welcome to CodeRanch!

Nathan Wimberley wrote:Wouldn't it be 0 1 2 3 4 5 6 7 8 9 because it needs to complete both sides of the statement.

No! The study guide is spot-on. The output of that code snippet is (if the last line is removed): "0 1 2 3 4".

Explanation: the for loop will keep executing if x is less than 5 AND y is less than 10. If one of those conditions is not true anymore, the for loop finishes its execution. So after a few iterations both x and y will be 4, the value of y is printed and then both variables are incremented with 1, now both x and y are 5. And then the condition is tested again: because 5 is NOT less than 5, the for loop finishes its execution and the execution of the code jumps to the first statement after this for loop (and that's why the value of y is not printed anymore).

Hope it helps!
Kind regards,
Roel

PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. Jeanne already added the code tags for you. See how much easier the code is to read?
 
Nathan Wimberley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if it used just & and not && it would print 0 to 9?
 
Nathan Wimberley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, sorry for the poor posting technique.
 
Nathan Wimberley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind I think I have it. If the for statement comes out false it will exit. If you wanted it to print 0-9 it would need an || to keep it true.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan Wimberley wrote:Never mind I think I have it. If the for statement comes out false it will exit. If you wanted it to print 0-9 it would need an || to keep it true.

Spot-on! That change is indeed required to have the code snippet print "0 1 2 3 4 5 6 7 8 9". The || (and |) operator will return true if at least one of both operands evaluate to true. The && (and &) operator will return true only if both operands evaluate to true. Have a cow for correctly answering your own question!

What do you think the output of this code snippet will be?

Hope it helps!
Kind regards,
Roel
 
Nathan Wimberley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would output 1 2 3 4 5
This is because Y increments before and X would increment after.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan Wimberley wrote:That would output 1 2 3 4 5
This is because Y increments before and X would increment after.

Unfortunately, that's incorrect! That code snippet will print "0 0 0 0 0 1 2 3 4 5 6 7 8 9". Can you figure out yourself why or do you need some guidance? One hint I can already give you: || is a short-circuit operator.
 
Nathan Wimberley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't paying close enough attention.
The first part being true will only run so Y will not increment until x<5 is false. Then after x = 5 Y will then start increment till y<10 is false.
you don't look to the other side of || till the left side is false.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan Wimberley wrote:I wasn't paying close enough attention.

That's something you should not do on the actual exam

Nathan Wimberley wrote:The first part being true will only run so Y will not increment until x<5 is false. Then after x = 5 Y will then start increment till y<10 is false.
you don't look to the other side of || till the left side is false.

You are spot-on!
 
Nathan Wimberley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what do you think the output will be if we make only one minor change to the previous code snippet?
 
Nathan Wimberley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be 1 2 3 4 5 6 7 8 9 because both sides are done each time through and when x< 5 is false then the for loop continues till y<10
when y< 10 is false then the for loop ends
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan Wimberley wrote:That would be 1 2 3 4 5 6 7 8 9 because both sides are done each time through and when x< 5 is false then the for loop continues till y<10
when y< 10 is false then the for loop ends

Again you are spot-on! I think you are now ready to ace every question about for statements on the actual exam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic