programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Introductory Java- Loop/Break Question

Greenhorn
Posts: 18
Hi guys,

I am taking my first programming class (Intro to Java) and I have been doing well, thus far.
However, our newest homework assignment has thrown me for a loop--pun intended.

Using a for statement and a formula which approximates pi (known as the Nilakantha formula), I am supposed to achieve the following output:
The value of 'pi' is: 3.1415926228  when i = 200
The value of 'pi' is: 3.1415926516  when i = 500
The value of 'pi' is: 3.1415926533  when i = 1000
The number of iterations to get to 3.14159265 is 339. 'Pi' = 3.1415926600

However, when I break at the 339th iteration, it skips over the 500 and 1000, and I only get the following output:
The value of 'pi' is: 3.1415926228  when i = 200
The number of iterations to get to 3.14159265 is 339. 'Pi' = 3.1415926600

I have spent about eight hours on this today and I can't get it.
I've tried nesting the 'if pie' section within a while (isRangeFound = false) loop, thinking that I would only break out of that loop, but when I do that, I get:
The value of 'pi' is: 3.1415926228  when i = 200
The value of 'pi' is: 3.1415926516  when i = 500
The value of 'pi' is: 3.1415926533  when i = 1000
The number of iterations to get to 3.14159265 is 0. 'Pi' = 0.000000000

Please let me know if you can shed light on why my break either skips the entire program or else doesn't even affect the initialized 0 values of savepie and savei.
My current code is listed below.

author
Sheriff
Posts: 23364
127

Michael Von Thorn wrote:
However, when I break at the 339th iteration, it skips over the 500 and 1000, and I only get the following output:
The value of 'pi' is: 3.1415926228  when i = 200
The number of iterations to get to 3.14159265 is 339. 'Pi' = 3.1415926600

Arguably, it didn't skip 500 and 1000... It simply just finished the loop at iteration 339 (which is what the break instruction does).

As for your second case, you didn't show us the code, so we can't really explain what is wrong.

Henry

Ranch Hand
Posts: 75
1

The break statement in Java programming language has the following two usages −

When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
It can be used to terminate a case in the switch statement.

When you break at the 339th iteration, the loop is terminated.  So, if pie is within the range you're looking for then the next question you should ask is if you've saved i and pie the first time pie is within the range.

Make sense?

Michael Von Thorn
Greenhorn
Posts: 18
Sorry. Here is the other code. (And I really believed that this one would work).
Shouldn't the break only break out of the inner while loop, but still process as a part of the larger for loop?
My final last-line output still reflects that savepie = 0 and savei = 0. It makes no sense to me.

How do I obtain just the first values of i and pie as pie first enters that range, while disregarding the rest?
Again, I need savei to equal 339 in the end (since the 339th iteration of the formula is when pie finally enters the desired ranged), and I need savepie to equal 3.141592660 (since he wants that one rounded to 10 decimals).

Michael Von Thorn
Greenhorn
Posts: 18

Emil Jennings wrote:
The break statement in Java programming language has the following two usages −

When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
It can be used to terminate a case in the switch statement.

When you break at the 339th iteration, the loop is terminated.  So, if pie is within the range you're looking for then the next question you should ask is if you've saved i and pie the first time pie is within the range.

Make sense?

Yes, thank you. I basically understand it in concept, but I can't figure out how to make the loop work. If I don't break, then it gives me the very last value of pie within that range (at the very last iteration - 1000).
It just runs through the entire thing, in other words. But there's no way to just pull out the very first value without breaking.

Emil Jennings
Ranch Hand
Posts: 75
1
Hold on a second my friend...stop coding for a minute.

You have to display the value of pi at the 200th, 500th, and 1000th iteration.
You also have to display the number of iterations it took for pi to have a value between 3.14159265 and 3.14159266.

When you start your 1000 iterations, you have to see if isRangeFound for pi (which when you start it is not).  But once pi isRangeFound you save the value of pi and the iteration that value was found at and make sure the program knows isRangeFound occurred.

Emil Jennings
Ranch Hand
Posts: 75
1

...it gives me the very last value of pie within that range (at the very last iteration - 1000)

Yes, it does because you never ask isRangeFound.

See?

Michael Von Thorn
Greenhorn
Posts: 18

Emil Jennings wrote:

...it gives me the very last value of pie within that range (at the very last iteration - 1000)

Yes, it does because you never ask isRangeFound.

See?

Do you mean like this (below)? It still gives me a final line output of: The number of iterations to get to 3.14159265 is 1000. π = 3.1415926533

Also, since it's an intro class, he attached a logic flowchart (and that's what I've been trying to follow).
I'll include the attachment.
Loops.jpg

Henry Wong
author
Sheriff
Posts: 23364
127

Hint: Be careful to not confuse the assignment operator with the equality comparison operator. See your while loop.

Michael Von Thorn
Greenhorn
Posts: 18

Henry Wong wrote:
Hint: Be careful to not confuse the assignment operator with the equality comparison operator. See your while loop.

Thanks. Nice spot!
I change it. I still can't get it right, however.
I'm going crazy right now. I had an easy time with the other assignments, but this is killing me.

Michael Von Thorn
Greenhorn
Posts: 18
My big problem is that a logic flowchart doesn't indicate whether you need an if, a while, a do while, etc...

The middle part which looks like -
(isRangeFound = "false")
and (pie >= 3.14159265)
and (pie < 3.14159266)

doesn't help me. I don't know if that parenthesis is showing me that I need a while loop or if it's just reminding me that isRangeFound is false before this part.

Emil Jennings
Ranch Hand
Posts: 75
1
• 1 X 2
Henry...you could be a little more helpful, such as the flow chart only depicts one loop.  I know the idea of these forums is not to spoon feed but come on...

Michael - The symbols of the flow chart and the arrows indicate what they are for.  The fact that the flowchart has one place that has arrows going into and out of (1 to 1000) is the indication that only one loop is needed.  Everything between Begin and isRangeFound = False are simple processes (assignment statements).  You should revisit program flowcharting and pseudocode.  The flowchart has the for loop, which your code has.  Does the flowchart have a while loop in the for loop (diamonds are decisions, not loops)?

That middle part in the flow chart is written wrong, it should be isRangeFound = "false" and (pie >= 3.14159265 and pie < 3.14159266) parens set up order, again revisit flowcharting and pseudocode.

Basically, the flowchart is correct:
Loop 1 to 1000
variable assignment
variable assignment
variable assignment
one compound condition (hint it's above)
three assignments if condition is true, none if condition is false
next condition for output

Straight up answer about the parens - in the 2nd condition (i=200, 500, 1000) they are unnecessary.  And they are wrong in the first condition.  In the first "diamond" each condition is in it's own set of parens, which means they might as well not be in any because that means three conditions must be true:
isRangeFound = "false",
pie >= 3.14159265, and
pie < 3.14159266,
when in fact only two conditions must be true:
isRangeFound = "false", and
(pie >= 3.14159265 and pie < 3.14159266)

(isRangeFound = "false") and (pie >= 3.14159265) and (pie < 3.14159266) means isRangeFound must be "false" and pie >= 3.14159265 and pie < 3.14159266 which will never be true because pie can't be >= 3.14159265 and < 3.14159266 at the same time.  These are three unique conditions.
(isRangeFound = "false") and (pie >= 3.14159265 and pie < 3.14159266) means isRangeFound must be "false" and in addition to that pie >= 3.14159265 and pie < 3.14159266.  The placement of the parens states that isRangeFound = "false" is one condition, while (pie >= 3.14159265 and pie < 3.14159266) is one compound condition where each of the two conditions must be true for the entire condition to be true.

You wrote the middle part correctly according to the flowchart as
(isRangeFound = "false")
and (pie >= 3.14159265)
and (pie < 3.14159266)

Unfortunately the flowchart is wrong and it should state
(isRangeFound = "false" and (pie >= 3.14159265 and pie < 3.14159266)

Make sense now?

Emil Jennings
Ranch Hand
Posts: 75
1
Good job Michael,

Consider this:

The difference it isRangeFound is merely a condition to be checked and doesn't require iteration (it's either true of false).

If there is no other description of the problem, the pseudocode in the flowchart you posted IS flawed because of the placement of parens is wrong and will never yield the desired results.

Marshal
Posts: 57437
175
MVT: welcome to the Ranch

I am afraid I shall have to ask you to edit one post to remove the CAPITAL LETTERS. As you doubtless know, writing all upper‑case is frowned upon on all websites.
Please spell the variable pi not pie, unless you usually write pie for π. What Henry meant is writing == true or == false is very error‑prone, as you can read in this thread.

Campbell Ritchie
Marshal
Posts: 57437
175
Emil Jennings earlier pointed out that the loop in this code:-…is unnecessary. In fact what you are doing is starting the loop and then immediately terminating it. Many of us don't like break; inside loops, myself included, but if you are going to use break;, it shou‍ld be wrapped in an if: if (something) break;  But an if shou‍ld occupy 3‑4 lines and include {}. What you achieve there is a loop which only runs once, so it shou‍ld be substituted by an if statement. I believe you cannot use break; inside an if statement.

Marshal
Posts: 5348
374

Michael Von Thorn and Emil Jennings wrote:

Don't use \n for a line break in printf. Different OS can treat it differently. Instead, use %n which is platform independent.

Michael Von Thorn
Greenhorn
Posts: 18

Campbell Ritchie wrote:MVT: welcome to the Ranch

I am afraid I shall have to ask you to edit one post to remove the CAPITAL LETTERS. As you doubtless know, writing all upper‑case is frowned upon on all websites.
Please spell the variable pi not pie, unless you usually write pie for π. What Henry meant is writing == true or == false is very error‑prone, as you can read in this thread.

I'm sorry about the caps, Campbell. I was very excited at the time, but I do promise not to do that again.
Thank you to everyone here for accepting me right into the community. I am having a lot of fun programming and I look forward to reading more posts.
As for this thread question, I just want everyone to know that I solved the problem.

The working code segment is below:

Michael Von Thorn
Greenhorn
Posts: 18
I meant to respond to the pie question, too. I do know that it is "pi."
For whatever reason, the professor wanted us to refer to the variable as pie within the program (perhaps for fun).
I'm taking calculus II right now and I'm really good at math. I swear.

Sheriff
Posts: 4576
128
"Pi" is how English speakers write this Greek letter: π.  It's pronounced the same way as "pie", so yeah, I think your professor is having a laugh.

 All of life is a contant education - Eleanor Roosevelt. Tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS