πŸ’» Computer Science Study Guide

Coding Basics Guide

Master the fundamentals of programmingβ€”variables, data types, loops, functions, and more. Your journey to becoming a coder starts here.

πŸ’» Overview

Programming is the process of giving instructions to a computer to perform specific tasks. These instructions are written in programming languages that the computer can understand. This guide covers the fundamental building blocks that are common across most programming languages.

What You'll Learn

  • Variables: How to store and manage data
  • Data Types: Different kinds of data (numbers, text, etc.)
  • Operators: How to perform calculations and comparisons
  • Conditionals: Making decisions in your code
  • Loops: Repeating actions efficiently
  • Functions: Organizing code into reusable blocks

πŸ“¦ Variables

A variable is like a labeled box that stores data. You give it a name, and you can put a value inside it. Later, you can retrieve or change that value.

// Creating variables let playerName = "Alex"; // Stores text let score = 100; // Stores a number let isGameOver = false; // Stores true/false // Changing a variable's value score = 150; // Now score is 150

Variable Naming Rules

  • Must start with a letter, underscore (_), or dollar sign ($)
  • Can contain letters, numbers, underscores, dollar signs
  • Cannot use reserved keywords (like if, for, while)
  • Are case-sensitive (score β‰  Score)
πŸ’‘ Best Practice: Use Descriptive Names

Use playerScore instead of ps or x. Clear names make your code easier to understand.

πŸ“Š Data Types

Data types tell the computer what kind of data a variable holds. Different types behave differently.

Type Description Example
String Text (letters, words, sentences) "Hello, World!"
Number/Integer Whole numbers 42, -7, 0
Float/Double Decimal numbers 3.14, -0.5
Boolean True or false values true, false
Array/List Collection of values [1, 2, 3, 4]
// Different data types let name = "Sarah"; // String let age = 25; // Number (Integer) let height = 5.7; // Float let isStudent = true; // Boolean let colors = ["red", "blue", "green"]; // Array

πŸ”’ Operators

Arithmetic Operators

Operator Name Example Result
+Addition5 + 38
-Subtraction10 - 46
*Multiplication6 * 742
/Division20 / 45
%Modulus (remainder)17 % 52

Comparison Operators

Operator Meaning Example Result
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than10 > 5true
<Less than3 < 8true
>=Greater than or equal5 >= 5true
<=Less than or equal4 <= 7true

Logical Operators

Operator Name Description
&&ANDBoth conditions must be true
||ORAt least one condition must be true
!NOTReverses the boolean value

πŸ”€ Conditionals (If/Else)

Conditionals let your program make decisions based on whether something is true or false.

let age = 18; if (age >= 18) { console.log("You can vote!"); } else { console.log("You're too young to vote."); }
πŸ“– Multiple Conditions (else if)
let score = 85; if (score >= 90) { console.log("Grade: A"); } else if (score >= 80) { console.log("Grade: B"); } else if (score >= 70) { console.log("Grade: C"); } else { console.log("Grade: F"); } // Output: Grade: B
🧠 Remember the Flow
IF this β†’ THEN that β†’ ELSE something else

πŸ”„ Loops

Loops let you repeat code multiple times without writing it over and over.

For Loop

Use when you know how many times to repeat.

// Print numbers 1 to 5 for (let i = 1; i <= 5; i++) { console.log(i); } // Output: 1, 2, 3, 4, 5

For Loop Breakdown

  • let i = 1 β†’ Start at 1 (initialization)
  • i <= 5 β†’ Keep going while i is ≀ 5 (condition)
  • i++ β†’ Add 1 to i each time (increment)

While Loop

Use when you don't know how many times to repeat.

let count = 0; while (count < 3) { console.log("Count is: " + count); count++; } // Output: Count is: 0, Count is: 1, Count is: 2
⚠️ Infinite Loop Warning

Always make sure your loop has a way to stop! If the condition never becomes false, your program will freeze.

βš™οΈ Functions

Functions are reusable blocks of code that perform a specific task. Think of them as mini-programs within your program.

// Defining a function function greet(name) { return "Hello, " + name + "!"; } // Calling the function let message = greet("Alex"); console.log(message); // Output: Hello, Alex!

Function Parts

  • Name: What you call the function (greet)
  • Parameters: Inputs the function accepts (name)
  • Body: The code inside the curly braces
  • Return: The value the function gives back
πŸ“– Function Example: Calculate Area
function calculateArea(width, height) { return width * height; } let area = calculateArea(5, 10); console.log(area); // Output: 50
πŸ’‘ Why Use Functions?
  • Reusability: Write once, use many times
  • Organization: Break complex problems into smaller parts
  • Readability: Give meaningful names to blocks of code
  • Debugging: Easier to find and fix errors

✏️ Practice Problems

Problem 1 Easy
What will be printed? let x = 10; x = x + 5; console.log(x);

Answer: 15

Explanation: x starts at 10. Then we add 5 to it (10 + 5 = 15) and store it back in x.

Problem 2 Medium
Write a function that takes a number and returns "even" or "odd".
function checkEvenOdd(num) { if (num % 2 === 0) { return "even"; } else { return "odd"; } } console.log(checkEvenOdd(4)); // "even" console.log(checkEvenOdd(7)); // "odd"

The modulus operator (%) gives the remainder. If a number divided by 2 has remainder 0, it's even.

Problem 3 Medium
Write a loop that prints the numbers 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 (countdown).
for (let i = 10; i >= 1; i--) { console.log(i); } // Alternative with while loop: let count = 10; while (count >= 1) { console.log(count); count--; }

Note: i-- decreases i by 1 each iteration (opposite of i++).

Problem 4 Hard
Write a function that calculates the factorial of a number. (Example: 5! = 5 Γ— 4 Γ— 3 Γ— 2 Γ— 1 = 120)
function factorial(n) { let result = 1; for (let i = n; i >= 1; i--) { result = result * i; } return result; } console.log(factorial(5)); // 120 console.log(factorial(3)); // 6

We start with result = 1, then multiply by each number from n down to 1.

⚠️ Common Mistakes

❌ Using = instead of == in conditions

if (x = 5) assigns 5 to x. Use if (x == 5) to compare.

❌ Forgetting semicolons

Most languages need semicolons at the end of statements. Missing them can cause errors.

❌ Off-by-one errors in loops

Starting at 0 vs 1, using < vs <= can cause loops to run one time too many or too few.

❌ Case sensitivity

myVariable and myvariable are different variables. Be consistent!

Track Your Coding Journey

Use Centauri to organize your programming projects and learning goals.

Get Early Access

πŸ“š Further Resources