Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Ivor Horton's Beginning Java, Java 7 Edition
|
BOOK: Ivor Horton's Beginning Java, Java 7 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java, Java 7 Edition by Ivor Horton ; ISBN: 978-0-470-40414-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Java, Java 7 Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 19th, 2012, 12:21 PM
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Question on Chap 3: Loops and Logic - "Calculating Primes" examples

Greetings...I'm new to Java and the P2P site.

I have a question on the "Calculating Primes" examples in chapter 3 used to illustrate loops using the "break" and "continue" statements. I mostly understand the logic of the code and how it chooses to display a number as being prime or not except for the number 2 (which i know is a prime number). However I don't quite understand how the number 2 is allowed to be displayed.

When the IF statement in the inner FOR loop is executed the first time both "i" and "j" have a value of "2". If both have a value of 2 then the remainder of "j" dividing "i" is zero and the IF statement should evaluate to TRUE, which in turn means the variable "isPrime" is assigned a value of FALSE and the BREAK statement is executed. When the IF statement in the outer loop is executed "isPrime" now has a value of FALSE which means the println statement should not execute and display the value of "i" when it equals "2".

After the completion of the first iteration of the outer FOR loop and "i" is incremented to 3, I understand how all prime numbers from there on (up to 50) are displayed. Again, my problem is when both "i" and "j" equal 2.

Please help me understand where my thinking/logic is wrong :)

----------------------------------------------
Update:
Here's the code for the example I'm referring to:
----------------------------------------------

public class Primes {
public static void main(String[] args) {
int nValues = 50;
boolean isPrime = true;
for(int i = 2; i <= nValues; ++i) {
isPrime=true;
for(int j = 2; j < i; ++j) {
if(i % j == 0) {
isPrime = false;
break;
}
}
if(isPrime)
System.out.println(i);
}
}

}

Last edited by kao2110; January 19th, 2012 at 03:17 PM..
 
Old January 27th, 2012, 01:49 AM
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by kao2110 View Post
Again, my problem is when both "i" and "j" equal 2.
When both i and j equal 2, the inner loop simply doesn't execute at all. Look at its testing condition, in bold:
for (int j = 2; j < i; ++j) {...}
So you get 2 < 2, which is false, therefore the loop doesn't even begin to execute. The logic immediately skips to this line:
if (isPrime)
which happens to be true at that time, so the program prints "i", i.e. 2.
 
Old January 27th, 2012, 05:33 PM
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default THANKS!!

Awesome! Thanks sklm!!

Now it makes sense!!!! Based on what you are saying the testing condition is evaluated BEFORE the loop begins. If it evaluates to false then the loop never starts and it bypassed, if it evaluates to true then the loop begins and the testing condition is evaluated once for each iteration of the loop.

I thought that the loop executes at least once first then checks the testing condition to see if it should continue.

Here's what the book says when describing FOR loops:

"Execution of this loop continues as long as the condition you specify in the second part of the control mechanism, the loop_condition, is true. This expression is checked at the beginning of each loop iteration, and as long as it is true, the loop body executes."

After your explanation I guess I can see more of what they meant but for me it would have been clearer if they would have said: "Execution of this loop starts and continues as long as the condition you specify in the second part......"

Thanks again!

That explanation really helped a lot!!
 
Old January 28th, 2012, 04:15 AM
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by kao2110 View Post
I thought that the loop executes at least once first then checks the testing condition to see if it should continue.
Not the For loop. But there is the Do-While loop that does exactly that.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chap 9, CreateNewProject can't find "CodeControl" Moose BOOK: Beginning Microsoft Visual Studio LightSwitch Development 1 November 25th, 2011 09:49 PM
Can't get "ASP.NET configuration" to work in the examples. Javaman BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8 1 November 3rd, 2009 02:34 PM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM
Chap 15: Cookies / "Remember Me" Login sw02 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 August 12th, 2009 11:18 AM
Chap 11: "Properties" function missing tomche BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 May 24th, 2009 09:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.