In this section, we will explore loops and iteration in Delphi/Object Pascal. Loops are fundamental constructs that allow you to execute a block of code multiple times. Understanding how to use loops effectively is crucial for writing efficient and readable code.
Key Concepts
- For Loop: Used when the number of iterations is known beforehand.
- While Loop: Used when the number of iterations is not known and depends on a condition.
- Repeat-Until Loop: Similar to the while loop but checks the condition after executing the loop body.
For Loop
The for loop is used to iterate over a range of values. It is particularly useful when you know the exact number of iterations.
Syntax
Example
program ForLoopExample;
var
i: Integer;
begin
for i := 1 to 5 do
begin
WriteLn('Iteration: ', i);
end;
end.Explanation
iis the loop variable.- The loop starts with
iequal to 1 and incrementsiby 1 after each iteration untiliequals 5. WriteLnoutputs the current value ofi.
While Loop
The while loop is used when the number of iterations is not known in advance and depends on a condition.
Syntax
Example
program WhileLoopExample;
var
i: Integer;
begin
i := 1;
while i <= 5 do
begin
WriteLn('Iteration: ', i);
i := i + 1;
end;
end.Explanation
- The loop continues as long as the condition
i <= 5is true. iis incremented by 1 in each iteration.
Repeat-Until Loop
The repeat-until loop is similar to the while loop but checks the condition after executing the loop body.
Syntax
Example
program RepeatUntilExample;
var
i: Integer;
begin
i := 1;
repeat
WriteLn('Iteration: ', i);
i := i + 1;
until i > 5;
end.Explanation
- The loop executes the code block first and then checks the condition
i > 5. - The loop stops when
ibecomes greater than 5.
Practical Exercises
Exercise 1: Sum of First N Natural Numbers
Write a program to calculate the sum of the first N natural numbers using a for loop.
Solution
program SumOfNaturalNumbers;
var
i, N, sum: Integer;
begin
sum := 0;
Write('Enter the value of N: ');
ReadLn(N);
for i := 1 to N do
begin
sum := sum + i;
end;
WriteLn('Sum of first ', N, ' natural numbers is: ', sum);
end.Exercise 2: Factorial of a Number
Write a program to calculate the factorial of a number using a while loop.
Solution
program Factorial;
var
i, N, factorial: Integer;
begin
factorial := 1;
Write('Enter a number: ');
ReadLn(N);
i := 1;
while i <= N do
begin
factorial := factorial * i;
i := i + 1;
end;
WriteLn('Factorial of ', N, ' is: ', factorial);
end.Exercise 3: Reverse a Number
Write a program to reverse a given number using a repeat-until loop.
Solution
program ReverseNumber;
var
num, reversedNum, remainder: Integer;
begin
reversedNum := 0;
Write('Enter a number: ');
ReadLn(num);
repeat
remainder := num mod 10;
reversedNum := reversedNum * 10 + remainder;
num := num div 10;
until num = 0;
WriteLn('Reversed number is: ', reversedNum);
end.Common Mistakes and Tips
- Off-by-One Errors: Ensure that your loop conditions are correctly set to avoid executing the loop one time too many or too few.
- Infinite Loops: Always make sure that the loop condition will eventually become false. For example, in a
whileloop, ensure that the loop variable is updated within the loop. - Initialization: Properly initialize your loop variables before entering the loop.
Conclusion
In this section, we covered the basics of loops and iteration in Delphi/Object Pascal. We explored the for, while, and repeat-until loops, provided practical examples, and included exercises to reinforce the concepts. Understanding these loops will help you write more efficient and readable code. In the next section, we will delve into procedures and functions, which are essential for structuring your code and promoting reusability.
