Why don't I always need { } after an if () statement...

Why don’t I always need { } after an if statement…

I’m using Java Script (Unity Script).

Slowly getting my head around how to use Arrays.

Anyway, onto my question… in the below code how comes my first ‘if’ statement I need to use { } to contain the action. but not in my second.

if (showNumber > 5){
showNumber = 0;
}

The below does not require any { } .

if (showNumber > 0) print (weapon[showNumber]);

Here’s my full code for reference. Simply clicking a button which cycles through a array and displays different words.
At the moment it stops showing the appropriate number after cycling up to 5. Even though the showNumber variable keeps cycling from 1 to 5. Hmm… the learning continues.

#pragma strict

var weapon : String [];
var showNumber : int;

function Start () {
weapon = ["empty","dog","poision","gun","wallaby","devil"];
}

function Update () {
if (Input.GetButtonDown ("Fire1")) {
//add one to the showNumber variable every time the button is clicked
showNumber++;}

//if showNumber is bigger than 5, reset it to 0.
if (showNumber > 5){
showNumber = 0;
}
//if the number is great than two, print the weapon name.
if (showNumber > 0) print (weapon[showNumber]);

}

The above full code runs without errors.

thanks for any help you can give, i just want to understand why it works so I know what to do next time and why. Is it purely like this when your using ‘print’, or something else?

the {} simply defines a scope. If you don’t have them in regards to loops and if statements, it is impossible to define multiple lines to run. If you have multiple lines that should run if an if is true, and don’t use a scope, only the first line will be conditional. The rest of the lines will run with no regards to the state of the if statement.

if (true) 
Debug.Log("Is true");
Debug.Log("This will always print, no matter if the if is true or not"); //EDIT: This  doesn't describe the result of the above print, only this print

if (true)
{
Debug.Log("Is true");
Debug.Log("This only print if true");
}

for (int i = 0; i < 5; i++)
Debug.Log("Iteration: " + i);
Debug.Log("This print will only print once"); //EDIT: This  doesn't describe the result of the above print, only this print

for (int i = 0; i < 5; i++)
{
Debug.Log("Iteration: " + i);
Debug.Log("This will print each time the loop runs an iteration");
}

As for you question, this:

if (showNumber > 5){

showNumber = 0;

}

-could just as well be written like this:

if (showNumber > 5)

showNumber = 0;

As you only have one line, it would surely only run, if the if is true. But… I ALWAYS puts stuff in scopes, as it simply makes the code more readable to me.

In case when “if” statement contains only one line of code you can write it without brackets { }.
It is the same:

if (showNumber > 5){
   showNumber = 0;
}

and

if (showNumber > 5)
   showNumber = 0;

These both contructions are absolutelly correct and works well. But second is a little bit shorter.

Thank you both so much… its obviously a simple aspect of coding but I’ve some how missed a breakdown of why and when we should use {} .

Cheers for sharing and explaining so clearly, really appreciate it.

I guess I’ll generally use {} all the time when dealing with loops and if’s to make for tidy readable code.

To your second problem:

It doesn’t stop when it reachs 5. But you just start printing when showNumber > 0. The index in an array goes from 0 to Length - 1. In your case from 0 to 5. So when you reset showNumber to 0 you’re update method stops printig because you check for ‘greater than’.

Change this to ‘greater or equal’ than it prints all the time:

if (showNumber >= 0) {
    print (weapon[showNumber]);
}

Thanks for this, but it doesn’t seem to solve the problem.

For some reason the print shows the different words in the array up to the 5th (“devil”) then the number resets to 0 and cycles up again but the print remains the same “devil” and does not show the first word etc.

So not sure what needs changing, it seems the print bit of code stops being read once the variable loops back to 0, even when I change the code as you suggest.

#pragma strict		

var weapon : String [];
var showNumber : int;

function Start () {
weapon = ["empty","dog","poison","gun","wallaby","devil"];
}

function Update () {
if (Input.GetButtonDown ("Fire1")) {
//add one to the showNumber variable every time the button is clicked
showNumber++;}

//if showNumber is bigger than 5, reset it to 0.
if (showNumber > 5)
showNumber = 0;

//if the number is equal to or great than zero, print the weapon name.
if (showNumber >= 0) {
print (weapon[showNumber]);}

}

Have you checked if you simply have the console set to “Collapse”? If this button is on in the console, it will only show different print. So as soon as you have cycled through the first time, you are just re-printing the same values again, which won’t be shown?

Spot on… thank you… that could of kept me rewriting my script for an eternity… many thanks to you and the other guys for your replies. So much to learn… exciting times though… I’ve got a working prototype of the game I want to make (made with a lesser SDK) … one day (many months from now) it will be recreated in Unity.