Select Case Statements and Loops

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which data types can the expression in a Select Case statement evaluate to in VB.Net?

  • Only numeric types (Integer, Double)
  • Only Boolean and Date types
  • Only String and Char types
  • Any elementary data type including Boolean, Byte, Char, Date, and String (correct)

In a Select Case statement, the Case Else block is executed when none of the preceding Case conditions are met.

True (A)

What property of a ListBox control needs to be modified to prevent partial item display?

IntegralHeight

Multiple expression clauses in a Select Case statement are separated by __________.

<p>commas</p>
Signup and view all the answers

Match the loop types with their descriptions:

<p>For loops = Occur a certain number of times. Do loops = Keep running until a certain condition is reached.</p>
Signup and view all the answers

What is the primary purpose of a Select Case statement?

<p>To test a variable against a list of values for equality (C)</p>
Signup and view all the answers

Setting the Dock property of a ListBox to Fill makes it occupy the entire available space of its parent container.

<p>True (A)</p>
Signup and view all the answers

What event is typically used to handle user selection from a ListBox?

<p>SelectedIndexChanged</p>
Signup and view all the answers

In VB.NET, the keyword used to specify a decrement in a For loop is _______.

<p>Step</p>
Signup and view all the answers

In the context of loops, what distinguishes a For loop from a Do loop?

<p><code>For</code> loops execute a fixed number of times, while <code>Do</code> loops continue until a condition is met. (D)</p>
Signup and view all the answers

Which property is used to set the text displayed on a Windows Forms button?

<p>Text (C)</p>
Signup and view all the answers

In a For Each loop, the loop variable must be explicitly incremented.

<p>False (B)</p>
Signup and view all the answers

What method is used to add items to a ListBox control?

<p>Items.Add</p>
Signup and view all the answers

When adding items into a ListBox from your C drive, My.Computer.FileSystem.GetDirectories requires the ________ path as parameter.

<p>root</p>
Signup and view all the answers

What is the purpose of the Clear() method when used with the Items property of a ListBox?

<p>To remove all items from the ListBox (D)</p>
Signup and view all the answers

A Do Until loop executes its block of code as long as the condition is true.

<p>False (B)</p>
Signup and view all the answers

In VB.NET, what function is used to generate a random number?

<p>Random.Next</p>
Signup and view all the answers

In a Do While loop, the code block is executed as long as the specified condition remains __________.

<p>true</p>
Signup and view all the answers

After the following code is executed, what will be the final displayed message?

<p>Zainab IsFemale (B)</p>
Signup and view all the answers

Match example list box entries, to their associated gender:

<p>Ahmed = Male Zainab = Female</p>
Signup and view all the answers

Flashcards

Select Case Statement

A statement that allows a variable to be tested for equality against a list of values. Each value is called a case.

Expression (Select Case)

An expression that must evaluate to any of the elementary data types in VB.Net.

Expression List

A list of expression clauses representing match values for the main expression, separated by commas.

Statements (Select Case)

Statements that run if the select expression matches any clause in the expression list.

Signup and view all the flashcards

Else Statements

Statements that run if the select expression does not match any clause in the expression list.

Signup and view all the flashcards

For Loops

Loops occur a certain number of times.

Signup and view all the flashcards

Do Loops

Loops keep running until a certain condition is reached.

Signup and view all the flashcards

For...Next Loop with Step

A loop that increments the counter by a specified value on each iteration.

Signup and view all the flashcards

Looping Backward

A loop that iterates backwards from an initial value to a final value.

Signup and view all the flashcards

For Each Loop

A loop iterates over each element in a collection.

Signup and view all the flashcards

Do Until Loop

A loop that repeats until a specified condition becomes true.

Signup and view all the flashcards

Do While Loop

Loops that repeat as long as a specified condition is true.

Signup and view all the flashcards

Study Notes

  • Lecture three covers Select Case statements and Loops.

Select Case

  • Enables a variable to be tested for equality against a list of values.
  • Each value constitutes a case.
  • The variable being switched on is checked for each select case.
  • Syntax:
    • Select Case expressionCase value1
    • Case value1
    • Statements
    • Case value2
    • Statements
    • Case Else
    • Statements
    • End Select
  • Expression must evaluate to any elementary data type in VB.Net like Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, SByte, Short, Single, String, UInteger, ULong, and UShort.
  • Expressionlist is a list of expression clauses, which represent matching values for the expression.
    • Multiple expression clauses are separated by commas.
  • Statements are the code that runs following the case that runs if the select expression matches any clause in the expression list.
  • Else statements are the code that runs following the default Case, if the select expression does not match any clause in the expressionlist of any of the Case statements.
  • Example program creates a Windows Forms Application project called Select Demo.
    • Sets the Text property of the form to Select Case.
    • Adds a ListBox control named 1stData with IntegralHeight set to False and Dock to Fill.
    • Fills the 1stData listbox with the names: Omer, Ahmed, Ali, and Noor.
    • The program displays a message box indicating the person's favorite color depending on the name selected in the listbox.
  • VB.NET code example:
    • Private Sub lstData_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstData.SelectedIndexChanged
    • Dim strName, strFavoriteColor As String
    • strName = lstData.Items(lstData.SelectedIndex).ToString
    • Select Case strName
    • Case "Omer": strFavoriteColor = "Yellow"
    • Case "Ahmed": strFavoriteColor = "Blue"
    • Case "Ali": strFavoriteColor = "Green"
    • Case "Noor": strFavoriteColor = "white"
    • End Select
    • MessageBox.Show(strName & "'s favorite color is " & strFavoriteColor, "Select Demo")
    • End Sub
  • The select case statement can also be used to display a person's gender depending on the name selected.
    • Example VB.NET code:
      • Select Case strName.ToLower
      • Case "Omer", “Ahmed", “Ali": MessageBox.Show("This person's gender is male.", "Select Demo")
      • Case “Noor", “Rana": MessageBox.Show("This person's gender is female.", "Select Demo")
      • End Select
  • The Case Else statement can be used to catch all remaining unmatched cases.
    • Example VB.NET code:
      • Select Case strName
      • Case “Ahmed", “Omer": MessageBox.Show("This person's gender is male.", "Select Demo")
      • Case “Noor", “Teba", “Zainab": MessageBox.Show("This person's gender is female.", "Select Demo")
      • Case Else: MessageBox.Show("I don't know this person's gender.", "Select Demo")
      • End Select

Loops

  • When writing computer software, you often need to perform the same task several times.
  • For example: creating a telephone bill for all customers or reading in ten files from your computer's disk.
  • For Loops: These loops occur a certain number of times, like ten times.
  • Do Loops: These loops keep running until a certain condition is reached, like until all the data is processed.
  • For...Next Loop: loops a specific number of iterations
    • Create a new Windows Forms Application project called Loops.
    • Add a ListBox and a Button control to the form.
    • Change the Name property of the ListBox to 1stData and its IntegralHeight property to False.
    • Change the Name property of the button to btnForNextLoop.
    • Set its Text property to For NextLoop.
    • Double-click the button to create its Click event handler and add the following code:
      • Private Sub btnForNextLoop_Click(sender As Object, e As EventArgs) Handles btnForNextLoop.Click
      • Dim intCount As Integer
      • lstData.Items.Clear()
      • For intCount = 1 Το 5
      • lstData.Items.Add("I'm item " & intCount.ToString & " in the list!")
      • Next
      • End Sub
  • For...Next Loop with Step: loops a specific number of iterations with a specified increment.
    • Return to the Forms Designer for the Loops project.
    • Add a Button control to your form.
    • Set its Name property to btnForNextLoopWithStep.
    • Set its Text property to For Next Loop w/ Step.
    • Double-click the button and add the following code in the Click event handler:
      • Private Sub btnForNextLoopWithStep_Click(sender As Object, e As EventArgs) Handles btnForNextLoopWithStep.Click
      • lstData.Items.Clear()
      • For intCount As Integer = 4 Το 62 Step 7
      • lstData.Items.Add(intCount.ToString)
      • Next
      • End Sub
  • Looping Backward: loops in reverse order
    • Return to the Forms Designer and add another Button control to your form.
    • Set its Name property to btnBackwardsForNextLoop.
    • Set its Text property to Backwards For Next Loop.
    • Double-click the button and add the following code in the Click event handler:
      • Private Sub btnBackwardsForNextLoop_Click(sender As Object, e As EventArgs) Handles btnBackwardsForNextLoop.Click
      • lstData.Items.Clear()
      • For intCount As Integer = 10 Το 1 Step -1
      • lstData.Items.Add(intCount.ToString)
      • Next
      • End Sub
  • For Each Loop: loops through each item in a collection.
    • Return to the Forms Designer, add another Button control to your form.
    • Set its Name property to btnForEachLoop and its Text property to For Each Loop.
    • Double-click the button and add the following code in the Click event handler:
      • Private Sub btnForEachLoop_Click(sender As Object, e As EventArgs) Handles btnForEachLoop.Click
      • lstData.Items.Clear()
      • For Each strFolder As String In My.Computer.FileSystem.GetDirectories("C:")
      • 1stData.Items.Add(strFolder)
      • Next
      • End Sub
  • Do Until...Loop: executes a block of code until a condition becomes true.
    • Return to the Forms Designer in the Loops project, add another Button control to your form.
    • set its Name property to btnDoUntilLoop.
    • set its Text property to Do Until Loop.
    • Double-click the button and add the following code to its Click event handler:
      • Private Sub btnDoUntilLoop_Click(sender As Object, e As EventArgs) Handles btnDoUntilLoop.Click
      • Dim objRandom As New Random
      • Dim intRandomNumber As Integer = 0
      • 1stData.Items.Clear()
      • Do Until intRandomNumber = 10
      • intRandomNumber = objRandom.Next(25)
      • 1stData.Items.Add(intRandomNumber.ToString)
      • Loop
      • End Sub
  • Do While...Loop: executes a block of code as long as a condition is true.
    • Return to the Forms Designer and add another Button control to your form.
    • Set its Name property to btnDoWhileLoop.
    • Set Text property to Do While Loop.
    • Double-click the button and add the following code to the Click event handler:
      • Private Sub btnDoWhileLoop_Click(sender As Object, e As EventArgs) Handles btnDoWhileLoop.Click
      • Dim objRandom As New Random
      • Dim intRandomNumber As Integer = 0
      • ClearList()
      • Do While intRandomNumber < 15
      • intRandomNumber = objRandom.Next(25)
      • lstData.Items.Add(intRandomNumber.ToString)
      • Loop
      • End Sub

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Marriott Select Brands Flashcards
7 questions

Marriott Select Brands Flashcards

WellRegardedObsidian1129 avatar
WellRegardedObsidian1129
SELECT tables continued....
31 questions

SELECT tables continued....

YouthfulHarmonica5852 avatar
YouthfulHarmonica5852
Government Select Committees
1 questions

Government Select Committees

TopnotchLeibniz5787 avatar
TopnotchLeibniz5787
Use Quizgecko on...
Browser
Browser