No history yet

Program Flow Basic Decisions

The Flow of a Program

At its core, a computer program is just a list of instructions. By default, the computer reads and executes these instructions one after another, from top to bottom, without skipping a beat. This predictable, line-by-line execution is called sequential execution.

Think of it like a recipe. You perform step 1 (preheat the oven), then step 2 (mix the ingredients), and so on. You wouldn't jump from step 1 to step 5.

This orderly process is the foundation of all programming. However, a program that only runs in a straight line isn't very useful. Real-world applications need to react to different situations. A game needs to know if a player has run out of health. A banking app needs to check if an account has sufficient funds for a withdrawal. To handle these scenarios, we need to alter the default program flow. This is where decision-making comes in.

Making a Simple Choice

The most fundamental tool for decision-making in programming is the if statement. It allows us to execute a block of code only when a specific condition is met, or evaluates to true.

The structure is straightforward: you have the keyword if, followed by a condition to test, and then a block of code that runs only if that condition is true. If the condition is false, the program simply skips over that block of code and continues with the next instruction.

if (condition is true) {
  // Execute this block of code
}

Let's look at a practical example. Imagine we're writing a simple game where a player needs at least 100 points to unlock a special item. We can use an if statement to check their score.

int playerScore = 150;

if (playerScore >= 100) {
  // This code will run because the condition is true
  print("Congratulations! You've unlocked the special item.");
}

In this case, since playerScore is 150, the condition playerScore >= 100 is true. As a result, the message is printed. If playerScore were 50, the condition would be false, and the print statement would be completely ignored. The program would just continue on after the if block's closing brace }.

The piece of code inside the parentheses, playerScore >= 100, is called a . It's a statement that the computer can evaluate to be either true or false. These expressions are the heart of decision-making in code. They can be simple comparisons like the one above, or more complex logical tests you'll learn about later.

Control flow statements determine the order in which code is executed, allowing programs to make logical decisions, perform repetitive tasks, and handle errors efficiently.

This is the first step in building program logic. By checking conditions with if statements, you can create paths that your program will only follow under the right circumstances.

Quiz Questions 1/4

What is the default, line-by-line order in which a computer program executes its instructions?

Quiz Questions 2/4

What is the primary purpose of an if statement?

Now you have the fundamental building block for making your programs smarter and more responsive.