Flow ControlConditionals


We use conditional statements in every day life all the time. Things like, "if you don't clean up your room, you're grounded." Or, "If you loved me, you would." There are always at least two parts to it. The first is the statement of the condition, the "if you..." part. The second is the statement that describes what happens if the condition is met.

If...Then

If...Then is a simple statement and a real workhorse. It works like this:

If 1 > 2 Then    // do something interesting End If 


If...Then...Else

If...Then...Else is the enhanced version of If...Then. Parents sometimes use an abbreviated form of this conditional, the old "clean up your room, or else." While leaving off the rest of the sentence does lend dramatic intensity when chiding your child, it has little effect when programming. The If...Then...Else statement provides a response whether the expression evaluates to true or False.

If 1 > 2 Then    // do something interesting Else    // do something even more interesting End If 


If...Then...Elseif...Then

The addition of Elseif to the vocabulary means that you can write as many conditional statements as you care to type. As soon as one of the conditions evaluates to true, the code exits the statement. The optional Else serves as the catch-all, handling "all other" situations.

If 1 > 2 Then    // do something interesting Elseif 2 = (1 + 1)    // do something even more interesting Else    // do something for all the conditions you haven't thought of End If 


Select Case

Writing out long lists of If...Elseifs can be tedious. There's a lot of typing to be done and it can be hard to read. REALbasic provides the Select...Case statement as a much more efficient and manageable alternative. As the name implies, Select is designed to provide a list of conditions to be evaluated, called Cases. The code in the first Case to evaluate to true is executed; then the program resumes operations after the end of the statement. This means only one of the conditions is acted upon, even if there are more than one condition that would evaluate to true.

Dim I as Integer Dim b as Boolean i = 2 Select Case i Case 1   b = False Case 2   b = True Case 3   b = False Else   b = False End Select 


In this example, you see the basic syntax of the Select statement. It always starts with Select Case followed by a variable or expression. When it is a variable, like it is in the previous example, you are testing for the value of the variable i. The following Case statements represent the alternative actions to be taken, depending on what the value of i is. In this case, i equals two, so the program first evaluates Case 1, which can be read as "In the case that i is equal to one." It doesn't equal one, so the next Case is evaluated. Because i does, in fact, equal two, the program executes the code that follows, setting the value of b to TRue. After that code is executed, all other processing of the Select statement ends.

Because the Select statement stops as soon as it finds a Case that evaluates to TRue, it will stop checking Cases at the first instance and will ignore the following cases, regardless of their status.

Select Case True Case (1 > 0)   // True Case (2 > 0)   // True Case (3 < 0)   //False End Select 


In this example, a series of three expressions are being evaluated to see if they are true. The first two Cases are trueone is greater than zero, and two is also greater than zero. The program will evaluate the first Case, execute the code for that Case, and then skip the rest to pick up processing after the end of the Select statement.

This can be used to your advantage, as long as you have the expressions evaluated in the proper order. The following example uses a Select statement to simplify the process of testing whether a value falls within a range.

Dim x as Integer Dim s as String x = 5 // x > 4 and x <= 5 Select Case True Case x > 7   s = "x is greater than 7" Case x > 6   s = "x is greater than 6" Case x > 5   s = "x is greater than 5" Case x > 4   s = "x is greater than 4" // 5 is greater than 4 Case x > 3   s = "x is greater than 3" Case x > 2   s = "x is greater than 2" Case x > 1   s = "x is greater than 1" Else   s = "x is less than or equal to 1" End Select 


This is the equivalent to the following:

Dim x as Integer Dim s as String x = 5 // x > 4 and x <= 5 Select Case True case x > 7   s = "x is greater than 7" case (x > 6) and (x <= 7)   s = "x is greater than 6" case (x > 5) and (x <= 6)   s = "x is greater than 5" case (x > 4) and (x <= 5)   s = "x is greater than 4"// 5 is greater than 4 case (x > 3) and (x <= 4)   s = "x is greater than 3" case (x > 2) and (x <= 3)   s = "x is greater than 2" case (x > 1) and (x <= 2)   s = "x is greater than 1" else   s = "x is less than or equal to 1" end Select 


In both cases, the value for s is "x is greater than 4". In this example, you are really testing to see if x is greater than 4 and less than or equal to 5, but by taking advantage of the way the Case statement works, you are able to write it in a much more streamlined way.




REALbasic Cross-Platform Application Development
REALbasic Cross-Platform Application Development
ISBN: 0672328135
EAN: 2147483647
Year: 2004
Pages: 149

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net