Conditions & Loops

Programming in LOLGraphics 3.4 · 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.

    Conditions & Loops

    Introduction

    What we have learned so far is very good, but in order to write anything useful in a programming language, it’s a must to have conditions and loops or at least a way to make “pseudo-loops” using conditions and labels. Luckily, LOLGraphics has all 3 and in this chapter we will be learning all of them! 🎊

    If else statements in other programming languages

    In python for example, an if else statement looks the following:

    if x==5:
       #Instructions
    elif x=6:
       #Instructions
    else:
       #Instructions
    

    Different programming languages may have different syntaxes, but the overall idea stays the same. For example in java you must put the conditions in brackets, write else if instead of elif, and if the block is more than one line wrap it with curly brackets {}.

    However, in LOLGraphics it looks entirely different.

    If else statements in LOLGraphics

    In LOLGraphics there is a single flag that can be updated using commands and is used in conditions and loops. It’s default value is false, and to update it type PLZ ASK CEILIN KAT 2 CHEK IZ <condition>. The condition can be comparing between 2 numbers, addresses, or variables, or a combination of two.

    The command PLZ ASK CEILIN KAT 2 CHEK IZ [X]==8 will check is the value in the one byte variable X is equal to 8. You can use two byte variables ([[X]]), four byte variables ([[[X]]]), and eight byte variables ([[[[X]]]]).

    So we know how to set the flag, but what to do with it. Luckily for use, ceiling cat is very generous and will keep nodding while the flag is true. Simply type IF CEILIN KAT IZ NODDING PLZ RUN <subprogram>. If you want to run a subprogram when the flag is false, type ELSE PLZ RUN <subprogram>. Note that unlike other programming languages, those are two separate commands that come independently from each other which means that you can have any “else” without an “if” in LOLGraphics.

    Also, you can manually set the flag to true and false by using the commands PLZ ASK CEILIN CAT 2 NOD and PLZ ASK CEILIN CAT 2 STOP NODING

    Illustration

    Finite Recursion & Roman Numerals

    In the previous chapter, you have learned how to make infinite recursions. Using the if/else statement, you can make a finite recursion. Using this you can write a program that coverts numbers to Roman Numerals. Here’s the source code that is not included as an example program in the editor:

    
    HAI 3.4 0 10
    IM IN UR CODE EXECUTIN UR KOMANDZ
    
    I HAS A TWO BYTE DAT IZ CALLED X
    DIS IZ MY LABEL! IT IZ KALLED START
    PLZ ASK TEH USR 2 GIMME A TWO BYTE X
    PLZ PRINT TWO BYTE X
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>999
    IF CEILIN KAT IZ NODDING PLZ RUN M
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>899
    IF CEILIN KAT IZ NODDING PLZ RUN CM
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>499
    IF CEILIN KAT IZ NODDING PLZ RUN D
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>399
    IF CEILIN KAT IZ NODDING PLZ RUN CD
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>99
    IF CEILIN KAT IZ NODDING PLZ RUN C
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>89
    IF CEILIN KAT IZ NODDING PLZ RUN XC
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>49
    IF CEILIN KAT IZ NODDING PLZ RUN L
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>39
    IF CEILIN KAT IZ NODDING PLZ RUN XL
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>9
    IF CEILIN KAT IZ NODDING PLZ RUN X
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>8
    IF CEILIN KAT IZ NODDING PLZ RUN IX
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>4
    IF CEILIN KAT IZ NODDING PLZ RUN V
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>3
    IF CEILIN KAT IZ NODDING PLZ RUN IV
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>0
    IF CEILIN KAT IZ NODDING PLZ RUN I
    
    PLZ PRINT TEXT
    PLZ PRINT TEXT======================
    PLZ GOTO LABEL START
    
    IM OUTTA UR CODE
    
    IM IN UR SUBPROGRAM DAT IZ KALLED M
    PLZ TYPE TEXT M
    PLZ SET TWO BYTE X X-1000
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>999
    IF CEILIN KAT IZ NODDING PLZ RUN M
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED CM
    PLZ TYPE TEXT CM
    PLZ SET TWO BYTE X X-900
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED D
    PLZ TYPE TEXT D
    PLZ SET TWO BYTE X X-500
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED CD
    PLZ TYPE TEXT CD
    PLZ SET TWO BYTE X X-400
    IM OUTTA UR SUBPROGRAM
     
    IM IN UR SUBPROGRAM DAT IZ KALLED C
    PLZ TYPE TEXT C
    PLZ SET TWO BYTE X X-100
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>99
    IF CEILIN KAT IZ NODDING PLZ RUN C
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED XC
    PLZ TYPE TEXT XC
    PLZ SET TWO BYTE X X-90
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED L
    PLZ TYPE TEXT L
    PLZ SET TWO BYTE X X-50
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED XL
    PLZ TYPE TEXT XL
    PLZ SET TWO BYTE X X-40
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED X
    PLZ TYPE TEXT X
    PLZ SET TWO BYTE X X-10
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>9
    IF CEILIN KAT IZ NODDING PLZ RUN X
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED IX
    PLZ TYPE TEXT IX
    PLZ SET TWO BYTE X X-9
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED V
    PLZ TYPE TEXT V
    PLZ SET TWO BYTE X X-5
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED IV
    PLZ TYPE TEXT IV
    PLZ SET TWO BYTE X X-4
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED I
    PLZ TYPE TEXT I
    PLZ SET TWO BYTE X X-1
    PLZ ASK CEILIN KAT 2 CHEK IZ [[X]]>0
    IF CEILIN KAT IZ NODDING PLZ RUN I
    IM OUTTA UR SUBPROGRAM
    
    

    Notice how some of the digits are checked recursively, while others like L for 50, XL for 40, and IV for 4 can appear only once and therefore are only printed once.

    “8462 demo” part 1

    Remember previously, the “8462 demo” was mentioned? It’s like a WASD demo only using the numpad. Let’s implement part of it here. Note that this is only part of the code, so don’t expect to copy it to the editor and have it working. Also don’t worry too much about what exactly the subprograms do, we will cover that in the chapter about graphics. Concentrate on the conditions. Assume that the variable graphics was given by the user previously in the code.

    
    PLZ ASK CEILIN KAT 2 CHEK IZ [BUTTON]==8
    IF CEILIN KAT IZ NODDING PLZ RUN UP
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [BUTTON]==2
    IF CEILIN KAT IZ NODDING PLZ RUN DOWN
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [BUTTON]==4
    IF CEILIN KAT IZ NODDING PLZ RUN LEFT
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [BUTTON]==6
    IF CEILIN KAT IZ NODDING PLZ RUN RIGHT
    
    

    As you can see the code is neither attractive nor efficient. Later this chapter we will learn how to write a better version of this code. Also, the code above contains 8 lines of code since empty lines and comments are not sent to the interpreter.

    Illustration
    Ceiling Cat

    Switch case

    Luckily, there is a way to make the code in the previous section more efficient & more attractive, using the switch case commands. The LOLGraphics Interpreter stores a single 8 byte variable (it’s the largest size possible in LOLGraphics in order that you will be able to put in it also smaller sizes). It can be updated using the SWITCH command, and checked using the CASE command. Let’s use a more efficient version of the code in the previous section as an example:

    
    SWITCH [BUTTON]
    CASE 8 UP
    CASE 2 DOWN
    CASE 4 LEFT
    CASE 6 RIGHT
    CASE 86,68,9 UP_RIGHT
    CASE 48,84,7 UP_LEFT
    CASE 42,24,1 DOWN_LEFT
    CASE 62,26,3 DOWN_RIGHT
    
    

    The first line takes whatever is in the one byte variable BUTTON and puts it in the switch/case variable (the default value is 0). The second line of code checks is the variable equal to 8, and if it is goes to the subprogram called UP (again, don’t worry to much about what it does, we will learn more about that in the chapter about graphics).

    The third, fourth, and fifth do more or less the same, so let’s skip them and explain the sixth line of code. This line has multiple numbers separated by commas without spaces. This is the way it must be done. If you don’t type commas or do type a space between values, your code won’t work! If the value of the switch/case variable is equal to 86, 68, or 9, subprogram UP_RIGHT will run. If you check the numpad, it will make sense.

    The code above consists of 9 lines of code, just one more than the code in the previous section however accomplished much more since it allows also to move by diagonals. If you wanted to make it only up/down/left/right, it would be 5 lines of code.

    If statements or switch/case?

    As you have seen the code using switch case is both cleaner and more efficient. So why on earth would you want to use if/else? In this specific program, using switch/case is preferable, however you should know that it’s less flexible since it can only be used to check is a variable equal to a constant number. If you want to check is a variable equal to another variable or use more less operations, then you must use if else. Also sometimes you want to compare a variable to a variable, and also in this case switch/case won’t be very useful for you.

    Infinite loops

    To create an infinite loop, write FOREVER RUN subprogram. Now let’s rewrite the infinite loop code from “Interface, hello world program, subprograms, & labels”, only with actual loops:

    
    HAI 3.4 0 100
    IM IN UR CODE EXECUTIN UR KOMANDZ
    
    FOREVER RUN PRINT
     
    IM OUTTA UR CODE
    
    IM IN UR SUBPROGRAM DAT IZ KALLED PRINT
    PLZ PRINT TEXT I CAN HAS A CHEEZBURGER?
    IM OUTTA UR SUBPROGRAM
    
    

    Finite loops

    Illustration

    Loops work sort of like if statements in that that you can’t write the condition itself in the same line. The loop checks is ceiling cat nodding and while it is, runs a subprogram. The command looks as following: WHILE CEILIN CAT IZ NODDIN PLZ RUN <subprogram>. If you want it to work as an actual while loop, update the flag before this line and at the end of the subprogram.

    Factorial

    The following code prints the factorial of a number the user enters. It is not available as an example code in the LOLGraphics Editor.

    
    HAI 3.4 0 100
    IM IN UR CODE EXECUTIN UR KOMANDZ
    
    I HAS A EIGHT BYTE DAT IZ CALLED X
    I HAS A EIGHT BYTE DAT IZ CALLED I
    PLZ ASK TEH USR 2 GIMME A EIGHT BYTE X
    PLZ SET EIGHT BYTE I X-1
    
    PLZ ASK CEILIN KAT 2 CHEK IZ [[[[I]]]]>1
    WHILE CEILIN CAT IZ NODDIN PLZ RUN MULTIPLY
    PLZ PRINT EIGHT BYTE X
     
    IM OUTTA UR CODE
    
    IM IN UR SUBPROGRAM DAT IZ KALLED MULTIPLY
    PLZ SET EIGHT BYTE X X*I
    PLZ SET EIGHT BYTE I I-1
    PLZ ASK CEILIN KAT 2 CHEK IZ [[[[I]]]]>1
    IM OUTTA UR SUBPROGRAM
    
    

    Fibonacci sequence

    The following code prints numbers from Fibonacci sequence. It is available in the LOLGraphics Editor as one of the example codes.

    
    HAI 3.4 0 100
    IM IN UR CODE EXECUTIN UR KOMANDZ
    
    I HAS A ONE BYTE DAT IZ CALLED MAX
    PLZ TYPE TEXT HOW MANY NUMBERS DO   YOU WANT ME TO PRINT?
    PLZ ASK TEH USR 2 GIMME A ONE BYTE MAX
    PLZ PRINT ONE BYTE MAX
    PLZ PRINT TEXT ======================
    
    I HAS A ONE BYTE DAT IZ CALLED I
    I HAS A EIGHT BYTE DAT IZ CALLED A
    I HAS A EIGHT BYTE DAT IZ CALLED B
    I HAS A EIGHT BYTE DAT IZ CALLED C
    
    PLZ SET ONE BYTE I 2
    PLZ SET EIGHT BYTE A 0
    PLZ SET EIGHT BYTE B 1
    PLZ ASK CEILIN KAT 2 CHEK IZ [MAX]>0
    IF CEILIN KAT IZ NODDING PLZ RUN PRINT_A
    PLZ ASK CEILIN KAT 2 CHEK IZ [MAX]>1
    IF CEILIN KAT IZ NODDING PLZ RUN PRINT_B
    PLZ ASK CEILIN KAT 2 CHEK IZ [I]<[MAX]
    WHILE CEILIN CAT IZ NODDIN PLZ RUN COUNT
    
    IM OUTTA UR CODE
    
    IM IN UR SUBPROGRAM DAT IZ KALLED COUNT
    PLZ SET ONE BYTE I I+1
    PLZ SET EIGHT BYTE C A
    PLZ SET EIGHT BYTE A B
    PLZ SET EIGHT BYTE B C+A
    PLZ PRINT EIGHT BYTE B
    PLZ ASK CEILIN KAT 2 CHEK IZ [I]<[MAX]
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED PRINT_A
    PLZ PRINT EIGHT BYTE A
    IM OUTTA UR SUBPROGRAM
    
    IM IN UR SUBPROGRAM DAT IZ KALLED PRINT_B
    PLZ PRINT EIGHT BYTE B
    IM OUTTA UR SUBPROGRAM