Forgot username/password? Don't have an account?
Login with Facebook
Home » Courses » Conditionals 1 - 'if' » Brackets
Subscribe for updates.
* New to us? log in with Facebook and get a free day pass

Brackets

The if-Construct: Use of Brackets

If-constructs in code determine the flow of a program. If the conditional value in an if-clause evaluates to “true”, then one statement is executed; if the result is false, then the statement is not executed.

Often, we may want to execute a sequence of operations inside an if-clause. A pair of “Curly Braces”: { } are provided to group the statements together.

If we omit curly braces and there'smore than 1 statement in the if-block, the first statement is executed if the conditional evaluates to true. The rest of the statements are always executed, irrespective of the conditional .

A conditional expression evaluates to a boolean value (true / false). If we store the value of the evaluation in a boolean variable, and then use that variable in an “if” conditional, then the code becomes more easily readable. The variable retains its value until it is re-assigned.

For example: suppose veteran actor Julie Andrews were asked a simple question in the middle of an interview session, namely, “What is your favorite thing?” You would be in for a shock if you expected a single answer!

Pseudo code would look something like:

var question:string = “…”;

// some statements here, populate the variable question

If(question == “what is your favorite thing?”)

{

trace(“Girls in while dresses with blue satin sashes!”);

trace(“Snowflakes that stay on my nose and eyelashes!”);

trace(“Silver white winters that melt into streams!”);

trace(“These are a few of my favorite things!”);

}

If we had not used the pair of curly braces, the only time the first trace would be executed would be if the question matched:

This wasn't true, the shock thing: the other statements would run anyway. NEENER I corrected code!!! GO ME!

the rest of the statements will execute every time the program is run, no matter what question is asked!

This concludes the study of a single-if clause. In future summaries, we shall also look at other constructs for condition-testing.

Got A Question?

'I love being Prime'

Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.

Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.
Act now