02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoPrevious Video

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>

Overview

We will learn about various conditionals related to ‘if’. The main focus is on the importance of brackets and conditionals such as if/else, if/else if…, nested if, if not etc.

04:14

Conditionals and Operators

Greater than the tutorials you'll find elsewhere, 02geek takes less time to teach how logical operators are directly linked to conditionals, and how conditional expressions can be formed using these o

04:49

if

We introduce the “if” construct and show how it can be used for making programming decisions while implementing the program logic

04:30

Brackets

We explain the importance of using brackets with if, mainly to combine a sequence of operations, all to be performed on the same condition

07:41

else

We introduce the language mechanism for providing alternatives in code; ie,. to make decisions when we have 2 options

07:29

Else-if: dealing with multiple conditions

We introduce the else-if construct, to help make logic decisions when more than 2 options are available

08:41

Nested if conditional construct

We introduce the nested if construct, useful when there are more than one criteria of decision making, including one inside another

09:08

Nested if’s – The Answer

We introduce another approach to nested if’s organization: mainly inverting the nesting levels of if constructs

04:44

Say it ain't so – the “If-Not” conditional

We introduce how to reorganize condition checking code using if constructs having Not (!) operators in the conditionals

02:38