none
Beginner Question About Loops RRS feed

  • Question

  • I have an assignment about loops and i can't figure it out.I need some help.

    I need to make a triangle based on input.For example if the input number is 6 it should write;

    0
    01
    012
    01234
    012345

    on the screen.I know the start and that its 2 loops together but i can't find a way to do it.

    TextWindow.Write("Height: ")
    triangleheight = TextWindow.ReadNumber()

    What do i write after that ?Thanks in advance for all your answers.

    Tuesday, November 5, 2013 8:09 PM

Answers

  • What code do you have so far for the loops?

    Its not really the point for someone to just write the code for you.  So apart from general advice we need to see the code so far to help you do it for yourself.

    This is an example of nested (2 levels) of loops

    For i = 1 To 5
      For j = 1 To 5
        TextWindow.WriteLine(i+" + "+j+" = "+(i+j))
      EndFor
    EndFor

    Tuesday, November 5, 2013 10:26 PM
    Moderator

All replies

  • You will need a loop over height (0 To triangleheight)

    Inside this loop you will need to write the numbers up to the current loop index (starting at 0).

    If you are not sure about loops, then start creating the triangle without loops.

    HINT (note the difference between TextWindow.Write and TextWindow.WriteLine when outputting data to the TextWindow.

    Tuesday, November 5, 2013 9:22 PM
    Moderator
  • You will need a loop over height (0 To triangleheight)

    Inside this loop you will need to write the numbers up to the current loop index (starting at 0).

    If you are not sure about loops, then start creating the triangle without loops.

    HINT (note the difference between TextWindow.Write and TextWindow.WriteLine when outputting data to the TextWindow.


    I can't do the loop within the loop part,could you give me a more direct answer if possible please
    Tuesday, November 5, 2013 9:39 PM
  • What code do you have so far for the loops?

    Its not really the point for someone to just write the code for you.  So apart from general advice we need to see the code so far to help you do it for yourself.

    This is an example of nested (2 levels) of loops

    For i = 1 To 5
      For j = 1 To 5
        TextWindow.WriteLine(i+" + "+j+" = "+(i+j))
      EndFor
    EndFor

    Tuesday, November 5, 2013 10:26 PM
    Moderator
  • Hi Bahattin

    Yes at 1st attempt loops and stuff can look a bit weird. But they're pretty simple.

    So approach your assignment bit by bit with a view to learn something.

    1: Start with TextWindow.Write and TextWindow.WriteLine and the TextWindow.Read operations.

    Best way to learn is see the example then you try it, maybe play around with it, and see what happens.

    TextWindow.Write("Enter something here: ")
    yourInput = TextWindow.Read()
    
    TextWindow.Write("Write your input to the TW: ")
    TextWindow.WriteLine(yourInput)

    Try the above sample out, change it and see what it does, then:

    2: Try a loop: there's a few different loops you can use and some are good for different things.

    For i = 1 To 10
      TextWindow.Write(i)
      TextWindow.WriteLine(": Hello")
    EndFor

    This writes to the TW the value of i and on the same line the word ": hello".

    It runs from the key word For to the word EndFor 10 times, i.e. i=1, i=2, i=3, ..... to i=10".

    Once i gets to 10 the loop is finished and the program will read the next line of code after EndFor.

    You can see this for yourself if you put a TextWindow.Writeline statement after the "For" loop.

    Try it out, see how you go and if you have any questions post your code sample and related question.

    All the best, and the Small Basic curriculum does a great job walking you though all this stuff.

    Tuesday, November 5, 2013 10:57 PM
    Moderator
  • If you work through the curriculum:

    Small
    Basic Curriculum: Online

    Chapters 1.1 to 1.5, try their samples as you go and the exercises at the end you start learning what Small Basic does.

    Avoid skipping ahead, it's not good to miss stuff. As you go you could have a crack at your assignment as well.

    It's fun getting the computer to do what you want it to.

    It takes time to get good, remember it takes 4 years to become a plumber but in a week or 4 you will start to understand how to do the above task.

    Tuesday, November 5, 2013 11:21 PM
    Moderator
  • If i was doing this i would try Text.Append and just a single loop.

    output = Text.Append(output, i)

    Tuesday, November 5, 2013 11:36 PM
    Moderator
  • Hey Bahattin, sorry i overlooked your 2 loop situation.

    Using an empty TextWindow.WriteLine("") you can write nothing to the line and the cursor will move to next line.

    Also on the nested loops, the outer loop steps once and the inner loop will run it's course, then the outer loop will step once again and the inner loop will run it's course again.

    For i = 1 To 3
      For j = 1 To 5
        'here loops 5 times and because it's inside an outer loop it will do that 3 times
        '5 times 3 times
      EndFor
    EndFor

    line = 6
    
    For i = 1 To line
      TextWindow.Write("line: ")
      TextWindow.Write(i)
      TextWindow.WriteLine("")
    EndFor
    
    TextWindow.WriteLine("----------------------------")
    
    For i = 1 To line
      length = i
      For j = 1 To length
        TextWindow.Write(j - 1)
      EndFor
      
    EndFor

               

    Wednesday, November 6, 2013 3:19 AM
    Moderator
  • What code do you have so far for the loops?

    Its not really the point for someone to just write the code for you.  So apart from general advice we need to see the code so far to help you do it for yourself.

    This is an example of nested (2 levels) of loops

    For i = 1 To 5
      For j = 1 To 5
        TextWindow.WriteLine(i+" + "+j+" = "+(i+j))
      EndFor
    EndFor


    Thank you for your help,i was able to do it using this method.
    Wednesday, November 6, 2013 7:05 AM