140 likes | 154 Views
Learn about control structures and logical operators in Visual Basic through exercises. Topics include checkbox properties, message dialog display, unary operators, and looping statements.
E N D
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011
Objectives • In this chapter, you will do exercises related to: • Usage of control structures in Visual Basic • Logical operators
Exercises • Which property specifies whether a CheckBox is selected? • 1. Selected 2. Checked 3. Clicked 4. Check • Call the _____method of class MessageBox to display a message dialog. • 1. Display 2. Message 3. Open 4. Show • A unary operator ______. • 1. requires exactly one operand 2. requires two operands 3. must use the AndAlso keyword 4. can have no operands • The ____ operator is used to ensure that two conditions are both true. • 1. Xor 2. AndAlso 3. Also 4. OrElse • The Do…Loop While statement body repeats when the loop-continuation condition _____. • 1. is False after the body executes 2. is False before the body executes 3. is True after the body executes 4. is True before the body executes • An infinite loop occurs when the loop-continuation condition in a Do While…Loop or Do…Loop While statement______. • 1. never becomes True 2. never becomes False 3. is False 4. is tested repeatedly
Exercises (cont'd) • The Do…Loop Until statement checks the loop-termination condition _______. • 1. for False after the body executes 2. for False before the body executes 3. for True after the body executes 4. for True before the body executes • Counter-controlled repetition ____ the control variable after each iteration. • 1. increments 2. initialize 3. decrements 4. either answer 1 or 3 • What aspect of the control variable determines whether looping should continue? • 1. name 2. initial value 3. type 4. final value • If the step is omitted, the increment of a For…Next statement defaults to ______. • 1. 1 2. -1 3. 0 4. Either answer 1 or 2 • Which of the following is the appropriate For…Next header for varying the control variable over the following sequence of values: 25, 20, 15, 10, 5? • 1. For i As Integer = 5 To 25 Step 5 2. For i As Integer = 25 To 5 Step -5 • 3. For i As Integer = 5 To 25 Step -5 4. For i As Integer = 25 To 5 Step 5
Exercises (cont'd) • A Case that handles all values larger than a specified value must precede the > operator with keyword ______. • 1. Select 2. Is 3. Case 4. All • Use a(n) _____ to separate multiple conditions in a Case statement. • 1. period 2. asterisk 3. comma 4. colon • The ______property determines by how much the current number in a numericUpDown control changes when the user clicks the up arrow or down arrow. • 1. Amount 2. Step 3. Increment 4. Next • Which For…Next header alters the control variable from 0 to 50 in increment of 5? • 1. For i = 0 To 50 Step 50 2. For 0 To 50 Step 5 3. For i = 0 To 50 Step = 5 4. For i = 0 To 50 Step 5
Select…Case Statement • Relational operator • Case Is < 0 • Case Is >100 • Multiple values • Case 0, 5 To 9 ' 0 and range 5 ~ 9
True/False Statements • The Case Else is required in the Select…Case selection statement. • The expression x>y And a<b is true if either x>y is true or a<b is true. • An expression containing the Or operator is true if either or both of its operands are true • The expression x<=y AndAlso y>4 is true if x is less than or equal to y and y is greater than 4 • Logical operator Or performs short-circuit evaluation.
True/False Statements (cont'd) • The Exit Do, Exit For and Exit While statements, when executed in a repetition statement, cause immediate exit from only the current iteration of the repetition statement. • The Do…Loop While statement tests the loop-continuation condition before the loop body is performed. • The Or operator has a higher precedence than the And operator.
What Does the Code Do? • Dim y As Integer • Dim x As Integer • Dim mysteryValue As Integer • x=1 • mysteryValue=0 • Do • y = x^2 'What is the value of y in the first iteration? • displayListBox.Items.Add(y) • mysteryValue += 1 • x += 1 • Loop While x<=10 • resultLabel.Text = mysteryValue 'What is the value of resultLabel.Text? • 'What are values of the displayListBox? • 'What is the value of x?
What is Wrong in the Code? • Dim y As Integer = 5 • Dim z As Integer = 1 • Do • z*=y • Loop Until y < 1 • y -=1 • resultLabel.Text = z
What is Wrong in the Code? • Determine whether an Integer is even or odd • Select Case value Mod 2 • Case 0 • outputLabel.Text = "Odd Integer" • Case 1 • outputLabel.Text = “Even Integer"
Write a Program • Use For … Next to write a program to display the triangle of asterisks in outputTextBox. Use the following statements: • outputTextBox.AppendText("*"):displays asterisks one at a time • outputTextBox.AppendText(vbCrLf) • outputTextBox.AppendText(" "): inserts a space * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Write a Program (cont'd) • Use For … Next to write a program to display a rectangle in outputTextBox. Use the following statements: • outputTextBox.AppendText("+") • outputTextBox.AppendText(vbCrLf) • outputTextBox.AppendText(" "): inserts a space + - - - - - - - - - - + | | | | | | + - - - - - - - - - - +
Exercises After the Class • Chapter 5 in your textbook • Self-Review Exercises • Quick Quiz • Exercises