Exiting an IF statement

I've read a few posts and the arduino guide on "if,else,else if" but I'm somewhat confused on what is required to leave an IF statement.

In my "code" below, i believe that as long as apple no longer equals red, the code will exit that section and continue to monitor the apple variable until it sees that it is red or green. As long as the variable isn't red or green, it will exist in the default 'else' state. Additionally, if the banana variable within apple is reading yellow and executing ABC, will it leave that if segment if apple no longer equals red but banana still equals yellow?

Apologies if my approach and example is foolish. I'm only trying to simplify the example.

If this isn't the case, do i need to use the 'break' argument?

read variable "apple"
if apple = red
  read banana
    if banana = yellow
       digitalWrite ABC
    else
       digitalWrite DEF
else if apple = green
  read orange
    if orange = color1
       digitalWrite GHI
    else
       digitalWrite JKL
else
  digitalWrite MNO

I’m somewhat confused on what is required to leave an IF statement

An if statement does not cause a loop so there is nothing to leave, unlike a while loop

Please post a full, real sketch that illustrates your problem
My advice would to always put { and } round dependant code blocks even if the code is only one statement and put the { and } on their own line and then Auto format the code in the IDE so that the structure shows. Try it with your pseudo code first

It is impossible from your “code” to determine what should happen as you cannot tell which else belongs to which if. Please describe what you want to happen

That fact that you don't have real program and what you have posted isn't real code makes it difficult to help.

In a real program loop() loops, if statements do not. So talking about things like if something is no longer some value makes little sense. An if statement does the check once and then either performs some actions or moves on to next bit of code. When all the code inside loop() has been run through then it will start again at the top and probably eventually get round to the if statement again, checking the now current state of the variables if anything has changed.

Steve

I haven't written the code yet as I'm trying to figure out the best method or structure for the overall program.

I'm trying to design a device that executes various tasks based on the status of momentary switches/buttons that will exist in various states. In the example below, if 'A' is TRUE, I only want the device to execute anything that exists within that section. If c and d are TRUE, i only want the device to execute anything within that section. If 1 and 2 and 3 are all true, I only want the device to execute anything within that section.

But if the loop is currently executing the stuff within the 1 and 2 and 3 loop and suddenly A is FALSE (and G is presumably FALSE also), will the loop exit A completely and begin executing anything within in the Z (default) section?

A
 c and d
   1 and 2 and 3
 e and f
   4 and 5 and 6


G
 H and I
   7 and 8 and 9
 J and K
   0 and 1 and 2


Z

Sorry, I'm not bad at reading code but random letters on a page (with not an if in sight) make no sense to me.

However I'm not sure you understand that values in program only change if the code does something. If a switch is pressed or released the code will do absolutely nothing different until after you read the pin the button is connected to. It won't magically just notice that something in the external world has changed.

Good luck - Steve

ok, i wasn't clear enough, my apologies

If A = true
if c and d = true
if 1 and 2 and 3 = true

In that structure, if all values were satisfied that the section of 1 and 2 and 3 were currently being executed, does the 1 and 2 and 3 section stay active if suddenly A were to go FALSE?

In that structure, if all values were satisfied that the section of 1 and 2 and 3 were currently being executed, does the 1 and 2 and 3 section stay active if suddenly A were to go FALSE?

With out a real program to evaluate I am going to use my crystal ball and say YES, the 1 and 2 and 3 section will stay active. That is, until the program exits that section, checks the value of A and determines it is FALSE.

If the 1/2/3 section is "blocking" it may take a period of time for the code to determine that A has changed to FALSE.

EDIT: remember that == is for comparison not =

Ok, i was finally able to get to the lab and actually test this out.

In the example below, sw1 turns light8 on (as intended). As long as this is TRUE, I am able to turn on light7 as well (as intended). However, when i turn off light8 (sw1 == HIGH), light7 remains on (even after i turn that respective switch off). Additionally, when i turn off the switch (sw2), light7 remains on.

How do i exit (disable?) out of everything within the first if statement?

int sw1 = 52; 
int sw2 = 50; 

int val1 = 0;
int val2 = 0;

int light7 = 7;
int light8 = 8;




void setup() {
  pinMode (sw1, INPUT_PULLUP);
  pinMode (sw2, INPUT_PULLUP);

  pinMode (light7, OUTPUT);
  pinMode (light8, OUTPUT);
}


void loop() {
  val1 = digitalRead(sw1); //52
  if (val1 == LOW) {
    digitalWrite(light8, HIGH);
    val2 = digitalRead(sw2); //50
    if (val2 == LOW) {
      digitalWrite(light7, HIGH);
    }
    else {
      digitalWrite(light7, LOW);
    }
  }
  else {
    digitalWrite(light8, LOW);


  }


}

However, when i turn off light8 (sw1 == HIGH), light7 remains on (even after i turn that respective switch off). Additionally, when i turn off the switch (sw2), light7 remains on.

If light7 does not turn off with sw2 while light8 is on from sw1 it sounds like there is a wiring error.

To have light7 turn off with light8 you need to explicitly turn if off in the else{} block which turns of light8.

void loop() {
  val1 = digitalRead(sw1); //52
  if (val1 == LOW) {
    digitalWrite(light8, HIGH);
    val2 = digitalRead(sw2); //50
    if (val2 == LOW) {
      digitalWrite(light7, HIGH);
    }
    else {
      digitalWrite(light7, LOW);
    }
  }
  else {
    digitalWrite(light8, LOW);
    digitalWrite(light7,LOW);//turn of light7 along with light8
  }
}

What is the requirement? Why are the ifs nested at all?

cattledog:
If light7 does not turn off with sw2 while light8 is on from sw1 it sounds like there is a wiring error.

To have light7 turn off with light8 you need to explicitly turn if off in the else{} block which turns of light8.

void loop() {

val1 = digitalRead(sw1); //52
 if (val1 == LOW) {
   digitalWrite(light8, HIGH);
   val2 = digitalRead(sw2); //50
   if (val2 == LOW) {
     digitalWrite(light7, HIGH);
   }
   else {
     digitalWrite(light7, LOW);
   }
 }
 else {
   digitalWrite(light8, LOW);
   digitalWrite(light7,LOW);//turn of light7 along with light8
 }
}




Without changing any wiring, your code worked as intended. 

However, in the code that i posted, light7 stayed on even though i turned off sw1. This tells me that any IF statements nested will remain active even if the parent if statement turns FALSE. Granted they won't be active per se but they will remain in the their current state despite the parent state turning to false.

wildbill:
What is the requirement? Why are the ifs nested at all?

The requirement is that the device recognizes the status of 7 switches/sensors and moves servos, lights and a dc motor accordingly. There is a main switch, 3 sub switches and 3 break beam sensors. Outputs include 4 servos and a dc motor. I want the device to read the main switch first, then the status of the 3 sub switches. After that it gets a bit complicated.
As to why they are nested? I most likely don't know any better. I'm still pretty new to coding. I'm better than I used to be :- D

Nesting conditional statements is hard to understand, error-prone to alter. Its usually an
indication you could be thinking things out differently, perhaps splitting the code into smaller
functions, perhaps coding up as a state-machine.

If you do have code with lots of guard expressions before it does stuff, consider using return:

void maybe_do_complex_stuff ()
{
 if (! condition1)
   return ;
 if (! condition2)
   return ;
 // now we know where we are and can do stuff
 do_stuff () ;
}

void do_complex_stuff ()
{
 ...
}

Note the use of lots of little functions with descriptive names, note the escaping with return to avoid
nesting the conditions.

BTW using unnecessary variables is clutter and makes code harder to read, here's a classic example
that you've done:

  val1 = digitalRead(sw1); //52
  if (val1 == LOW) {
    ..

This is much better like this:

  if (digitalRead (sw1) == LOW) {
    ...

Can you see why? You don't need to remember what val1 means, and there's one less line of
code - both are wins.

Vampyrewolf:
The requirement is that the device recognizes the status of 7 switches/sensors and moves servos, lights and a dc motor accordingly. There is a main switch, 3 sub switches and 3 break beam sensors. Outputs include 4 servos and a dc motor. I want the device to read the main switch first, then the status of the 3 sub switches. After that it gets a bit complicated.
As to why they are nested? I most likely don't know any better. I'm still pretty new to coding. I'm better than I used to be :- D

You definitely need to read about state machines and draw out state-transition diagrams for your system. That's
how we deal with complex interactions with hardware. You may need to have nested state machines if its really complex.

Once you've got a diagram you can think things through more easily, and coding is much simpler. First step
is identifying the various notional states the system (or sub-systems) can be in.