Control structures are fundamental in any programming language as they allow you to control the flow of execution in your programs. In Ada, control structures include conditional statements, case statements, and loops. This section will cover these control structures in detail, providing examples and exercises to help you understand their usage.

Conditional Statements

Conditional statements allow you to execute certain parts of your code based on specific conditions. Ada provides two main types of conditional statements: if statements and case statements.

If Statements

The if statement in Ada is used to execute a block of code if a specified condition is true. It can also include elsif and else parts to handle multiple conditions.

Syntax

if condition then
   -- statements to execute if condition is true
elsif another_condition then
   -- statements to execute if another_condition is true
else
   -- statements to execute if none of the above conditions are true
end if;

Example

procedure Check_Number is
   Number : Integer := 10;
begin
   if Number > 0 then
      Put_Line("Number is positive");
   elsif Number < 0 then
      Put_Line("Number is negative");
   else
      Put_Line("Number is zero");
   end if;
end Check_Number;

Case Statements

The case statement is used to select one of many possible blocks of code to execute. It is similar to the switch statement in other programming languages.

Syntax

case expression is
   when value1 =>
      -- statements to execute if expression equals value1
   when value2 =>
      -- statements to execute if expression equals value2
   when others =>
      -- statements to execute if expression does not match any of the above values
end case;

Example

procedure Day_Of_Week is
   Day : Integer := 3;
begin
   case Day is
      when 1 =>
         Put_Line("Monday");
      when 2 =>
         Put_Line("Tuesday");
      when 3 =>
         Put_Line("Wednesday");
      when 4 =>
         Put_Line("Thursday");
      when 5 =>
         Put_Line("Friday");
      when 6 =>
         Put_Line("Saturday");
      when 7 =>
         Put_Line("Sunday");
      when others =>
         Put_Line("Invalid day");
   end case;
end Day_Of_Week;

Loops

Loops are used to execute a block of code repeatedly. Ada provides several types of loops: for loops, while loops, and loop statements.

For Loops

The for loop is used to iterate over a range of values.

Syntax

for variable in start_value .. end_value loop
   -- statements to execute in each iteration
end loop;

Example

procedure Print_Numbers is
begin
   for I in 1 .. 5 loop
      Put_Line(Integer'Image(I));
   end loop;
end Print_Numbers;

While Loops

The while loop executes a block of code as long as a specified condition is true.

Syntax

while condition loop
   -- statements to execute while condition is true
end loop;

Example

procedure Count_Down is
   Counter : Integer := 5;
begin
   while Counter > 0 loop
      Put_Line(Integer'Image(Counter));
      Counter := Counter - 1;
   end loop;
end Count_Down;

Loop Statements

The loop statement is a general-purpose loop that can be controlled using exit statements.

Syntax

loop
   -- statements to execute
   exit when condition;
end loop;

Example

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

Practical Exercises

Exercise 1: Even or Odd

Write a procedure that checks if a given number is even or odd and prints the result.

Solution

procedure Even_Or_Odd is
   Number : Integer := 4;
begin
   if Number mod 2 = 0 then
      Put_Line("Number is even");
   else
      Put_Line("Number is odd");
   end if;
end Even_Or_Odd;

Exercise 2: Grade Evaluation

Write a procedure that evaluates a student's grade based on a score and prints the corresponding grade (A, B, C, D, F).

Solution

procedure Grade_Evaluation is
   Score : Integer := 85;
begin
   case Score is
      when 90 .. 100 =>
         Put_Line("Grade: A");
      when 80 .. 89 =>
         Put_Line("Grade: B");
      when 70 .. 79 =>
         Put_Line("Grade: C");
      when 60 .. 69 =>
         Put_Line("Grade: D");
      when others =>
         Put_Line("Grade: F");
   end case;
end Grade_Evaluation;

Exercise 3: Sum of Numbers

Write a procedure that calculates the sum of numbers from 1 to 10 using a for loop and prints the result.

Solution

procedure Sum_Of_Numbers is
   Sum : Integer := 0;
begin
   for I in 1 .. 10 loop
      Sum := Sum + I;
   end loop;
   Put_Line("Sum: " & Integer'Image(Sum));
end Sum_Of_Numbers;

Conclusion

In this section, we covered the essential control structures in Ada, including conditional statements (if and case), and loops (for, while, and loop). Understanding these control structures is crucial for controlling the flow of your programs and making decisions based on conditions. Practice the provided exercises to reinforce your understanding and prepare for more advanced topics in Ada programming.

© Copyright 2024. All rights reserved