Loops


  1. Count-Based Loops

    The syntax for count-based loops is as follows:

    repeat (<number of times>):
        <expression>

    Example:

    shape = ellipse(250, 250, 300, 300) 
    i = 0
                            
    repeat(3): 
        new_shape: Object = ellipse(250, 250, 300-i*10, 300-i*10) 
        shape += new_shape
        i += 1
    
    shape.draw(1, 1000)


  2. Data Structure-Based Loops

    The syntax for data structure-based loops is as follows:

    for <variable> in <list>:
        <expression>

    Example:

    fruits = ["apple", "banana", "tomato"]
    i = 0
    new_txt: Text = ""
    txt_box: Object
    
    for i in fruits:
        new_txt += i
    
    txt_box.write(new_txt, 100, 100)
    
    txt_box.draw(1, 1000)


  3. Precondition-Based Loops

    The syntax for data precondition-based loops is as follows:

    while (<condition>):
        <expression>

    Example:

    shape = ellipse(110, 110, 30, 30) 
    i = 4
                                                    
    while(i > 0): 
        new_shape: Object = ellipse(100+i*10, 100+i*10, 30, 30) 
        shape += new_shape
        i -= 1
                            
    shape.draw(1, 1000)