OK...here is the deal.... I am messing around with this jGRASP program...

I was able to create a program that will display the squares of the numbers from 1 to 10 by using the "FOR" structure.

This is what I have done and here are the results, which is what I wanted, thank GOD!

public class squareFor
{
public static void main(String args[])
{
for(int num=1;num<=10;num++)
System.out.println(num+ " square "+num*num);
}
}

RESULT

----jGRASP exec: java squareFor

1 square 1
2 square 4
3 square 9
4 square 16
5 square 25
6 square 36
7 square 49
8 square 64
9 square 81
10 square 100

----jGRASP: operation complete.

Now, the meaning if this post.....I am having an issue in creating a squareDoWhile program that will do this as well. Can any help me....please? :confused: :S :-/ :'(

OK...here is the deal.... I am messing around with this jGRASP program...

I was able to create a program that will display the squares of the numbers from 1 to 10 by using the "FOR" structure.

This is what I have done and here are the results, which is what I wanted, thank GOD!

public class squareFor
{
public static void main(String args[])
{
for(int num=1;num<=10;num++)
System.out.println(num+ " square "+num*num);
}
}

RESULT

----jGRASP exec: java squareFor

1 square 1
2 square 4
3 square 9
4 square 16
5 square 25
6 square 36
7 square 49
8 square 64
9 square 81
10 square 100

----jGRASP: operation complete.

Now, the meaning if this post.....I am having an issue in creating a squareDoWhile program that will do this as well. Can any help me....please? :confused: :S :-/ :'(

So you want to turn this loop:

for(int num=1;num<=10;num++)
 System.out.println(num+ " square "+num*num);

into a do-while loop?

Look at the for-loop structure:

for (initialization code; test condition code; variable adjustment code)
{
     // repeated code
}

Look at the do-while structure:

// initialization code
do
{
    // repeated code
    // variable adjustment code
}
while (test condition);

Initialization code occurs once, and it needs to occur before anything else, so it goes before do .
The repeated code will be the same in the do-while loop as it was in the for-loop, except that the variable adjustment code will be moved from the parentheses in the for-loop into the brackets in the do-while loop. There is only one thing in the parentheses in the do-while loop as opposed to three things in the for-loop. That's the condition code that bails you out of the loop.

So take your for-loop and classify everything as to which category it goes in:

1. initialization code
2. test condition code
3. variable adjustment code
4. repeated code within for-loop brackets

// code for 1
do
{
     // code for 3 and 4
}
while (/* code for 2 */);

i hope u figured it out, but the following code works, u mite need to change that starting part a bit , because the code is from actual class. : )

void squares()
{
int i=0;
do
{       System.out.print((i*i)+"\n");
i++;
}
while (i<=10);
}

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.