Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Control Flow: if, switch, f | Go Programming Bootcamp lesson | BraveProgrammer
  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 5 of 60

·Go

Control Flow: if, switch, for Loops

20 minutes

Course progress8%

Control Flow: if, switch, for Loops

Control flow lets your program make decisions and repeat actions. Go keeps these constructs readable and consistent.

if statement

Use if to run code conditionally.

if n%2 == 0 {
    fmt.Println("even")
} else {
    fmt.Println("odd")
}

switch

Use switch when you want to compare one value against several possibilities.

switch day {
case "Mon", "Tue", "Wed", "Thu", "Fri":
    fmt.Println("weekday")
case "Sat", "Sun":
    fmt.Println("weekend")
default:
    fmt.Println("unknown day")
}

switch does not fall through by default; this prevents bugs from missing break statements.

for loops

Go has one loop statement, for. It can act like a counter loop, a while loop, or an infinite loop.

for i := 0; i < 10; i++ {
    fmt.Println(i)
}

j := 0
for j < 5 {
    fmt.Println(j)
    j++
}

for {
    fmt.Println("running")
    break
}

Info

Use switch for clearer, more maintainable code when comparing one value against many cases.
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from Go Programming BootcampBrowse activities
Previous lessonPrimitive Types & ConstantsContinue to lesson 6Arrays, Slices, and MapsBack to Go Programming Bootcamp curriculum