Control Structures
These are examples of control structures. You may also be interested in:

 Conditional structures
 Exceptions
 Flow-control structures
 Loops

Task

List the conditional structures offered by a programming language. See Wikipedia: conditionals for descriptions.

Common conditional structures include if-then-else and switch.

Less common are arithmetic if, ternary operator and Hash-based conditionals.

Arithmetic if allows tight control over computed gotos, which optimizers have a hard time to figure out.



 
 
Print "QB64/Qbasic conditional structures"
Dim k As String
Menu 1
View Print 13 To 23
Print "A menu example using the many options of IF statement"
k = " "
12: While k <> ""
    k = UCase$(Input$(1))
    If k = "O" GoTo O
    If k = "F" Then 22
    If k = "S" Then GoSub S: GoTo 12
    If k = "C" Then GoSub 4: GoTo 12
    If k = "E" Then GoSub 5: Exit While
Wend
Cls
Print "the same menu example with Select Case"
Sleep 2
While k <> ""
    k = UCase$(Input$(1))
 
    Select Case k
        Case "O"
            Print "You choose O"
        Case "F"
            Print "You choose F"
        Case "S"
            Print "You choose S"
        Case "C"
            Print "You choose C"
        Case "E"
            Print "You choose Exit"
            _Delay 1
            Exit While
        Case Else
            Print "Wrong choice"
    End Select
Wend
View Print
Cls
Menu 2
View Print 13 To 23
Print "menu demonstration using ON value GOTO"
k = " "
While k <> ""
    k = Input$(1)
    On Val(k) GOSUB 1, 2, 3, 4, 5
Wend
End
 
1:
Print "Chosen O"
Return
 
2:
Print "Chosen F"
Return
 
3:
Print "Chosen S"
Return
 
4:
Print "Chosen C"
Return
 
5:
Print "Chosen E"
If k = "5" Then End
Return
 
 
O:
Print "You choose O"
GoTo 12
 
22:
Print "You choose F"
GoTo 12
 
S:
Print "You choose S"
Return
 
 
 
Sub Menu (Kind As Integer)
    Locate 7, 33: Color 3, 4
    Print "Choose the item"
    Color 7, 0
    Locate , 33
    If Kind = 1 Then Print "Open a file"; Else Print "1) Open a file";
    Color 14, 1
    Locate , 33
    If Kind = 1 Then Print "O" Else Print "1"
    Color 7, 0
 
    Locate , 33
    If Kind = 1 Then Print "Find a file"; Else Print "2) Find a file";
    Color 14, 1
    Locate , 33
    If Kind = 1 Then Print "F" Else Print "2"
    Color 7, 0
 
    Locate , 33
    If Kind = 1 Then Print "Scan a file"; Else Print "3) Scan a file";
    Color 14, 1
    Locate , 33
    If Kind = 1 Then Print "S" Else Print "3"
    Color 7, 0
 
    Locate , 33
    If Kind = 1 Then Print "Copy a file"; Else Print "4) Copy a file";
    Color 14, 1
    Locate , 33
    If Kind = 1 Then Print "C" Else Print "4"
    Color 7, 0
 
    Locate , 33
    If Kind = 1 Then Print "Exit from Menu"; Else Print "5) Exit from Menu";
    Color 14, 1
    Locate , 33
    If Kind = 1 Then Print "E" Else Print "5"
    Color 7, 0
 
End Sub
 