Processing – If Statement and FOR loops
There are many ways to change what your program will do or follow. One of them will be the IF statements. Statement is an expression of an attitude or belief that is made by means of actions or appearance and it works also in programming. We can tell a program to do something for us IF something else is true. If its false then it will continue what it was doing till what we want is true so it can change to the action we wanted it to do. Here is an example.
We started with a window being red. As we keep the window open it adds +1 red value to it making the red color becoming brighter and brighter. This program wouldn’t have an end to it with the red increasing with time. We didnt want that to happen. We wanted the red to stop at 255 and then go back to 0 and start increasing again from the start. Thats why we added the IF statement to make it work how we wanted to.
by adding this code ”if (statement) update” we changed the program to what we wanted to. If the value of the red color is larger than 255 then set the value of the red color back to 0. Written like this ”if(Redval > 255) Redval = 0;”
There are a lot of different IF values we can use in the program and this is how we use them:
“>” if something is greater than our limit
“>=” if something is greater OR equal than our limit
“<” if something is less than our limit
“<=” if something is less OR equal than our limit
“==” if something is equal to our limit
“!=” if something in not equal to our limit
“!a” if something is the opposite of our “a” statement
“a && b” if both “a” and “b” statements are true
“a || b” if either of “a” or “b” are true
And these are most if not all of the ways we can use the IF statements.
Now as for the FOR loops, they are an easier way to write a WHILE loop and to not have bits of codes all over the place.
On top of the code you can see an example of a WHILE loop and it is written in this format:
Initialization
while(test)
Statement
Update
On the bottom of the code you can see an example of a FOR loop and it is written in this format:
for(initialization; test; update)
Statement
Writing a loop in a FOR format takes less place and you have all the coding in one lines instead of trying to write them in different lanes. It just makes things much for easier to make, write and keep in order.