What is the programming rule for { brackets for a list of if statments

Hi,
Sorry to post for such a simple thing but I just can't get the number of { right and I can't find the programming rule to dictate when and when not to {. I find different examples with different amounts of { .
Ok so it sounds more complicated trying to explain. My project is simple have a sprinkler come on for a squirt then off for a long time. I had that working fine. Now I want 3 buttons to help fine turn the watering when in operation so 1 button will increase the delay between squirts and button 3 will shorted the time between squirts, and button number 2 can give me a squirt now.
Simple i thought I'd try out the button Library so added that, and using blinkwithoutdelay for my main squirt wait cycle.
Button presses call sub-routines to increase or decrease the "stayOFF" varriable.

So my problem is in the void loop i have a list of IF statements and I can't get the { and } in the right places to run through every if and still run through the Blinkwithoutdelay bit

if (squirt.isPressed())
{ //this is where they can go on a single if statement
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
}

So below is my code if anyone can help and explain the programming rule I would be grateful :*

#include <Button.h>
#include <Arduino.h>


Button squirt = Button(8,PULLDOWN);
Button more_frequent = Button(10,PULLDOWN);
Button less_frequent = Button(9,PULLDOWN);
int time = 1000;
int offtime = 5000;
int thing = 13;
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval =1000;
long stayON = 1000;           // interval at which to be on  (milliseconds)
long stayOFF = 5000;           // interval at which to off longer than on (milliseconds)


void setup(){
  pinMode(thing, OUTPUT);
}
void more(){
  stayOFF = stayOFF + 10000;
}
void less(){
  stayOFF = stayOFF - 10000;
}


void loop() {
  if (squirt.isPressed()) 
  {
    digitalWrite(13,HIGH);
    delay(1000);
    digitalWrite(13,LOW);
  }
  if (less_frequent.isPressed()) 

    less();

  if (more_frequent.isPressed()) 

    more();

  unsigned long currentMillis = millis();

  if (ledState == LOW) 
    interval = stayOFF;
  else
    interval = stayON;

  if(currentMillis - previousMillis > interval) 
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW)
    ledState = HIGH;
  else
    ledState = LOW;
  digitalWrite(thing, ledState);
}

Search google for "C++ Compound statements"

In the loop you have the brace the wrong way round. The matching one is also the wrong way round.
Rule, open brace { first must have a matching closing brace }
Any statements within two braces are treated as one statement.

thank you for the speedy replies
I read the C++ on Compound statements and on If statements and have added a couple to the if statements that have an else.
It compiles and runs with the LED (standing in for squirt) fashing evanly on and off so I added a Print. statement to try to de bug and I see that the button increases the stayOFF varriable but the blinkwithoutdelay part is not acting on it, can you see why.

#include <Button.h>
#include <Arduino.h>


Button squirt = Button(8,PULLDOWN);
Button more_frequent = Button(10,PULLDOWN);
Button less_frequent = Button(9,PULLDOWN);
int time = 1000;
int offtime = 5000;
int thing = 13;
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval =1000;
long stayON = 1000;           // interval at which to be on  (milliseconds)
long stayOFF = 5000;           // interval at which to off longer than on (milliseconds)


void setup(){
  pinMode(thing, OUTPUT);
  Serial.begin(9600);   // activate the serial output connection

}
void more(){
  stayOFF = stayOFF + 1000;
}
void less(){
  stayOFF = stayOFF - 1000;
}


void loop() {
  if (squirt.isPressed()) 
   digitalWrite(13,HIGH);
    delay(1000);
    digitalWrite(13,LOW);
  
  if (less_frequent.isPressed()) 

    less();

  if (more_frequent.isPressed()) 

    more();

  unsigned long currentMillis = millis();

  if (ledState == LOW) 
   { interval = stayOFF;
   }
   else
    interval = stayON;

  if(currentMillis - previousMillis > interval) 
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW)
{ ledState = HIGH;
}
else
    ledState = LOW;
  digitalWrite(thing, ledState);
  
   Serial.print("stayOFF time ");
   Serial.print(stayOFF);

}

Thank you for your time. It is appreciated.

If you use the IDE's auto format tool, your indentation will be sorted out, and the structure of your code will become clearer.

As you are new to programming, it may be easier to use {} on all 'if' and 'loop' statements, avoids having code left out of an if.
use a format of your choice, or as AWOL said, let the IDE do it for you.

if( statement ){
   //This line indented indicating its part of the 'if'
}
//Code not indented as not part of 'if'

So with your code:-

if (squirt.isPressed()) 
   digitalWrite(13,HIGH);
    delay(1000);
    digitalWrite(13,LOW);

ONLY the digital write high to pin 13 is conditional the other three lines will always be executed.
You should do:-

if (squirt.isPressed())
  {
   digitalWrite(13,HIGH);
    delay(1000);
    digitalWrite(13,LOW);
  }

To answer the original question:

The rule is:

If you have more than one statement controlled by the IF, then you MUST use { and } to group those statements.
If you have ONLY ONE statement controlled by the IF, then you CAN use { and } around it, but you don't have to.

For example:

Correct:

if(case == true)
{
  doSomething();
  doSomethingElse();
  variable--;
}

Incorrect:

if(case == true)
  doSomething();
  doSomethingElse();
  variable--;

In the second example, doSomething() will only run if case == true, but doSomethingElse() and variable-- will ALWAYS run.

The same rule holds true for ELSE, ELSE IF, WHILE, FOR, etc.

Hi
Thanks Majenko and Grumpy_mike you both pointed me to the same thing that the { braces (as I now know to call them) contain ALL the statements to execute for a single if statement.

back to the original post Majenko I wasn't actually thinking of the problem that you and Grumpy_mike pointed out I was thinking of the fact that I have consecutive If questions.
The reason being that my sketch seems to deal only with the first.
The Print. debug i added show the stayOFF variable is being changed but this bit

if (ledState == LOW) 
    interval = stayOFF;
  else
    interval = stayON;

  if(currentMillis - previousMillis > interval) 
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW)
    ledState = HIGH;
  else
    ledState = LOW;
  digitalWrite(thing, ledState);

doesn't seem to change the length of the off period.
Any thoughts

Only change the value of interval when you change the state of the LED, not all the time like you are doing at the moment.

maxmaisy:
Any thoughts

There are different coding styles that suit different people.

The one I prefer is one that I think makes the control structure of your code clearest and makes it easiest for you to understand the structure and see where you have got it wrong.

When you use any flow control structures (if, while, do, for and so on) always follow them with a compound statement. In other words, put an opening brace { followed by a closing brace } and then put any code that you want to be controlled in between the braces. (Even if there is only a single statement between the braces, always use the braces.)

Put each { and } on a separate line with matching pairs indented by the same amount and the code between the indented one extra level.

Grumpy_mike, your last advice

Grumpy_Mike:
Only change the value of interval when you change the state of the LED, not all the time like you are doing at the moment.

Is a little cryptic for me

/*  if (ledState == LOW) 
  { 
    interval = stayOFF;
  }
  else
    interval = stayON;
*/
  if(currentMillis - previousMillis > interval) 
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW)
  { 
    ledState = HIGH;
    interval = stayON;
  }
  else
    ledState = LOW;
    interval = stayOFF;
  
  digitalWrite(thing, ledState);

Is this what you were meaning?
The LED flips but the buttons do nothing, I'm clearer on the compound structure but it hasn't help me figure out why my sketch isn't working perhaps it has nothing to do with the compound structure.

  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW)
  { 
    ledState = HIGH;
    interval = stayON;
  }
  else
    ledState = LOW;
    interval = stayOFF;

I'm troubled by the lack of braces here where the indentation suggests you need them.

If you want to do two or more things after an "if" or "else" you need braces.

This is what your code is really doing:

  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW)
  { 
    ledState = HIGH;
    interval = stayON;
  }
  else
  {
    ledState = LOW;
  }
  
  interval = stayOFF;  // unconditional

Is this what you were meaning?

Yes.

Thank you one and all the problem was the braces. As well as all the other points previously mentioned.

  if(currentMillis - previousMillis > interval){ //**this brace was missed**
    // save the last time you blinked the LED 
    previousMillis = currentMillis;      
  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW)
   { 
    ledState = HIGH;
    interval = stayON;
   }
  else
   {  
    ledState = LOW;
    interval = stayOFF;
    }
  digitalWrite(thing, ledState);
  }// ** and its partner here**

When copying the Blinkwithoutdelay I missed the fact that this whole section as pasted above is in braces.
I hadn't found it to start with because of this compound statement. with out the braces the void loop read the delay every pass.

  if (squirt.isPressed()){ 
    digitalWrite(13,HIGH);
    delay(1000);
    digitalWrite(13,LOW);
   }

When I added the braces as Grumpy-mike said the Void loop no longer used this delay was skipped and the output flickered. So seemed to be getting further away from a solution.
Well thank you I hope my conclusion helps others to learn as much as I do.

I'm a hobbiest programmer so I don't do it every day. Unmatched braces can be frustrating so I've developed a couple of strategies to keep them straight. My main strategy is to always type them out first before adding code. Whether a function or if statement and even just regular brackets always get written out first.

If I'm starting a new function I'll type out:

int newFunction(){}

Then I go back and open the braces with the cursor and enter key. Then I start putting in code. If a function grows a little too long I'll even use comments to label the braces. Especially the function end brace.

I find the Arduino IDE helps too. The ctrl-t formatting helps a lot. It will fail with mismatched braces. It formats things nicely. Compiling frequently also spots errors. I find I hit ctrl-s, ctrl-t, ctrl-r frequently.

I also start new version often. I first name a sketch sketch000. Then if I want to take a big swing at the code I save a new version (ie; sketch001) and work on that. If I get it screwed up I just fall back to the last one.

I also comment a lot. I learned that after going back to code I wrote myself and having trouble figuring it out.

I'm sure the pros here have their own strategies for keeping stuff straight. Thing is you need a strategy for some of this stuff. One that becomes routine. Like a good pre-shot routine in golf.

I just would like to point out that

{ ... }

is NOT equivalent to a single statement. It groups a bunch of statements into a block.

do
{ ... }
while (false);

is equivalent to a single statement though.

Have a look here mikeash.com: Friday Q&A 2010-12-31: C Macro Tips and Tricks for the subtle difference.

My main strategy is to always type them out first before adding code.

Good strategy I use it myself.
However if you move your cursor to the open brace the matching closing brace will be highlighted with a rectangle round it. So you can instantly see what is in between.

Jimmy60:
If I'm starting a new function I'll type out:

int newFunction(){}

Hit return a couple more times while you're typing that in and you'll get a layout which makes it much easier to see and understand the control structure:

int newFunction()
{
}