# Thread: Converting Loops

1. Member Join Date
Feb 2014
Posts
1
Rep Power
0

## Converting Loops

How do I convert my for loop below to a while and do while loop?

for(int a=1;a<=9;a++){
for(int b=1;b<=a;b++){
System.out.print(a);
}
System.out.println();
}//end 1-9 loop
for(int a=9; a>=1; a--) {
for(int b=1; b<=a; b++) {
System.out.print(i);
}
System.out.println();
}//ends 9-1 loop  Reply With Quote

2. Member Join Date
Feb 2014
Posts
52
Rep Power
0

## Re: Converting Loops

The while and do-while Statements (The Java Tutorials > Learning the Java Language > Language Basics)
Your code in [CODE] tags for clearer visibility.
Java Code:
```for(int a=1;a<=9;a++)
{
for(int b=1;b<=a;b++)
{
System.out.print(a);
}
System.out.println();
}//end 1-9 loop

for(int a=9; a>=1; a--)
{
for(int b=1; b<=a; b++)
{
System.out.print(i);
}
System.out.println();
}//ends 9-1 loop```  Reply With Quote

3. Member Join Date
Feb 2014
Posts
52
Rep Power
0

## Re: Converting Loops

To solve this problem just take each for loop separately. For example the outer for loop:
Java Code:
`for (int a = 1; a <= 9; a++)`
A for loop has the syntax for (initialization; condition; setVariableTo)

the initialization a = 1 sets a to 1
the condition states loop while a is less than or equal to 9
the setVariableTo says a++ at the end of the loop where
a++ means a = a + 1

from what i've stated you can see that:
a counts 1, 2, 3, 4, 5, 6, 7, 8, 9. At 10 a is no longer equal to OR less than 9.

For a while loop you have the syntax:
Java Code:
```while (conditionTrue)
{
// execute what is here
}```
Therefore for converting a for statement to a while just take the condition from the for loop and insert it in the while statement.
Java Code:
```while (a <= 9)
{
// execute
}```
But before this we need to declare a:
Java Code:
```int a = 1
while (a <= 9)
{
// execute
}```
From here you can see though that a in this case goes nowhere since we still need the increment statement.
Java Code:
```int a;
while (a <= 9)
{
a++;
}```
Your not done though you still need to do the inner loop. So do that part by inserting code inbetween a++ and the while statements first bracket.
Java Code:
```int a;
while (a <= 9)
{
// don't forget the initialization before the while
// other while statement here
a++;
}```  Reply With Quote

4. Señor Member Join Date
Jan 2014
Posts
184
Rep Power
0

## Re: Converting Loops

It's a great answer - though it looks like he needs do while loops, not a simple while loop.

So you'd want to show him these:

Java Code:
```int a = 0;
do {
//this this and this
a++
} while(a <= 9);```  Reply With Quote

5. Member Join Date
Feb 2014
Posts
52
Rep Power
0

## Re: Converting Loops

I was also going to do those also since he needed both but I thought he might get the gist from the first and the tutorial link. Since he didn't respond I didn't think he needed a follow up and I also thought that someone else might also put in there 2 cents.  Reply With Quote

#### Tags for this Thread

loops #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•