I want to draw a triangle with stars like below using for loop, but i really don\'t have any idea of how to do this ? Triangle is going to be like this:
*
**
**
Just to comment on your internet-found code...
for
loops should always start from 0
, unless you have a specific reason to start from 1
. Its a good habit to practice starting from 0
for everything, as it'll help you when it comes to using java arrays
.for
loops inside each other... The outside loop is just controlling how many lines there are in the triangle (8
in this case). The inner loop is writing the number of stars for that line. This isn't the best way of achieving the result, but it would work correctly.for
loop at the bottom is writing out stars to appear like the trunk of a tree.Hope this helps your understanding.