Lesson 5Controlling Program Flow with Conditional Statements

Without specific instructions, a program executes statements linearly, one after the other, until the program ends or until an error causes the program to fail. In most cases a solution will need to include places where the program can change direction, either to react to a defined condition at a specific place in the program or to repeat a set of instructions until a desired goal is met.

In this lesson, we will focus on the use of conditional statements to control the flow of a program based on specific conditions during runtime. In the next lesson, the focus will be on how to use loops to repeat a set of instructions.

CONDITIONAL STATEMENTS

In every programming language, it is important to be able to execute an appropriate block of code depending on some condition. That is the purpose of conditional statements. Go supports a variety of conditional statements, including the following:

  • if statements
  • if-else statements
  • Nested if statements
  • switch statements
  • select statements

In this lesson, we will cover the first four of these. You will see how each can be used to change the flow of your Go programs. The select statement works similarly to the switch statement, but is ...

Get Job Ready Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.