Help understanding for-loop
I am fairly new to programming, I just had a quick question as to why the fallowing code prints the way it does. Also I am new to this forum and any direction to online resources would be greatly appreciated.
public class print{
public static void main (string[] args){
int i=1;
int j;
while (i<=7){
for(j=1;j<=i;j=j+3)
System.out.print("*");
i=i+2
System.out.println();
}
}
}
the output is :
*
*
**
***
Re: Help understanding for-loop
It helps for understanding code if it is properly formatted (nested statements indented) and the code inside of loops is enclosed in {}s.
Please fix the loops and nesting and wrap your code with code tags:
[code=java]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.
Re: Help understanding for-loop
To "Debig" sutch snipplets put more system prints inside the code.
In a nitshell:
On each while block method execution it will print as many *, as for loop will iterate times.
Possible values for j are [1,4,7]
Possible values for i [1..7]
1. Loop *
i=1 j=1 (because 1 + 3 = 4> 1)
2. Loop *
i=3
j=1 (because 1 + 3 = 4> 3)
3. Loop **
i=5
j=1, j=4
4 .Loop ***
i=7
i=1,j=4,j=7
Re: Help understanding for-loop
Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.
The Oracle Tutorials are a great place to start.