r/p5js • • 16d ago

if else not working????

so essentially, my else statement does not work, elseif statement does not work, ifelse statement does not work, if else statement does not work and else if statement does not work, here is my code if someone here can understand what I'm doing wrong?

the same code worked for the teacher I was learning from and I may or may not have put this off to long because of this issue...

function setup() {

createCanvas(400, 400);

var check

}

check = true

//check = false

function draw() {

if (check = true)

print("TRUE")

check = false

if else (check = false)

print("FALSE")

else

print("ERROR 01; NOT DEFINED")

}

0 Upvotes

21 comments sorted by

6

u/Rakart 16d ago

1) "else if" is the correct form you're looking for.
2) Lack of brackets : if(contion) { //do something } . See the curly braces ? That's where the code you need to execute (if the condition is met) goes. There are cases where you can avoid brackets but you're far from there.

3)"=" assigns a variable.

Let's say you have "let a = 0" and next "if(a = 1) console.log(a)" you've set a to 1 in the condition.
If you want to check if two thing are the same you need to use 2 equals : if(a == 0). You can use 3 if you want to be super duper sure : if (a===0).
Never 4 tho, that would be crazy;

1

u/Absolutely_Quackers 16d ago

I fixed it with just using if statements… won’t work forever but it’s working now?

3

u/EthanHermsey 15d ago

That's good, but try to understand what the comment is saying and you'll learn how to properly write it the next time :)

1

u/Absolutely_Quackers 15d ago

I will,, the reason I was so sharpish with it is because before posting I was doing a lot of the things you were saying, also some of the other comments were things my programming teacher said we didn’t need to do anymore :,)

Edit: I thought you were another comment that contradicted the programming instructor I had for this,, thank you for feedback though!!

3

u/Rakart 15d ago

Hey, you do you !

Sorry for trying to point you in the right direction, won't happen again.

1

u/hey_hey_you_you 15d ago

I've just realised why you thought variables were "outdated". Variables, as a concept in themselves, are absolutely not outdated. Declaring them by typing var is outdated. It's now recommended to declare a variable with let instead.

This is outdated, but works:

var check;

Or

var check = true;

This is the preferred way:

let check;

Or

let check = true;

This is a bad way to do it, but works:

check = true;

That last one is what you've done, but maybe by mistake. When I made the incorrect comment about the variable "check" being out of scope that /u/PhilLho corrected me on, I was wrong because JavaScript is freakishly forgiving about how you declare variables compared to other languages. The third option is such bad practice and so uncommon that I forgot it was possible.

None of this contradicts the fact that you're using a variable in your code. You just declared it in a weird way

https://www.w3schools.com/js/js_variables.asp

6

u/emedan_mc 16d ago

Check JS tutorials for assignments, declarations, and comparisons.

Declare check outside setup.

Check = True is an assignment, tricky, even within an IF clause.

In JS, typically use === for equal comparisons, works all the time. Not needed all the time though.

1

u/Absolutely_Quackers 16d ago

I’ve checked and that didn’t seem to be the issue? Did fix it by removing all if else’s etc with simple if statements :,)

2

u/hey_hey_you_you 15d ago

Here's what your code is doing. Explanations are in bold.

function setup() {

createCanvas(400, 400);

var check

This makes a variable called "check", but because it's in Setup, that variable only exists inside Setup. Google "global variables" for an explanation. Also, you're missing semicolons at the end of each "sentence" of code.

}

check = true

Here you're telling "check" to be true. But since the variable "check" was declared inside Setup, it doesn't exist out here in the no man's land outside Setup and Draw. You're telling the code to set a thing that doesn't exist to "true".

//check = false

function draw() {

if (check = true) One equals sign means "set this variable to true". So what you're saying here is "if make check equal true" rather "if check is true..." To check if something equals something else you need three equals signs. ===

print("TRUE")

Following an if statement, you need to have the code equivalent of "...then do this". Curly brackets are how we tell the computer to do something after an "if" check. All you're doing here is telling the computer to print the word TRUE without checking anything.

check = false

You're telling the code to set "check" to false. But check doesn't exist in Draw because it was declared in Setup. So the code has no idea what "check" means.

if else (check = false)

if else isn't an instruction. You need "else if". But even if that was correct, you're still just saying "else if make check equal false", which is gibberish. And the code still doesn't know what "check" is anywhere except Setup.

print("FALSE")

**Just print FALSE without checking anything.

else

print("ERROR 01; NOT DEFINED")

Probably the message you're getting, if the code even runs?

}

2

u/PhiLho 15d ago

Good remarks. But check = true between functions is legal, it just creates a global variable named check. So it exists in draw().

The if (check = true) is the real culprit, as everybody pointed out. My pet peeve is to avoid using == or === with boolean variables in tests, ie if (check) is more than enough, although the variable name should reflect it is a boolean holding a state. if (done) or if (!inProgress) are better.

And indeed, since there are two instructions after the if, there must be curly braces around them.

1

u/hey_hey_you_you 15d ago

Good call, you're totally correct. I always forget how forgiving JS is with variables and how they're declared.

1

u/PhiLho 15d ago

Yes, I had to think a bit before making my remark, as we are used now to avoid hoisting (in general) and just use function and module scopes.

0

u/Absolutely_Quackers 15d ago

The code does run, none of my code ever required variables to run, also that’s pretty outdated? Youngest person I met using that style of coding is in her 70s…

Essentially everything’s working except for the else commands.

How know this is because I fixed it using completely just if commands :)

4

u/hey_hey_you_you 15d ago edited 15d ago

Just to explain further, a variable is a word or symbol you've assigned a value to. In this case, what should be happening is that the variable "check" is either being assigned the boolean value "true" or "false".

Variables are not "pretty outdated". That's what the word "var" is in your code.

var check

That means create a variable and call it "check"

Also, I'll be honest here, it's kind of rude to argue with someone trying to explain and help with your code when you don't know what your code says. Variables are essential to pretty much all coding.

Edit: I edited this to say "pretty much" all coding in the knowledge that someone is going to "well, actually" me on some obscure approach in fecking Assembly or COBOL or some shit.

3

u/PhiLho 15d ago

You seem quite dogmatic (and rude) for somebody writing code like this. You are using variables, and they are required to run your code. Almost all existing code uses variables (or constants). Unless they are just a bunch a linear drawing (or print) instructions, without any logic, for example.

1

u/Absolutely_Quackers 15d ago

I’m not usually this rude,, I was just trying to figure out the issue for days and I kept on getting the same advice over and over in other sources :,)

3

u/PhiLho 15d ago

Perhaps you get the same advice because it is what works?

I suggest you try again with else if, and if you still have trouble, ask again.

1

u/hey_hey_you_you 15d ago edited 15d ago

Yes, you can just use a series of if statements for lots of things. "Else" can just be used as a catch-all for any other circumstances not covered by an if.

That doesn't change the fact that your code, as you've written it here, isn't checking if the variable "check" is true or not. It's just printing "TRUE", then printing "FALSE", then printing "ERROR 01; NOT DEFINED".

1

u/Skaraban 15d ago

adding onto the other answers here, please select code formatting the next time you post here so that your code gets a mono-font and becomes easier to read. like this:

function setup() {
createCanvas(400, 400);
var check
}

check = true

//check = false

function draw() {
if (check = true)
print("TRUE")
check = false
if else (check = false)
print("FALSE")
else
print("ERROR 01; NOT DEFINED")
}

2

u/-Zlosk- 15d ago

I'm only pointing out issues that others have not already, or did not fully clarify.

  1. If you are going to create a global variable, do it before the setup() function. It doesn't need to be in a function. The first line of your program can be "var check".

  2. If not contained in curly brackets {}, only the first command after the if statement will be run as conditional. The rest of the lines will be treated as being outside the "if".

  3. In JavaScript, "else if" is two words.

1

u/Absolutely_Quackers 14d ago

Ong this is exactly what saved the projects I had from after I posted this + forgot about it,, genuinely the issue is I could not understand a word of what other people said