On October 23rd, 2014, we updated our
By continuing to use LinkedIn’s SlideShare service, you agree to the revised terms, so please take a few minutes to review them.
Advanced VB: Review of the basicsPresentation Transcript
Review of the Basics Advanced Visual Basic
Overview You are now in at least your third semester of programming. In this course, we will be exploring more of Visual Basic. Before doing so, we should re-familiarize ourselves with the development environment and some of the basic programming topics.
Variables You often have to store values when you perform calculations with Visual Basic. For example, you might want to calculate several values, compare them, and perform different operations on them, depending on the result of the comparison. You have to retain the values if you want to compare them.Variables store values in RAM. A variable has a name (the word that you use to refer to the value that the variable contains). A variable also has a data type (which determines the kind of data that the variable can store). Example: Dim strFirstName As String
Variables Research Question: What is the difference between a variable and a constant?
Arrays An array is a set of values that are logically related to each other, such as the number of students in each grade in a grammar school. An array allows you to refer to these related values by the same name and to use a number, called an index or subscript, to tell them apart. The individual values are called the elements of the array. They are contiguous from index 0 through the highest index value. Example: Dim strStudentNames(10) As String
Arrays Research Question: How many elements are in this array? Dim strStudentNames(10) As String
Loops Visual Basic loop structures allow you to run one or more lines of code repetitively. You can repeat the statements in a loop structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection.
Loops (while) Use a While...End While structure when you want to repeat a set of statements an indefinite number of times, as long as a condition remains True. Example: Dim counter As Integer = 0 While counter < 3 counter += 1 ‘Insert code to use current value of counter. End While MsgBox("While loop ran " & CStr(counter) & " times")
Loops (do) Use a Do...Loop structure when you want to repeat a set of statements an indefinite number of times, until a condition is satisfied. Example: Dim counter As Integer = 0 Dim number As Integer = 10 Do Until number = 100 number = number * 10 counter += 1 Loop MsgBox("The loop ran " & counter & " times.")
Loops (for…next) Use a For...Next structure when you want to repeat a set of statements a set number of times. Example: For index As Integer = 1 To 5 Debug.Write(index.ToString & " ") Next Debug.WriteLine("") ' Output: 1 2 3 4 5
Loops (for each) Use a For Each...Next loop when you want to repeat a set of statements for each element of a collection or array. Example: Sub BlueBackground(ByValthisFormAs System.Windows.Forms.Form) For Each thisControl As System.Windows.Forms.ControlIn thisForm.Controls thisControl.BackColor= System.Drawing.Color.LightBlue Next thisControl End Sub
Loops Research Question: What happens when a loop doesn’t end? Dim intValue as integer = 1 Do Until intValue = 2 MessageBox.Show(“you have a problem!”) Loop
Decisions Visual Basic lets you test conditions and perform different operations depending on the results of that test. You can test for a condition being true or false, for various values of an expression, or for various exceptions generated when you execute a series of statements.
Decisions (if…then…else) When an If...Then...Else statement is encountered, condition is tested. If condition is True, the statements following Then are executed. If condition is False, each ElseIf statement (if there are any) is evaluated in order. When a True elseifcondition is found, the statements immediately following the associated ElseIf are executed. If no elseifcondition evaluates to True, or if there are no ElseIf statements, the statements following Else are executed. After executing the statements following Then, ElseIf, or Else, execution continues with the statement following End If. Example: Dim count As Integer = 0 Dim message As String If count = 0 Then message = "There are no items." ElseIfcount = 1 Then message = "There is 1 item." Else message = "There are " & count & " items." End If
Decisions (if…then…else) The Case Else statement is used to introduce the elsestatements to run if no match is found between the testexpression and an expressionlist clause in any of the other Case statements. Although not required, it is a good idea to have a Case Else statement in your Select Case construction to handle unforeseen testexpression values Example: Dim number As Integer = 8 Select Case number Case 1 To 5 Debug.WriteLine("Between 1 and 5, inclusive") Case 6, 7, 8 Debug.WriteLine("Between 6 and 8, inclusive") Case 9 To 10 Debug.WriteLine("Equal to 9 or 10") Case Else Debug.WriteLine("Not between 1 and 10, inclusive") End Select
Decisions Research Question: What is the most efficient way to determine if someone can vote? Consider: Age = 17 Age = 18 Age = 19
Additional Information For additional information about these topics, please review your text and the links provided in Blackboard.