14,430,298 members
Rate this:
See more:
So I had a problem on a hw assignment that told me to print a triangle out of stars.

I ended up guessing and checking and pulling out this.
```DIM LINES AS INTEGER
CLS

INPUT "How many lines will your triangle be: ", LINES

FOR x = 1 TO LINES
PRINT
FOR y = 1 TO x
PRINT "*";
NEXT y
NEXT x
END```

The Code works but I dont understand how. I've picked up programming fast, but nested loops have left me confused most of the time. I now its weird to be learning qbasic but that is what my school offers. Can someone please explain the code I just wrote. And help me with DO, and FOR commands. Thank You I am really far ahead with the HW so my teacher isnt helping.

What I have tried:

I guess and checked the code and am now confused. An explanation is all im asking for. Tried to self explain but, didnt understand it.
Posted
Updated 26-Sep-16 17:02pm
v2

Rate this:

## Solution 1

I would have started on a piece of paper, since you only have 3 variables, LINES, X, Y

I'm going to use a notation a <- b means that a becomes the value of b

LINES <- 5 as a start point

X <- 1 (out of 5)
Y <- 1 (out of X which equals 1)
Print "*"
Line 1, one "*" printed

X <- 2 (out of 5)
Y <- 1 (out of X which equals 2)
Print "*"
Y <- 2 (out of X which equals 2)
Print "*"
Line 2, two "*" printed

X <- 3 (out of 5)
Y <- 1 (out of X which equals 3)
Print "*"
Y <- 2 (out of X which equals 3)
Print "*"
Y <- 3 (out of X which equals 3)
Line 3, 3 "*" printed

So for every line 'X' in my case 5 (Lines), Im going to use 'Y' to print the number of stars on that line - which equals X stars - 1st line has 1 star, 2nd has 2 stars, 3rd line has 3 stars, 4th line has 4 stars, 5th line has 5 stars

btw, in between the two loops X Y ie

```NEXT Y
NEXT X```

I would have done something like

```NEXT Y
PRINT (something to make a new line)
NEXT X```

but its been a long time since Ive done any BASIC so you'll have to look that up

does that help ?

[EDIT]

Its also easier to see 'loops' if you indent them

```FOR X = 1 to LINES
FOR Y = 1 to X

NEXT Y
NEXT X```

[/EDIT]
v4
G-MINOR 26-Sep-16 23:24pm

Thanks man what I got from this is x is telling how many lines to for the triangle while Y is telling how many stars to put. Since Y is 1 to x. It is the same number or lines. Makes perfect sense. Thanks.
Rate this:

## Solution 2

I would recommend to use the debugger, but I don't remember if it was efficient in QBasic.
Try this link from Standord University:
Learn to Program[^]
and this one

Search for tutorials as you need.
v3