I'm currently trying to teach myself to code in Processing 3
. Having looked over the reference doc, there is no then if (*expression*) {}
statement. My best guess is that I'll need to string together if (*expression*) {}
and else if (*expression)
statements, but I'm so far unable to get something similar to a direct then if
statement. How might I go about doing something like this?
Based on comments, I'm seeing that I should have provided an example case. I don't even entirely understand the logic and so this might be messy, but here's my best attempt:
Say that I want to draw a square to the console, where I have int x = 0
, int y = 0
, and rect(x, y, 20, 20)
. I then want to draw multiple squares in sequence and keep track of the number of squares. Then, if the number of squares is equal to ten, I want to fill each square with a randomly determined color. Every tenth square is intended to serve as a trigger which tells to program to alter the color of each set of ten squares.
I should add that, although there is probably a simpler method for writing this that likely involves looping statements like for
and while
, I'm trying to challenge myself to write this using the stuff that I've already learned.
My current code looks like this; please bare in mind that this is by no means complete and that there are likely to be other errors because I'm just starting to learn to code and I'm not perfect at it yet:
1. //global variables:
2. int x = 0;
3. int y = 0;
4. int numberOfSquares = 0;
5.
6. void setup() {
7.
8. // note that the size of the window is
9. // currently needlessly large, but this
10. // is the intended final size for the
11. // end result of this exercise
12. size(1000, 1000);
13. frameRate(5);
14. }
15.
16. void draw() {
17. //local variables: none yet
18.
19. // if statement used to draw ten squares
20.
21. if (numberOfSquares < 10) {
22. rect(x, y, 20, 20);
23. x = x + 40;
24. y = y + 40;
25. numberOfSquares = numberOfSquares + 1;
26.
27. // then, if ten squares exist, and only then
28. // fill each square with a random color
29.
30. if (numberOfSquares == 10) {
31. fill (random(255), random(255), random(255));
32. }
33. }
34. }
35.
36. // from here, draw 10 squares again,
37. // maintaining the original fill until
38. // another 10 squares are drawn
There are probably also some formatting errors in this edit; this is my first time using a Stack page and so I'm not entirely familiar with the conventions yet. My apologies.