Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can somone please EXPLAIN to me this bit of java code and how it goes about prin

ID: 3621359 • Letter: C

Question

Can somone please EXPLAIN to me this bit of java code and how it goes about printing a trianlguar shape of Stars. I need a break down of this code! I have the code but dont UNDERSTAND it. Three days now and nothing for such a simple program.
I neeed to understand it because I need to print out diffrent varirations ofthese triangles.
Heres the source code:
public class Stars
{
public static void main (String[] args)
{
int col,row;

for (row = 0; row <= 10; row++){

for (col = 0; col < row+1; col++)
System.out.print(" * ");
System.out.println();
}


Explanation / Answer

public class Stars
{
// execution starts in the main method
public static void main (String[] args)
{
// declare variables for loop iterations
int col,row;

// rows go down; for each iteration of this loop print a row of stars
for (row = 0; row <= 10; row++){
// columns go across the screen; for each iteration of this loop print one star
// print row+1 stars
for (col = 0; col < row+1; col++)
System.out.print(" * ");
// print a new line to start the next row
System.out.println();
}
}

For the row=0 iteration of the outer loop, we will print 0+1 = 1 stars.

*

For the row=1 iteration of the outer loop, we will print 1+1 = 2 stars.

**

For the row=2 iteration of the outer loop, we will print 2+1 = 3 stars.

***

After 3 iterations of the outer loop, we have the following output:

*

**

***

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote