Skip to main content

What does this line of Code Represent ?

2 replies [Last post]
AndreeU17
Offline
Joined: 2012-10-25

HI i want to know the break down of this Asterisk Pyramid and why use that piece of code an not another.
Picture of the Code
I understand this part

Public Class Pyramid {
Public static void miain(String[] ars){
}
}

But not this code

for (int x = 1; x < 10; x++)
or this code
for (int y = 0; y < x; y++)

also why do we need two of these
System.out.print("*") and System.out.println();

i though with one of the print line is good enough so why have two. I know the first indicating the screen to print * but the other doesnt indicate to print anything. Im guessing its telling the computer to print this line System.out.print("*") multiple time, hence forth creating the pyramid.

The Pyramid
*
**
***
****
*****
******
*******
********
*********

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
shingnpr
Offline
Joined: 2012-09-21

LOOP 1:for (int x = 1; x < 10; x++)
This loop is for no. of rows you will have in pyramid

LOOP 2:for (int y = 0; y < x; y++)
This loop is for no. of Asterisk in single row.

When LOOP 2 finishes then we want to print next Asterisk to next line hence we use System.out.println(); to move control to next line.

Hope this clear your doubts.

saberMule
Offline
Joined: 2012-10-27

System.out.print is telling it to print an asterisk. If it needs to print more than one (closer to the bottom of the pyramid), it will continue to print as many as it needs on that line. System.out.println is telling it to print a new line when it needs to, beginning a new row of asterisks, which will then continue to add an asterisk (System.out.print) until the conditions are met to start yet another. You are correct with your assumption.