In this section, we will explore the different types of loops available in Ada. Loops are fundamental constructs in programming that allow you to execute a block of code multiple times. Ada provides several looping constructs to handle various scenarios efficiently.

Types of Loops in Ada

  1. For Loop
  2. While Loop
  3. Loop with Exit

  1. For Loop

The for loop in Ada is used to iterate over a range of values. It is particularly useful when you know the number of iterations in advance.

Syntax

for <loop_variable> in <start_value> .. <end_value> loop
   -- Statements to execute
end loop;

Example

with Ada.Text_IO; use Ada.Text_IO;

procedure For_Loop_Example is
begin
   for I in 1 .. 5 loop
      Put_Line("Iteration: " & Integer'Image(I));
   end loop;
end For_Loop_Example;

Explanation

  • I is the loop variable.
  • 1 .. 5 is the range of values.
  • The loop will execute 5 times, printing the iteration number each time.

  1. While Loop

The while loop in Ada is used when the number of iterations is not known in advance. The loop continues to execute as long as a specified condition is true.

Syntax

while <condition> loop
   -- Statements to execute
end loop;

Example

with Ada.Text_IO; use Ada.Text_IO;

procedure While_Loop_Example is
   Counter : Integer := 1;
begin
   while Counter <= 5 loop
      Put_Line("Iteration: " & Integer'Image(Counter));
      Counter := Counter + 1;
   end loop;
end While_Loop_Example;

Explanation

  • Counter is initialized to 1.
  • The loop continues as long as Counter is less than or equal to 5.
  • Counter is incremented by 1 in each iteration.

  1. Loop with Exit

The loop with exit construct in Ada is a more flexible looping mechanism. It allows you to exit the loop based on a condition that can be checked at any point within the loop.

Syntax

loop
   -- Statements to execute
   exit when <condition>;
end loop;

Example

with Ada.Text_IO; use Ada.Text_IO;

procedure Loop_With_Exit_Example is
   Counter : Integer := 1;
begin
   loop
      Put_Line("Iteration: " & Integer'Image(Counter));
      exit when Counter = 5;
      Counter := Counter + 1;
   end loop;
end Loop_With_Exit_Example;

Explanation

  • Counter is initialized to 1.
  • The loop continues indefinitely until Counter equals 5.
  • The exit when statement is used to break out of the loop when the condition is met.

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

with Ada.Text_IO; use Ada.Text_IO;

procedure Sum_N_Natural_Numbers is
   N : Integer := 10;
   Sum : Integer := 0;
begin
   for I in 1 .. N loop
      Sum := Sum + I;
   end loop;
   Put_Line("Sum of first " & Integer'Image(N) & " natural numbers is: " & Integer'Image(Sum));
end Sum_N_Natural_Numbers;

Exercise 2: Factorial Calculation

Write a program to calculate the factorial of a given number using a while loop.

Solution

with Ada.Text_IO; use Ada.Text_IO;

procedure Factorial_Calculation is
   N : Integer := 5;
   Factorial : Integer := 1;
   Counter : Integer := 1;
begin
   while Counter <= N loop
      Factorial := Factorial * Counter;
      Counter := Counter + 1;
   end loop;
   Put_Line("Factorial of " & Integer'Image(N) & " is: " & Integer'Image(Factorial));
end Factorial_Calculation;

Exercise 3: Find the First Multiple of 7

Write a program to find the first multiple of 7 greater than 50 using a loop with exit.

Solution

with Ada.Text_IO; use Ada.Text_IO;

procedure First_Multiple_Of_7 is
   Number : Integer := 51;
begin
   loop
      exit when Number mod 7 = 0;
      Number := Number + 1;
   end loop;
   Put_Line("The first multiple of 7 greater than 50 is: " & Integer'Image(Number));
end First_Multiple_Of_7;

Common Mistakes and Tips

  • Off-by-One Errors: Ensure that your loop ranges and conditions are correctly set to avoid off-by-one errors.
  • Infinite Loops: Be cautious with while loops and loop with exit to ensure that the exit condition will eventually be met.
  • Variable Initialization: Always initialize your loop control variables to avoid unexpected behavior.

Conclusion

In this section, we covered the different types of loops in Ada, including for loops, while loops, and loop with exit. We also provided practical examples and exercises to reinforce the concepts. Understanding these looping constructs is essential for writing efficient and effective Ada programs. In the next section, we will delve into subprograms, including procedures and functions, to further enhance your Ada programming skills.

© Copyright 2024. All rights reserved