using the |= and OR operators

Hi Arduini,
I’m pretty new to programming but getting there. I have some code that provides conditional outcome on whether or not the number 0 or 1 are stored in EEPROM. I’ve tried to use the following variations or the “not equals” |= and “OR “||” operators as follows:

In this code the first time I run the code with a new ATMEGA 328pu processor in an Arduino pro-mini type configuration (running at 3.3v and 8mHz clock), I get, as expected print out, “no 0 or 1 detected” then “this is the eeprom contents” “255” (contents of an unused EEPROM memory slot). I then get the printout “should have written 1 in here” followed by a “1”.

#include <EEPROM.h>
int toggleState = 0;
int x= 1;
int y =0;

void setup() {
toggleState =;
Serial.println(“this is the eeprom contents”);
Serial.println (toggleState);

if ((toggleState != x) || (toggleState != y)){ // if first use - or EEPROM corrupted
//if (toggleState != x || toggleState != y){ // if first use - or EEPROM corrupted
//if ((toggleState != 0) || (toggleState != 1)){ // if first use - or EEPROM corrupted
//if (toggleState != 0 || toggleState != 1){ // if first use - or EEPROM corrupted

Serial.println(“no 0 or 1 detected”);
EEPROM.write(350,1);//write 1 (ON) to memory
int readToggle =;
Serial.println(“should have written 1 in here”);
Serial.println (readToggle);



void loop() {


So far so good.

But in following attempts I get
“this is the eeprom contents” “1” followed by the unexpected
“no 0 or 1 detected” then “1”.

I tried the (commented out) variations following the if statement - but always the same result.

Clearly, the if statement IS being satisfied – so I must be doing something wrong with the “|=” and “||” operators. But I just can’t work out what it might be – can anyone help?
All the best,

          if ((toggleState != x) || (toggleState != y)){ // if first use - or EEPROM corrupted

Comparing the value read from EEPROM to 0 or 1 will NOT corrupt EEPROM.

Suppose that you read a 12. That is not 0 or is not 1, so you write a 1.

Then, you read 1. That is not 0, so you write a 1.

You want to say "if the value is not 0 AND is not 1,...".

Thanks PaulS, your suggestion works - I have misunderstood the OR operator. I did not think my code was somehow corrupting the EEPROM, just that I wanted to cover that possibility - the code is primarily for first time use in a conditional (switch) setting.
Greatly appreciated.