Lesson 2: Basic Syntax and Control
Structures
Lesson Objectives
By the end of this lesson, you will be able to:
Understand the syntax rules of a programming language.
Use variables and data types correctly.
Apply conditional statements and loops to control program flow.
1. Understanding Syntax
Every programming language has a set of rules that define how programs are written. These
include:
Case sensitivity (e.g., name and Name are different variables in Python).
Proper use of indentation and brackets.
Using semicolons (in some languages like JavaScript and Java).
Example (Python):
print("Hello, World!") # Correct
Print("Hello, World!") # Incorrect (case-sensitive)
2. Variables and Data Types
In programming, variables store data that can change. Each variable has a type, such as:
String: "Hello"
Integer: 25
Float: 3.14
Boolean: True or False
Example (JavaScript):
let name = "Alice"; // String
let age = 25; // Integer
let height = 5.4; // Float
let isStudent = true; // Boolean
3. Conditional Statements
Conditional statements allow the program to make decisions.
Example (Python):
age = 18
if age >= 18:
print("You can vote.")
else:
print("You cannot vote.")
4. Loops
Loops help execute a block of code multiple times.
For Loop (Executes a fixed number of times)
Example (JavaScript):
for (let i = 0; i < 5; i++) {
console.log("Iteration: " + i);
}
While Loop (Executes until a condition is false)
Example (Python):
count = 0
while count < 5:
print("Count:", count)
count += 1
5. Functions
Functions allow code reuse by defining a set of instructions once and calling them multiple
times.
Example (Python):
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
Next Steps
Practice writing simple programs with conditions and loops.
Experiment with different data types and functions.
Learn about arrays and objects in programming.