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

Conditionals and Operators

<p>In our introduction to Conditionals, we saw how we use conditional expressions in real life. Now, a machine needs to be explicitly programmed, in a programming language to make such decisions. In this summary, we shall elaborate on the concepts of logical expressions and operators, which together form conditional expressions. Conditional expressions are heavily used in if-else constructs.</p> <p>A conditional is an entity which has a “Boolean value”, namely true or false. A conditional is constructed with logical expressions and operators. The operators used are of two types:</p> <p> • Comparison Operators:</p> <p>Perform arithmetic comparisons; namely, &lt;, &gt;, =, !=(not equal), &lt;= &gt;=</p> <p> • Logical Operators:</p> <p>Perform logical operations; namely, && (and), || (or) and ! (not)</p> <p>A Logical Expression is built using identifiers (variables), constants, comparison operators and other logical expressions. </p> <p>A Conditional is composed of 1 or more logical expressions, strung together with 0 or more logical operators.</p> <p>It's time to get a bit personal with a story from long ago. In 1984 Mary had a little lamb (by the way his name was Bobby) and she had a best friend,Ann. Ann was a farmer as well and had 4 lambs. It's a really long story that we won't get into, but actually Mary and Ann made a pact or as Mary said to Ann in the early bad hair days of the 80's : “Ann you know I love you but I needto go to Holland until 2014. When I come back if you have more lambs then me then we will eat one of yours.If I have more then you,mine will be on the table.”</p> <p>Yes, a very touching story–the girls didn't speak to each other again – but there is an “if” statement lurking in this tale... </p> <p>The pseudo code for the above example’s reasoning would look like:</p> <p>var maryLambs:integer = 1;</p> <p>var annLambs : integer = 4;</p> <p>// process logic for 30 years</p> <p>var eatMaryLamb:boolean = false;</p> <p>var eatAnnlamb:boolean = false;</p> <p>eatMaryLamb = (maryLambs &gt; annLambs);</p> <p>eatAnnLambs = (maryLambs &lt;= annLambs);</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