Control Flow Statements: Conditional Statements, Loop Statements, MsgBox and InputBox

If/ElseIf/End If and a Select Case that handles ranges, 4 loop shapes with Do...Loop While as the run-at-least-once one, and MsgBox/InputBox as the quick dialogue pair: the till's first working total.

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The till's first conversation

Before ShopKeeper has buttons, Mehta Uncle wants proof the logic works: a quick program that asks for prices one by one, stops on a blank entry, and shows the total with a comment: small bill, normal, or big.

That one errand needs everything in this lesson: a loop that must run at least once, a decision with ranges (0, up to 500, above 500), and 2 tiny dialog boxes to talk with. All of it in VB.NET's block-shaped grammar.

Formula

The decision syntaxes, exam form

If block (note Then, and the closing End If):

If total > 500 Then

...

ElseIf total > 0 Then

...

Else

...

End If

Select Case (no break anywhere, no fall-through):

Select Case total

Case 0 : ...

Case 1 To 500 : ...

Case Is > 500 : ...

Case Else : ...

End Select

Theory

Select Case outgrows Java's switch

Remember BCA403's rule: switch matched single constant values only; ranges forced you back to if-else. VB.NET's Select Case has no such fence:

  • Case 1 To 500: a whole range
  • Case Is > 500: a comparison
  • Case 1, 3, 5: a list of values

And 2 reliefs: each Case ends where the next begins (no fall-through, so no break to forget), and Case Else catches the rest. When one variable faces many shapes of test, Select Case reads like the price list it implements.

At a glance

The 4 loop shapes

LoopChecksRuns minimumShape
For ... To ... [Step]Before each pass0 timesFor i = 1 To 10 ... Next
While ... End WhileBefore0 timesWhile qty > 0 ... End While
Do While/Until ... LoopBefore0 timesDo While entry <> "" ... Loop
Do ... Loop While/UntilAFTER1 timeDo ... Loop While again = "y"

Practical

The quick total, start to finish

Module QuickTotal
    Sub Main()
        Dim total As Double = 0
        Dim entry As String

        Do                              ' post-test: ask at least once
            entry = InputBox("Price (leave blank to finish):")
            If entry <> "" Then
                total += CDbl(entry)    ' InputBox returns a STRING
            End If
        Loop While entry <> ""

        Select Case total
            Case 0
                MsgBox("Empty bill")
            Case 1 To 500
                MsgBox("Total: " & total)
            Case Is > 500
                MsgBox("Big bill today: " & total)
        End Select
    End Sub
End Module

Quiz

For i As Integer = 10 To 1 : MsgBox(i) : Next How many boxes appear?

  1. 10, counting down from 10 to 1
  2. None: with the default Step of +1, 10 is already past 1, so the body never runs
  3. 1: it shows 10 and stops
  4. Infinite: the loop never reaches 1
Show the answer

None: with the default Step of +1, 10 is already past 1, so the body never runs

A For loop steps by +1 unless told otherwise, and its pre-test asks: is the counter still on the way to the end value? Starting at 10 heading UP can never reach 1, so the test fails immediately and the body runs zero times: no error, no warning, just silence. Counting down needs the explicit Step -1: For i = 10 To 1 Step -1. Option A is what the code LOOKS like it does; options C and D invent behaviours the pre-test rules out. Silent empty loops like this are a favourite spot-the-bug snippet.

Think first

Why Do ... Loop While, specifically?

The quick total uses the post-test loop. Recall BCA403's chai-serving hosts, then justify: why must THIS loop check its condition at the bottom?

Show the answer

Because the first question must ALWAYS be asked: you cannot know whether entry is blank before showing the InputBox at least once. A pre-test loop (Do While entry <> "") would test entry before it exists. Post-test = the confident host from BCA403's do-while: act first, check afterwards. VB.NET's spelling is Do ... Loop While (or Loop Until, the same idea with the condition inverted: Loop Until entry = ""). Same concept, new grammar, third language in a row: menus and prompts always want the body-first loop.

Watch out

The dialog pair's fine print

InputBox always returns a String: "350" is text until CDbl/CInt converts it, and arithmetic on unconverted entries concatenates instead of adding when Option Strict is off: "1" & typing errors make "150" bills.

Cancel returns an empty string: your loop must treat "" as the exit signal (ours does), else Cancel crashes CDbl.

MsgBox is for messages, not layout: real screens come from Unit 3's controls; MsgBox is the developer's quick voice.

Theory

The logic is ready for buttons

Everything the till needs to THINK is now written: decide (If, Select Case with ranges Java never had), repeat (4 loop shapes; Exit For / Exit Do for early exits), and converse (MsgBox out, InputBox in). Unit 3 replaces the InputBox conversation with a real form: TextBox for the price, Button for add, DataGrid for the bill, and every Case in today's Select becomes a label on screen. The brain is done; next we build the face.

Summary

Key takeaways

  • If ... Then ... ElseIf ... Else ... End If: block decisions, closed explicitly.
  • Select Case beats Java's switch: ranges (1 To 500), comparisons (Is > 500), lists (1, 3, 5), Case Else, and NO fall-through or break.
  • Four loops: For/Next (default Step +1), While/End While, Do While/Until...Loop (pre-test), Do...Loop While/Until (post-test, runs once minimum).
  • For 10 To 1 without Step -1 runs zero times, silently.
  • MsgBox shows; InputBox asks and returns a STRING (empty on Cancel): convert with CDbl/CInt.
  • Exit For and Exit Do leave a loop early.
  • Memory hook: Select Case takes ranges, Do...Loop asks after, InputBox speaks only String.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Programming in Visual Basic .NET

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati