Chapter 5 of Visual BASIC
If…Then…Else
There are times when you want the program to respond differently to different events. Say you wanted to write some code that checks to see that the user entered a number. To do this we would use an if statement. The format for an if statement in Visual BASIC is if condition is true then…do something. The condition would use one of these operators:
| < | less than |
| > | greater than |
| == | equal to |
| <= | less than or equal to |
| >= | greater than or equal to |
| <> | not equal to |
| < | less than |
To check to see if what the user entered is numeric, we use the builtin function IsNumeric. To do this we put IsNumeric then parenthases and then what we want to check. IsNumeric returns a Boolean variable, which means that the answer will either be True or False, which we need to know for the if statement. Let’s use the program from the last section and check and see if the numbers entered is numeric.
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim decNum1 As Decimal Dim decNum2 As Decimal If IsNumeric(txtNum1.Text) = True Then decNum1 = txtNum1.text If IsNumeric(txtNum2.Text) = True Then decNum1 = txtNum2.text lblAnswer.Text &= txtNum1.Text + txtNum2.Text End Sub |
If you want to do more than one line of code then you will need to come to the next line after the Then (hitting enter) and type all the code there. When you are done, you’d put an End If.
Else Statements
Else statements are used when you want want something to happen if the comparison is false. The else statement comes in handy when say you want your program to do something if your name is Jimmy but do something completely different if your name was Sandra. You can never have an else statement by itself, it always needs to accompany an if statement. Else would come after the last line of code to execute if the condition is true, and before the End If. Let’s check and see if a number is odd or even. To check if the number entered is an even number, simply mod it by 2. If the result is 0 then the number is even, otherwise it is odd. This code would go inside of a Button, and will work assuming you have the other components named the same thing:
Dim intNum as Integer intNum = txtNum.Text If (intNum mod 2) = 0 Then lblMessage.Text = “The number is even” Else lblMessage.Text = “The number is odd” End If |
The reason we mod it by 2 is because only even numbers can be divided evenly by 2, so if a number divided by 2 has no remainder, we can conclude that it is even.
Remember the first program in this section, where we had two if statements one for each TextBox, well we can combine them. to do this, we would use one If statement, a condition, a conjunction, and another condition. The conjuctions are:
| And | Both conditions must be true, both are tested, even if condition 1 is false |
| Or | One conditions must be true, both are tested even if condition 1 is true |
| AndAlso | Both conditions must be true, if condition 1 is false, condition 2 won’t be tested |
| OrElse | One condition must be true, if condition 1 is true, condition 2 won’t be tested |
| Xor | Only one condition can be true, if both are true it will evaluate to false |
So to rewrite the code, we would do:
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim decNum1 As Decimal Dim decNum2 As Decimal If IsNumeric(txtNum1.Text) = True AndAlso IsNumeric(txtnum2.Text) = True Then lblAnswer.Text = txtNum1.Text + txtnum2.Text Else lblAnswer.Text = “Please enter number” End If End Sub |
Now an else statement can have as many lines of code as you want, you can even put more if statements in there. Say for example you wanted to turn a number grade into a letter grade, to do this we would need a bunch of ElseIf statements.
Dim intGPA As Integer intGPA = 40 If intGPA >= 90 AndAlso intGPA <= 100 ThenlblMessage.Text = “You’are on the high honor roll” ElseIf intGPA <= 89 AndAlso intGPA >=80 Then lblMessage.Text =”You are on the honor roll this semester” ElseIf intGPA<=79 AndAlso intGPA >=70 Then lblMessage.Text =”You passed the course” ElseIf intGPA <= 69 AndAlso intGPA >=0 Then lblMessage.Text = “You failed the course ” Else lblMessage.Text = “There’s an error with your grade” End If |
Case Select
ElseIf statements can get pretty messy. Just look at the previous code, for some it might be diffictule to decifer. If you have too many choices then you may want to use Select Case. Personally, I use Select Case if an If statement has three or more possible outcomes. To use Select Case, you would have an object’s property or variable as the focus and then each case is a condition.
Dim charGPA As Char charGPA = “A” Select Case charGrade Case “A” lblMessage.Text=”You got an A”; Case “B” lblMessage.Text=”You got a B”; Case “C” lblMessage.Text=”You got a C”; Case “D” lblMessage.Text=”You got a D”; Case “F” lblMessage.Text=”You got an F”; Case Else lblMessage.Text=”There’s an error with your grade.”; End Select |
The Select Case can be used with any variable type. Unlike other languages, the code ends when it hits the next Case statement. After it executes the code, it will go to the End Select and continue with the rest of the program. Using conditions, whether it be an If statement or Select Case is essential to everyday programs.
Next Section: Conditionals
Chapter 5 of C++
If statement
There are times when you want the program to respond differently to different events. Say for example you wanted to display a certain message if the name you entered in the last tutorial was Ernie. You would use an if statement when you want to compare two or more things and have the code coninue based on the result will continue accordingly. Some of the comparison operators that the if statement uses should be familiar from math class.
| < | less than |
| > | greater than |
| == | equal to |
| <= | less than or equal to |
| >= | greater than or equal to |
| != | not equal to |
The comparison needs to go in side Parentheses (). If there’s only one line of code then it can go right on the next line, if the code spans more than one line then it goes inside curly braces {}. Personally I always use curly braces because it makes it easier to read. You can also nest if statements inside of if statements. One thing I must point out is the if statement is a line of code that can’t have a semicolon.
if (1 < 2) { //Code goes here }if (1 <= 2) { //Code goes here }if (name == “Bob”) { //Code goes here } if (trueorfale == true) { //Code goes here } |
Else Statements
Else statements are used when you want want something to happen if the comparison is false. The else statement comes in handy when say you want your program to do something if your name is Jimmy but do something completely different if your name was Sandra. You can never have an else statement by itself, it always needs to accompany an if statement, and like the if statement, all code must go inside curly braces {}.
int GradePointAverage = 83; if (GradePointAverage > 90) { cout<<“You’are on the high honor roll \n”; } else if (GradePointAverage >= 80) { cout<<“You’are on honor roll \n”; } else { cout<<“You aren’t on the honor roll this semester \n”; } |
As you can see if the GPA was an 83 then it would skip the first if statement but go to the second one and execute
that. Say you wanted to make it check if it was between 90 and 100 and if it was between that then its on high honor roll, between 80 and 90 its just honor roll, 70 to 79 is passing, 0 to 69 is failing and anything else is an error. To do this you would need to combine if statements using ands and ors. In C++ the and statement consists of two ampersands (&&) while or is two pipes(||)
int GradePointAverage = 40; if (GradePointAverage >= 90 && GradePointAverage <= 100) { cout<<“You’are on the highhonor roll”; } else if (GradePointAverage <= 89 && GradePointAverage >=80) { cout<<“You are on the honor roll this semester”; } else if (GradePointAverage <= 79 && GradePointAverage >=70) { cout<<“You passed the course “; } } else if (GradePointAverage <= 69 && GradePointAverage >=0) { cout<<“You failed the course “; } else { cout<<“There’s an error with your grade”; } |
Switch
Else ifs can get messy if you aren’t careful. If you have too many choices then you may want to use a switch statement. Switch statements are like using if statements, but really clean up the code a lot. With a switch you need an int or char variable that will be the focus of the statement. Every different outcome of that variable is called a case. The cases will go inside of curly braces {}, with every case you need a colon (:) to tell where the case begins, and most of the time you will want to have a break statement with your case. Without break statements, the case won’t end and it will continue into the next case. Sometimes you will want the case to fall through to the next one. At the end of the switch you should have a default case. The default case is if it doesn’t fit in any of the other cases. In this example we’ll see why you’d want to leave out some of the break statements.
char grade=’C’; switch(grade) { case ‘a’: case ‘A’: cout<<“You got an A”; break; case ‘b’: case ‘B’: cout<<“You got a B”; break; case ‘c’: case ‘C’: cout<<“You got an C”; break; case ‘d’: case ‘D’: cout<<“You got an D”; break; case ‘f’: case ‘F’: cout<<“You got an F”; break; default: cout<<“There’s an error with your grade”; } |
Whether you use ifs and else ifs, or switch statements is a matter of circumstance and preference. Personally if its more then three else ifs then I use the switch statement, otherwise else ifs. Get used to using the conditionals, as they are common in everyday programs.
Next Section: Loops
Chapter 5 of Fundamentals of Programming
Modules
Say we had a program that gave directions to 3 places, a video store, a bank, and the post office. Now say that the video store was the same directions as the bank, just down one more block. In theory we would need to write out the directions to the bank twice. Not anymore. Now we can put things we use often in their own subsection of our program called modules or functions. Say we wanted to get the 11 and 12 times table:
| MAIN PROGRAM CALL ELEVEN PRINT ELEVEN CALL TWELVE PRINT TWELVE END MAINFUNCTION ELEVEN FOR X = 0 TO 12 ELEVEN_TIMES_TABLE[x] = 0 TIMES X NEXT X RETURN ELEVEN_TIMES_TABLE END FUNCTION FUNCTION TWELVE FOR X = 0 TO 12 TWELVE_TIMES_TABLE[x] = 0 TIMES X NEXT X RETURN TWELVE_TIMES_TABLE END FUNCTION |
All functions need to return something, otherwise the data in there will disappear. Take note that the CALL command is usually used to call functions. Although it may be more uncommon, you could be calling them using INVOKE, or anything clear like that. Also notice when we return the values we don’t put an index, this is telling those who read your program, and soon the computer that you are returning all the elements in the array, not just a specific one.
Recursive functions are when a function calls itself. Normally it will only allow you to do this so many times before the program crashes due to stack overflow. What you won’t get an error with most of the time, is functions calling other functions. It is a common thing in programs. Take that directions program algorithm:
| MAIN PROGRAM CALL VIDEO_STOREEND MAIN FUNCTION VIDEO_STORE CALL BANK PRINT “Go down another block and your there. RETURN END FUNCTION FUNCTION TWELVE PRINT “Make a right on Maple go down to Cedar and make a right.” RETURN END FUNCTION |
In the last example we have functions that print in the function themselves and do not return anything. We still use the RETURN algorithm to let the reader know that you are going back to where the function was called from.
Next Section: Objects