This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# For or While Statement Part 2

Avery Jerauld
Greenhorn
Posts: 28
You will run two small labs using the statements; discuss the attributes of the programs; and write your own program using the for-or while statements. This lab is part of your discussion grade for the unit.

Write a program that squares the numbers from 1 to 10 using a for or while statement. Afterwards, print the results to the screen. Discuss how you used the statements learned in the lab to complete your program in the discussion box for this unit.

That second part is what I need help on. I had this in another thread but we got side tracked and did something else and was recommended on making another thread. So this is that thread. Here is what I have:

I honestly have no clue what I'm doing. The "<=0144" is there because NetBeans decided that was better than "<=100". This probably looks like a bunch of stupid mistakes but I have no idea what to do. Help?

Matthew Brown
Bartender
Posts: 4568
9
I don't know why Netbeans told you that - maybe you've got some strange settings turned on. 0144 is an octal integer literal. So it's actually exactly equivalent to using 100 - though I'd only expect it to be used in a context where octal was particularly relevant. 100 is obviously much easier for humans to read.

Another question, given yous statement of the problem: why 100 and not 10?

Edit: anyway, what your current code is doing is multiplying all the squares of numbers between 0 and 99 inclusive, and writing out the final answer. However, since you start with zero, and 0*anything is still zero, the final answer is always going to be zero.

Avery Jerauld
Greenhorn
Posts: 28
Matthew Brown wrote:Another question, given yous statement of the problem: why 100 and not 10?

I put 100 because that would be the square root of 10. Would the program be reading the square or the root before it squares it? If that makes any sense.

Matthew Brown wrote:anyway, what your current code is doing is multiplying all the squares of numbers between 0 and 99 inclusive, and writing out the final answer. However, since you start with zero, and 0*anything is still zero, the final answer is always going to be zero.

Whenever I run the test with it as (done =1; done <=10; done++) it just says that the square is 0.

Campbell Ritchie
Sheriff
Posts: 50768
83
That is what comes from coding before you have worked out the algorithm. Get some paper a pencil and an eraser (preferably a large one ) and write down how you plan to do that on paper. When you have it down to words of one syllable, then you will find it easy to turn into code.

Avery Jerauld
Greenhorn
Posts: 28
Campbell Ritchie wrote:That is what comes from coding before you have worked out the algorithm. Get some paper a pencil and an eraser (preferably a large one ) and write down how you plan to do that on paper. When you have it down to words of one syllable, then you will find it easy to turn into code.

Well I know how to do that.

Start=1
1*1=1 (which needs to be displayed)
Start+1=2
2*2=4 (which needs to be displayed)

And keep going until Start>10

My problem is the converting over to code from that.

fred rosenberger
lowercase baba
Bartender
Posts: 12235
36

To me, that says "Do something with the numbers from 1 to 10".

So, I'd start with a for loop (since i know EXACTLY how many times I want my loop to run) that simply prints the numbers 1 to 10.

Once I had that working, I'd go back and see what I'm supposed to do with the numbers from 1-10...in this case, square it.

Once I had that working, I'd look at the next step...

Avery Jerauld
Greenhorn
Posts: 28
That step by step thing helped a little Fred. Except I really don't know what I did. I just took out some signs and randomly switched stuff around until I got it to display 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.

But I have no idea how to square it.

My problem isn't necessarily writing the code, but it's not knowing what to write. I don't know how I'm supposed to put "square this if this is true". I have the formula and everything perfectly set, but I just don't know how to implement it into the system.

Edit:
This is what I have so far.

Matthew Brown
Bartender
Posts: 4568
9
Avery Jerauld wrote:But I have no idea how to square it.

Really? Because the code is almost identical to the way you explained it:

Raphael Esteves Martins
Greenhorn
Posts: 1
• 1
Hello! If you want that the program shows all squares of number in interval 1...10. Or, the program shows:
0
1
4
9
16
25
36
49
64
81
100

You should think about the algoritm.
Lets think:
"Which variable will increase and multiply?"
"Which variable will show my multiply results?"

"The initial value is start=1;
If done=1 and done <=10, increases done. So, print it on screen!
Ah! Start and Done is the same thing.
Print done or start on screen."

But, you want:
"The initial value is start=0;
If start=0, start <=10, increase start.
Ah! start*start=done.
So, print done on screen!"

fred rosenberger
lowercase baba
Bartender
Posts: 12235
36
it is important to remember that code is modular - you can usually separate things out into distinct components, and then swap them in and out with each other. So, once you have it printing the values 1-10, you should have a fairly obvious line that prints the current value.

you would then need to replace that one line with other code that prints the square of a number. so if you have something like:

Line 3 clearly prints the value of x. I/you can delete that one line and replace it with 1 (or 10, or 1000) line(s) that do something else. We know that on that line, x will have some value, and i want to print the square of that value.

In order to print it, i would probably need to calculate it, yes?

so i can change it to

So now i have calculated the square, so now i can print it:

So you can see, i have now replaces the original single line with two new lines. I could add in more lines if needed, and none of the rest of the code is effected.

there are caveats...if in my new code I change the value of x, it would mess up my loop, but sometimes thats what you need.

My point is, you need to separate out the various tasks in your code, and work on them one at a time. get one part working. Stick in dummy code if you need to...for example, I might have on my first go done this:

Joe Areeda
Ranch Hand
Posts: 331
2
I think the advice you gotten is pretty good. Your original code has some problems that indicate you may be missing some basics.

For one thing start is set to 0 so start * anything will stay zero.

Next your for loop is going to run 100 times not 10.

Also the for loop starts at 0 so no matter what you set start to initially it will be zero and stay zero.

Now even if we fix those and set start and done to 1 to begin with what's going to happen?

1 * 1 = 1
1 * 2 = 2
2 * 3 = 6
6 * 4 = 24
...
So you'll be calculating factorials not squares, which by the way will overflow Integers before you get to 100.

Joe

Campbell Ritchie
Sheriff
Posts: 50768
83
Factorials overflow the int datatype about 12!, and about 34! they revert to 0.

Avery Jerauld
Greenhorn
Posts: 28
You guys are all really great. I finally got it after all of you had to keep restating yourself just for me to understand. And no one got angry or anything. Thanks so much!

Campbell Ritchie
Sheriff
Posts: 50768
83
Aw shucks, you've got us all embarrassed

Often, if I find somebody doesn't understand what I have told them, I worry that I have explained it badly.

Matthew Brown
Bartender
Posts: 4568
9
Campbell Ritchie wrote:Often, if I find somebody doesn't understand what I have told them, I worry that I have explained it badly.

All human communication fails, except by accident

Campbell Ritchie
Sheriff
Posts: 50768
83
That's an interesting link. But when I first saw it, I thought it was "all human life is there".