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

  1. For Loop: Used when the number of iterations is known beforehand.
  2. While Loop: Used when the number of iterations is not known and depends on a condition.
  3. 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

for variable := start_value to end_value do
begin
  // Code to execute
end;

Example

program ForLoopExample;
var
  i: Integer;
begin
  for i := 1 to 5 do
  begin
    WriteLn('Iteration: ', i);
  end;
end.

Explanation

  • i is the loop variable.
  • The loop starts with i equal to 1 and increments i by 1 after each iteration until i equals 5.
  • WriteLn outputs the current value of i.

While Loop

The while loop is used when the number of iterations is not known in advance and depends on a condition.

Syntax

while condition do
begin
  // Code to execute
end;

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 <= 5 is true.
  • i is 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

repeat
  // Code to execute
until condition;

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 i becomes 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 while loop, 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.

Delphi/Object Pascal Programming Course

Module 1: Introduction to Delphi/Object Pascal

Module 2: Control Structures and Procedures

Module 3: Working with Data

Module 4: Object-Oriented Programming

Module 5: Advanced Delphi Features

Module 6: GUI Development with VCL and FMX

Module 7: Web and Mobile Development

Module 8: Best Practices and Design Patterns

Module 9: Final Project

© Copyright 2024. All rights reserved