# Programming in LOLGraphics 3.4/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:
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

## 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:
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.
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.

## 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:
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:

## Finite loops

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.

## Fibonacci sequence

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