Introduction to Programming
Introduction
In computer science, programming is the process of creating a set of instructions that tell a computer how to perform a task. These instructions are written in a programming language, which is then translated by a compiler or interpreter into machine code that the computer can execute. Programming is essential for developing software, websites, apps, and various other digital solutions.
Variables and Data Types
Variables
Variables are containers for storing data values. In programming, a variable is defined by a unique name and a data type. The value stored in a variable can be changed during the program's execution.
Example:
# Declaring and initializing variables in Python
x = 5
y = "Hello, World!"
Data Types
Data types specify the type of data that a variable can hold. Common data types include integers, floating-point numbers, strings, and booleans.
Example:
// Declaring and initializing variables in Java
int age = 25;
double height = 1.75;
String name = "Alice";
boolean isStudent = true;
Control Structures
Conditional Statements
Conditional statements allow the program to make decisions based on certain conditions. The most common types are if statements and switch statements.
Example (Python):
# Using if-else statement in Python
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
Loops
Loops are used to repeat a block of code multiple times. Common loop types include for loops and while loops.
Example (Java):
// Using a for loop in Java
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
Functions
Functions are blocks of code that perform a specific task. They allow for code reusability and modularization.
Example (Python):
# Defining and calling a function in Python
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
Arrays and Lists
Arrays
Arrays are data structures that store a collection of elements of the same data type. Elements in an array are accessed using an index.
Example (C++):
// Declaring and initializing an array in C++
int numbers[5] = {1, 2, 3, 4, 5};
Lists
Lists are similar to arrays but can store elements of different data types. In Python, lists are dynamic and can be easily modified.
Example (Python):
# Creating a list in Python
fruits = ["apple", "banana", "orange"]
Common Mistakes
- Forgetting to initialize variables before using them.
- Using the wrong data type for a specific operation.
- Not handling edge cases in conditional statements.
Key Points
- Programming involves creating instructions for computers to perform tasks.
- Variables store data values and have specific data types.
- Control structures like loops and conditional statements help control program flow.
- Functions are reusable blocks of code that perform specific tasks.
- Arrays and lists are used to store collections of data.
Practice Questions
-
Write a Python function to calculate the factorial of a given number.
Answer:
def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) -
Explain the difference between a for loop and a while loop in programming.
Answer: A for loop is used when the number of iterations is known, while a while loop is used when the condition for repetition is based on a boolean expression.
-
Create a Java program that prints the Fibonacci series up to 10 terms.
Answer:
int n = 10, firstTerm = 0, secondTerm = 1; for (int i = 1; i <= n; i++) { System.out.print(firstTerm + " "); int nextTerm = firstTerm + secondTerm; firstTerm = secondTerm; secondTerm = nextTerm; } -
What is the output of the following Python code snippet?
x = 5 y = 3 z = x + y * 2 print(z)Answer: 11
-
Explain the concept of scope in programming.
Answer: Scope refers to the visibility and accessibility of variables within a program. Variables can have local or global scope, affecting where they can be accessed and modified.
Want to save these Introduction to Programming notes?
Create a free account to bookmark notes, download past papers, track your revision and get AI study help - free for Kenyan students.
Already have one? Log in
Frequently Asked Questions
Other Grade 11 Computer Science topics
Get free notes & past papers by email
Join our list and we'll send fresh study notes and past papers straight to your inbox.