Break out of an if statement.

I'm trying to set up some code to break out of my if statement if another condition is met part way through running the statement. My only solution, which I'm yet to test, is to call the loop method from within the if statement if this condition is met, it compiles but surely this will eventually stack up multiple times? The break; used in switches and while loops wont wont compile so i cant use this.


There’s two things you can do:

  1. Place your “if” construct in its own function, and use “return” to break out of it, or
  2. Change how you are thinking about the flow of your program so you don’t need to break out of the “if”. The simplest way of changing your thinking is to, instead of thinking “I don’t want to run this if…” to “I only want to run this if…”

so if i had something like this...
if(reading1 > 0)
{ light 1 on
while(reading 2 == 0)
read sensor 2
read pir sensor
if (pirReading == 1)
if(pirReading == 1)
turn on light 2 on

...then light one and two would switch as the statements become true, but if the pir sensor is activated in the while loop this would it would break out of the loop, then return would exit the if statement without switching light 2 on?

pirsensor = 1will always be true