HTML Blox

Providing the building blox of the web.

  • HTML Blox Home
  • Blog
    • Design
    • Flash
    • General
    • HTML
    • Javascript
    • PHP
    • Programming
    • Web Servers
  • Forums
  • Where to Start?
  • Lessons
    • CSS
    • Design
    • HTML
    • Javascript
    • Programming
  • References
    • HTML
  • Resources
  • About
  • Contact
  • Go Green!

Programming Lessons

  1. Lesson 1: The Basics
  2. Lesson 2: A Brief History of Computers
  3. Lesson 3: The Workings of Computers
  4. Lesson 4: A Brief History of Programming
  5. Lesson 5: High-Level vs. Low-Level
  6. Lesson 6: Compiled vs. Interpreted
  7. Lesson 7: Variables
  8. Lesson 8.1: Arithmetic and Operators - Decimal
  9. Lesson 8.2: Arithmetic and Operators - Integer and Boolean
  10. Lesson 9.1: Conditional Statements - The Basics
  11. Lesson 9.2: Conditional Statements - Branching Conditionals
  12. Lesson 10.1: Loops - The Basics
  13. Lesson 10.2: Types of Loop
  14. Lesson 11: Functions
  15. Lesson 12: Strings
  16. Lesson 13: Procedural vs. Object-Oriented

Programming Reference

  1. No reference yet.

Links

  • W3C HTML 4.01 Strict Compliant
  • W3C CSS 2.1 Compliant
  • Web Standards Group Member
  • Follow us on Twitter
  • Number Overflow - Free Advanced Scripts and Tutorials
  • Azure Ronin Web Design - Professional Web Design and Development
  • XML Sitemap
  • U Comment, I Follow

Feeds

  • RSS 2.0 Feed
  • RDF/RSS 1.0 Feed
  • RSS 0.9 Feed
  • Atom Feed

Lesson 9.2: Conditional Statements – Branching Conditionals

del.icio.us Digg Blinklist reddit

In the previous part of this lesson we looked briefly at what a conditional statement is in simple terms. In this portion, we’ll take a look at the simpler types of conditional statements, the branching conditionals.

What are Branching Conditionals?

As we stated in the last portion, branching conditionals are conditional statements that have “branches” (though sometimes it’s only one branch), and performs only the actions in on branch based on the condition. The common type of branching conditional statements are if, if…else, and switch.

If Statements

If statements are statements that take a condition, and perform the action if the condition evaluates to true. You can remember this by making it a simple sentence:

“If something is true then do this action.”

In pseudo-code, we would write a condition like this:

If bob = true Then
    Print "It's Bob!"
End If

This is only pseudo-code, but gives the basic idea. “bob == true” is the condition we are checking. The action we perform is to print out “It’s Bob!”. We then have an End If signifying that we are done with this branch. We can have multiple statements in the If statement. We could also have multiple actions within the If, which is why we have the End If. For example, here is something that prints three messages:

If bob = true Then
    Print "It's Bob!"
    Print "Hello Bob!"
    Print "Bye Bob!"
End If

We can also have compound conditions within our condition as well. For example:

If bob = true AND bob_is_here = true Then
    Print "Bob is finally here!"
End If

So, if you follow your Boolean arithmetic we would get the following results based on our variables:

bob bob_is_here Result
true true Prints
true false Nothing
false true Nothing
false false Nothing

You can create as crazy a conditional as you want within the If statement (or any other conditional statement). It will still boil it all down to either true or false and take the action if it is true.

Now, I said that the If statement is a branching condition, but it only has one branch. To give it multiple branches we’ll need to add in the Else statement.

If…Else Statements

The Else statement let’s you perform an action if the previous If statement was false. To add on to our simple sentence from earlier:
“If something is true then do this action, else do this other action.”

Here is an example If…Else:

If bob = true Then
    Print "Bob is here!"
Else
    Print "Bob is gone!"
End If

Now, if bob is true, it will print “Bob is here!”. Unlike last time, if bob is not true (so bob is false), it will print “Bob is gone!”, instead of just doing nothing. Now, we have two branches in our statement.

We can also chain together If…Else statements, which allow us to add as many branches as we want. For example:

If bob_is_here = true Then
    Print "Bob is here!"
Else If tom_is_here = true Then
    Print "Tom is here!"
Else
    Print "Nobody is here!"

Now, this can be a bit tricky. Just remember that it will only perform one of these branches, and it will perform the first one it finds is true. So, here is our table of different values for our variables and what our result is:

bob_is_here tom_is_here Result
true true “Bob is here!”
true false “Bob is here!”
false true “Tom is here!”
false false “Nobody is here!”

Notice how if bob_is_here is true, regardless of what tom_is_here is, the result is always “Bob is here!” because it’s first. If bob_is_here is false, then we might say “Tom is here!” (if he is), or we will say “Nobody is here!” if our other statements don’t evaluate true.

Keep in mind that our examples have an Else at the end to catch it if we don’t do anything, but we don’t have to have one. We can just have an If and Else If, without a final Else if we don’t want it to do anything if both aren’t true.

Switch Statements

Our final common branching conditional is the Switch statement. Switches are essentially the same in function as If…Else If…Else statements, but differ in form.

A Switch statement takes a value (not just a Boolean), and then compares it’s value to a number of Cases which you then define. Some languages only allow you to use integer (whole number) values, while others allow you to use just about any type. For our pseudo-code, we’ll assume we can only use integers.

Switch myValue
  Case 0:
    Print "Your value is zero."
  End Case
  Case 1:
    Print "Your value is one."
  End Case
  Default Case:
    Print "Your value isn't one or zero."
  End Case
End Switch

In this example, if the value of myValue is 0, it will print “Your value is zero.” If the value is 1, it’ll print “Your value is one.” If it is neither of these, it will use the Default case and print “Your value isn’t one or zero.” The Default case is basically the same as an Else. You can write this same switch statement as an If…Else If…Else statement as well:

If myValue = 0 Then
  Print "Your value is zero."
Else If myValue = 1 Then
  Print "Your value is one."
Else
  Print "Your value isn't one or zero."
End If

The beauty of the switch statement doesn’t really shine when you have only a few cases, but if you have a larger number, then the switch because much easier to write then a large compound If…Else If…End If statement.

Nesting

The final topic we’ll cover in this lesson is nesting. Nesting is the term used when you put one (or more) conditional statements inside of another (you create nested conditional statements).

There isn’t really anything surprising about nesting branching statements. Each conditional statement will still perform it’s own block, and each nesting will perform it’s own. For each level you would have one branch performed. Here is an example:

If bob = true Then
  If bob_is_here = true Then
    Print "Bob is here!"
  Else
    Print "It's Bob, but he isn't here!"
  End If
Else If tom = true Then
  If tom_is_here = true Then
    Print "Tom is here!"
  Else
    Print "It's Tom, but he isn't here!"
  End If
Else
  Print "It's nobody!"
End If

Now, in this case our first level has three branches (If bob = true, If tom = true, Else). The first two branches then have a nested If statement, which also contain three branches. Before you take a look at the table below, see predict what you think would be the result for each table and see if you get it right. You can also have as many nested levels as you want, and mix branching and looping conditionals, or anything else for that matter.

Here are the results of our last example, depending on our variables:

bob tom bob_is_here tom_is_here Result
true true true true “Bob is here!”
true true true false “Bob is here!”
true true false true “Bob is here!”
true true false false “Bob is here!”
true false true true “It’s Bob, but he isn’t here!”
true false true false “It’s Bob, but he isn’t here!”
true false false true “It’s Bob, but he isn’t here!”
true false false false “It’s Bob, but he isn’t here!”
false true true true “Tom is here!”
false true true false “It’s Tom, but he isn’t here!”
false true false true “It’s nobody!”
false true false false “It’s nobody!”
false false true true “Tom is here!”
false false true false “It’s Tom, but he’s not here!”
false false false true “It’s nobody!”
false false false false “It’s nobody!”

In this lesson we discussed the common types of Branching Conditional Statements. In our next lesson we’ll talk about the other type of conditional statements: loops.

This entry was posted on Monday, November 9th, 2009 at 1:27 pm and is filed under Programming Lessons. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Traceback or Pingback to "Lesson 9.2: Conditional Statements – Branching Conditionals"

  1. Pingback from HTML Blox » Programming » Lesson 9.1: Conditional Statements – The Basics
    November 9th, 2009 at 2:15 pm

Leave a Reply

Please Login or Register or fill out the following to leave a reply:

If you haven't posted an approved comment before, your comment will not show up until approved.

  • HTML Blox Home
  • Blog
  • Design Blog
  • Flash Blog
  • General Blog
  • HTML Blog
  • Javascript Blog
  • PHP Blog
  • Programming Blog
  • Web Servers Blog
  • Forums
  • Where to Start?
  • Lessons
  • CSS Lessons
  • Design Lessons
  • HTML Lessons
  • Javascript Lessons
  • Programming Lessons
  • References
  • HTMLReferences
  • About
  • Resources
  • Contact
  • Go Green!
  • Sitemap

Copyright © 2008 HTML Blox

Unless otherwise noted, all images created and copyright HTML Blox.

Operated by Azure Ronin Web Design