Programming Basics: Statements & Functions: Crash Course Computer Science #12

3 min read 3 hours ago
Published on Feb 23, 2026 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial provides a comprehensive overview of programming basics, specifically focusing on statements and functions. By the end, you will understand how to use conditional statements, loops, and functions to control program flow, using examples from video game programming. This knowledge is essential for anyone starting in computer science and programming.

Step 1: Understanding Statements

Statements are the building blocks of programming. They instruct the computer to perform actions. Here are the core types of statements you will encounter:

  • Conditional Statements: These allow your program to make decisions based on conditions.
    • IF Statement: Executes a block of code if a specified condition is true.
    • ELSE Statement: Executes a block of code if the condition in the IF statement is false.

Practical Example:

IF playerHealth < 50 THEN
    display "Health is low!"
ELSE
    display "Health is sufficient."

Step 2: Utilizing Loops

Loops are used to execute a block of code multiple times. There are two common types of loops:

  • WHILE Loop: Continues executing as long as a specified condition is true.
  • FOR Loop: Executes a set number of times, usually iterating over a sequence like a list.

Practical Example of a WHILE Loop:

WHILE playerLives > 0
    display "You have " + playerLives + " lives left."
    playerLives = playerLives - 1

Practical Example of a FOR Loop:

FOR each enemy in enemyList
    attack(enemy)

Step 3: Creating Functions

Functions are reusable blocks of code that can be called upon to execute tasks. They help organize code and reduce repetition.

How to Define a Function:

  • Start with a keyword (often 'function' or similar).
  • Give it a name.
  • Specify any parameters it needs (inputs).
  • Define the code block it will execute.

Practical Example:

FUNCTION healPlayer(amount)
    playerHealth = playerHealth + amount
    display "Player healed by " + amount

Step 4: Implementing Functions in Your Game

Once functions are defined, you can call them throughout your program to perform actions without rewriting code.

Example of Calling a Function:

healPlayer(20)  // This will increase the player's health by 20

Conclusion

You've learned the foundational elements of programming: statements, loops, and functions. These concepts are crucial for controlling the flow of your programs. Start practicing by creating simple games or projects where you can implement these elements. The next steps could include diving into specific programming languages or exploring more complex data structures and algorithms. Happy coding!