Loop Examples

Programming Fundamentals · Wikibooks

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From Wikibooks, under CC BY-SA 4.0, as the source states it. This page at Wikibooks.

    Loop Examples

    Counting

    Pseudocode

    ... This program demonstrates While, Do, and For loop counting using user-designated start, stop, and increment values.
    
    Function Main
        Declare Integer start
        Declare Integer stop
        Declare Integer increment
        
        Assign start = GetValue("starting")
        Assign stop = GetValue("ending")
        Assign increment = GetValue("increment")
        Call WhileLoop(start, stop, increment)
        Call DoLoop(start, stop, increment)
        Call ForLoop(start, stop, increment)
    End
    
    Function GetValue (String name)
        Declare Integer value
        
        Output "Enter " & name & " value:"
        Input value
    Return Integer value
    
    Function WhileLoop (Integer start, Integer stop, Integer increment)
        Output "While loop counting from " & start & " to " & stop & " by " & increment & ":"
        Declare Integer count
        
        Assign count = start
        While count <= stop
            Output count
            Assign count = count + increment
        End
    End
    
    Function DoLoop (Integer start, Integer stop, Integer increment)
        Output "Do loop counting from " & start & " to " & stop & " by " & increment & ":"
        Declare Integer count
        
        Assign count = start
        Loop
            Output count
            Assign count = count + increment
        Do count <= stop
    End
    
    Function ForLoop (Integer start, Integer stop, Integer increment)
        Output "For loop counting from " & start & " to " & stop & " by " & increment & ":"
        Declare Integer count
        
        For count = start to stop step increment
            Output count
        End
    End
    

    Output

    Enter starting value:
    1
    Enter ending value:
    3
    Enter increment value:
    1
    While loop counting from 1 to 3 by 1:
    1
    2
    3
    Do loop counting from 1 to 3 by 1:
    1
    2
    3
    For loop counting from 1 to 3 by 1:
    1
    2
    3
    

    Flowchart

    Loops main flowchart Loops Get Value flowchart Loops While Loop flowchart Loops Do Loop flowchart Loops For Loop flowchart

    References