Brackets
<h2>The if-Construct: Use of Brackets</h2> <p>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. </p> <p>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.</p> <p>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 . </p> <p>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.</p> <p>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!</p> <p>Pseudo code would look something like:</p> <p>var question:string = “…â€;</p> <p>// some statements here, populate the variable question</p> <p>If(question == “what is your favorite thing?â€) </p> <p>{</p> <p>trace(“Girls in while dresses with blue satin sashes!â€); </p> <p>trace(“Snowflakes that stay on my nose and eyelashes!â€); </p> <p>trace(“Silver white winters that melt into streams!â€); </p> <p>trace(“These are a few of my favorite things!â€); </p> <p>}</p> <p>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: </p> <p>This wasn't true, the shock thing: the other statements would run anyway. NEENER I corrected code!!! GO ME! </p> <p>the rest of the statements will execute every time the program is run, no matter what question is asked! </p> <p>This concludes the study of a single-if clause. In future summaries, we shall also look at other constructs for condition-testing.</p>