Lessons in this course (12)

3. Operators, conditions and loops

Basic★ Must knowLesson 3 of 12
PurposeVB.NETC#
Equality===
Not equal<>!=
Logical and/or (short-circuit)AndAlso, OrElse&&, ||
Logical and/or (always both)And, Or&, |
NotNot!
String concatenation& (preferred) or ++
Integer division / modulus\, Mod/ (on ints), %
Power^Math.Pow
Reference equalityIs, IsNot== on references / is null
TernaryIf(cond, a, b)cond ? a : b
Null-coalescingIf(x, fallback)x ?? fallback
vb
If marks >= 75 Then
    Console.WriteLine("Distinction")
ElseIf marks >= 35 Then
    Console.WriteLine("Pass")
Else
    Console.WriteLine("Fail")
End If

Select Case day                          ' = C# switch
    Case DayOfWeek.Saturday, DayOfWeek.Sunday
        Console.WriteLine("Weekend")
    Case Else
        Console.WriteLine("Weekday")
End Select

Select Case score
    Case Is >= 90 : grade = "A"
    Case 75 To 89 : grade = "B"
    Case Else : grade = "C"
End Select

For i As Integer = 1 To 10 Step 2       ' 1,3,5,7,9 (bounds inclusive!)
    If i = 7 Then Exit For               ' = break
    If i = 3 Then Continue For           ' = continue
    Console.Write(i)
Next

For Each name As String In names
    Console.WriteLine(name)
Next

Do While attempts < 3                    ' also: Do Until, Loop While, Loop Until, While...End While
    attempts += 1
Loop

Pitfall: If user IsNot Nothing And user.IsActive Then evaluates both sides and throws when user is Nothing — use AndAlso.