Resources | Subject Notes | Computer Science | Lesson Plan
This section explores fundamental programming concepts: sequence, selection (conditional statements), and iteration (loops). Understanding these concepts is crucial for writing effective and logical computer programs.
Sequence refers to the order in which instructions are executed in a program. Instructions are typically executed one after the other, from top to bottom.
Example:
Selection statements allow a program to make decisions and execute different blocks of code based on whether a condition is true or false. The primary selection statement is the if
statement.
if
StatementThe if
statement checks a condition. If the condition is true, the code block within the if
statement is executed. If the condition is false, the code block is skipped.
Syntax:
if (condition) {
// Code to execute if the condition is true
}
Example:
number = 10
if (number > 5) {
print "Number is greater than 5"
}
else
StatementThe else
statement provides an alternative block of code to execute if the condition in the if
statement is false.
Syntax:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example:
number = 3
if (number > 5) {
print "Number is greater than 5"
} else {
print "Number is not greater than 5"
}
else if
StatementThe else if
statement allows you to check multiple conditions in sequence. It's useful when you need to execute different code blocks based on different possible outcomes.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the conditions are true
}
Iteration allows a block of code to be executed repeatedly. There are three main types of loops: for
, while
, and do-while
.
for
LoopThe for
loop is used when you know in advance how many times you want to repeat a block of code. It consists of a loop counter, an initial value, a condition, and an increment/decrement statement.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to be repeated
}
Example:
for (i = 1; i <= 5; i = i + 1) {
print i
}
while
LoopThe while
loop is used when you want to repeat a block of code as long as a condition is true. The condition is checked at the beginning of each iteration.
Syntax:
while (condition) {
// Code to be repeated
}
Example:
i = 1
while (i <= 5) {
print i
i = i + 1
}
do-while
LoopThe do-while
loop is similar to the while
loop, but the code block is executed at least once before the condition is checked.
Syntax:
do {
// Code to be repeated
} while (condition);
Example:
i = 1
do {
print i
i = i + 1
} while (i <= 5);
Concept | Description | Example |
---|---|---|
Sequence | Instructions executed in order. | Input, process, output. |
Selection (if/else) | Executing different code blocks based on conditions. | Checking if a number is positive or negative. |
Iteration (for loop) | Repeating a block of code a specific number of times. | Printing numbers from 1 to 5. |
Iteration (while loop) | Repeating a block of code as long as a condition is true. | Printing numbers from 1 to 5. |