Presentation is loading. Please wait.

Presentation is loading. Please wait.

Controlling Program Flow with Decision Structures.

Similar presentations


Presentation on theme: "Controlling Program Flow with Decision Structures."— Presentation transcript:

1 Controlling Program Flow with Decision Structures

2 If…Then Statement Is a Decision Structure that executes a set of statements when a condition is true. Takes the form: If condition then statements End if Ex: in the following If..Then statement intguess = 7 is the condition If intguess = 7 then lblmessage.text = “You guessed it” End If

3 The condition of an If…Then statement is a Boolean expression, which evaluates true/false or yes/no Relational Operators: Operator Meaning = Equal to < Less than < = Less than or equal to > Greater than >= Greater than or equal to <> Not equal to

4 If…Then… Else Statement This includes and optional Else clause that is executed when the If condition evaluates to False. This is a two way decision statement Takes the form: If condition then statements Else statements End If The End if must be at the end of every if…then statement regardless of how many decisions

5 Example: If..Then..Else If inttemp <= 32 then lblcomment.text = “Freezing” Else inttemp > 80 then lblcomment.text = “Hot” End If

6 If…Then…ElseIf This statement is used to decide between three or more actions Takes the form: If inttemp <= 32 then lblcomment.text = “Freezing” Elseif inttemp > 80 then lblcomment.text = “hot” Else lblcomment.text = “Moderate” End If

7 If…Then Decision Statements One Way Decision- If…Then Two Way Decision- If…Then…Else Multi-Way Decision- If…Then…ElseIf

8 Message Box A message box is a PREDEFINED DIALOG box. PREDEFINED means is the form is already created, you just add the text A message box is used to DISPLAY information to the user

9 Message Box cont. The message box includes a Show( ) method for displaying a Message Box –MessageBox.Show(message) Example: If intguess 50 then MessageBox.Show(“Guess out of Range”) Elseif intguess = insecretnumber then MessageBox.Show(“You guessed it”) End if

10 Nested If…Then…Else Statements An If..then…else statement can hold another If…then..else statement That is called a nested If statement If intguess = intsecretnumer then Lblmessage.text = “you guessed it” Else If inguess < intsecretnumber then Lblmessage.text = “too low” End If

11 Select…Case Statement Is a decision statement structure that uses the result of an expression to determine which block of code to execute Takes the Form: Select expression Case value End Select

12 Select… Case Is Compares the result of an expression to a range of values when a relational operator is part of the value Example: Select Case intscore Case Is <10 lblmessage.text = “nice Try” Case Is <25 lblmessage.text = “good” End Select

13 Random Numbers RND( )- generates random numbers greater than or equal to 0 and less than 1 To generate numbers in a greater range, Rnd( ) is multiplied by the upper limit of the range

14 Random Numbers This creates numbers from 0 to the upper limit. For example to generate numbers that are greater than or equal to 0 and less than 10 a statement is as such: lblrandom1.text = Rnd( ) * 10

15 To find random numbers in a lower range: (High number – Low number + 1) * Rnd( ) + Low Number High Number is the maximum value desired Low Number is the minimum value Looking for numbers greater than or equal to 10 and less than 30 lblrandom1.text = (30 – 10 + 1) * Rnd( ) + 10 = 21 * Rnd( ) + 10

16 Random Numbers cont. Numbers generated by Rnd( ) are real numbers with decimal portions. To produce random integers (whole numbers) the Int( ) function can be used to return integer portion without rounding lblrandnum1.text = Int(21 * Rnd( ) ) + 10

17 Algorithms A set of steps that tell how to solve a problem An algorithm can be further refined by writing it in pseudocode, which is both English and program code Creating an algorithm and then writing pseudocode allows a programmer to think through the program before actually typing code.

18 Static Variables Along with variables having scope(the set of statements that can use the variable) they have a lifetime Lifetime: lifetime in which memory exists Lifetime of local variable: duration of the procedure in which it was declared Lifetime of global variable: duration of the program

19 Static Variables cont. The lifetime of a local variable can be extended by using a Static variable, which takes the form: Static variable_name As type A static variable is declared using the keyword Static instead of Dim A static variable’s scope is local to the procedure in which it was declared, but its lifetime is the duration of the program

20 Static Variables cont. Static variables are necessary in event procedures with variables that should be retained in memory throughout the program execution. A variable declared in a click event is redeclared and reinitialized each time the click event occurs unless the variable is declared as static

21 Logical Operators Boolean expressions can also be formed using logical operators. A logical operator joins two expressions to create an expression that evaluates True/False Example: If intguess 50 Then lblmessage.text = “Invalid Guess” Elseif intguess = intsecretnumber then lblmessage.text = “You Guessed It” End If

22 Counter Variables A counter is a variable storing a number that is incremented by a constant value. Counters are useful in keeping track of how many a user guesses, types a password, uses a program. The statement looks like: counter = counter + 1


Download ppt "Controlling Program Flow with Decision Structures."

Similar presentations


Ads by Google