Nicholas Hoyt
Nicholas Hoyt

Reputation: 21

Is it possible to write a conditional with the logical form "then if"?

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.

Upvotes: 0

Views: 481

Answers (3)

Freek de Bruijn
Freek de Bruijn

Reputation: 3622

As background for people not yet familiar with Processing: "Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts." You can use different programming languages and this program is using Java (JavaScript and Python are also supported).

The draw function is called continuously and since you've set the frame rate to 5, it should be called about every 200 milliseconds.

The fill function sets the color used to fill shapes. This fill color will only be used to shapes that are added after you've called the fill function. In your program, calling fill doesn't change the color of the ten squares that the program has already created.

If you use the draw function below, the first ten squares are filled with white (the default fill color). After drawing the tenth square, a random fill color is selected, which will be used for the next ten squares.

By using an if statement within another if statement, you get the "then if" behavior I think you're looking for. The fill color will only be changed if both conditions are met: squareCount < maximumSquareCount and squareCount % 10 == 0:

//global variables:
int x = 0;
int y = 0;
int maximumSquareCount = 25;
int squareCount = 0;

void draw() {
    if (squareCount < maximumSquareCount) {
        rect(squareCount * 40, squareCount * 40, 20, 20);
        x = x + 40;
        y = y + 40;
        squareCount++;

        if (squareCount % 10 == 0) {
            fill(random(255), random(255), random(255));
        }
    }
}

The expression used to decide when to change the fill color might be a bit cryptic: squareCount % 10 == 0. The remainder operator % calculates the remainder after dividing (see https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html). In this case, the square count is divided by ten and if the remainder is equal to zero, the fill color is changed. This will happen after 10, 20, etc. squares have been added.

Upvotes: 1

laancelot
laancelot

Reputation: 3207

I'm not clear on what you mean, but here are some examples that may clear things up. May...

if (expression) {
  // If expression is true, then do this.
else if (anotherExpression) {
  // If expression is false, check whether anotherExpression is true.
  // If it is, do this.
  // There can be more than one "else if" chained one after the other.
} else {
  // If all those "if" and "else if" expressions were false, then do this.
  // The "else" part of an "if" is not required.
}

The official Java tutorial has a good page on the different types of if statements: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

Now if you want to chain conditions:

if (expression && anotherExpression) {
  // Do this if both expressions are true.
}

if (expression) {
  // Do something if expression is true.
  if (anotherExpression) {
    // Do this if anotherExpression is also true.
    // Since this block is inside the first "if", it needs both
    // expression and anotherExpression to be true.
  }
}

Of course you can mix and match these.

Upvotes: 1

TheBatman
TheBatman

Reputation: 770

There is some ambiguity as to your intent but I think I understand what you are going for. Lets first look at your code unchanged

void draw() 
{
    // if statement used to draw ten squares
    if (numberOfSquares < 10) 
    {
        rect(x, y, 20, 20);
        x = x + 40;
        y = y + 40;
        numberOfSquares = numberOfSquares + 1;
        // then, if ten squares exist, and only then
        // fill each square with a random color
        if (numberOfSquares == 10) { 
            fill (random(255), random(255), random(255));
        }
    }
}

So lets consider that Draw runs when you have numberOfSquares = 0. First, it will check numberOfSquares < 10, which is true. So it will create a single square. Then, it will check if numberOfSquares == 10. This will be false since we only have 1 square now and will not run. We would have to call draw() 10 times before if (numberOfSquares == 10) will evaluate to true.

Now referring to your question in a comment you say how might I implement something akin to "do thing, then do next thing for each time that next thing needs to be done"?This is the concept of looping, more specifically it is an if statement followed by a loop. Its a little unclear what you mean to repeat by this, but just as an example lets assume you want "if I have less than 10 squares, produce squares until I have 10 of them". You could use a while loop, or for loop for this, I personally prefer for loops.

void draw() 
{
    // for loop used to draw 10 squares
    for(int i = numberOfSquares; numberOfSquares < 10; i = i + 1)
    {
        rect(x, y, 20, 20);
        x = x + 40;
        y = y + 40;
        numberOfSquares = numberOfSquares + 1;
    }
    if (numberOfSquares == 10) 
    { 
            fill (random(255), random(255), random(255));
    }
}

Now when draw is called, it will produce squares until you have 10 every time. In this case, the inner if (numberOfSquares == 10) is redundant, because we just ensured that 10 will exist no matter what when we reach this if statement, so we can remove it.

void draw() 
{
    // for loop used to draw 10 squares
    for(int i = numberOfSquares; numberOfSquares < 10; i = i + 1)
    {
        rect(x, y, 20, 20);
        x = x + 40;
        y = y + 40;
        numberOfSquares = numberOfSquares + 1;
    }
    // fill 
    fill (random(255), random(255), random(255));
}

Upvotes: 0

Related Questions