



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




(45 respondents)
1) General comments on lecture / class. (2 students) Class is too early : Sorry not much I can do about that... more than maybe agree that Monday 9am is very early indeed.
Can you post your examples online? I will put all my in-class-examples in a .zip file and distribute to class.
Where can we find lots of practice problems? As mentioned in earlier mud response, take a look in the Feldman Ada book or any other Ada book, there are lots and lots of examples to work through, and many of them alos have solutions on the CD or within the book.
2) What is the difference between a Definite and Indefinite while loop? (1 students) While loops are generally used for indefinite iteration i.e. the number of times the loop has to execute has not been determined. A For loop is used for definite iteration i.e. the number of times the loop has been executed is predetermined. You can replace a For loop with a while loop, but the substitution is prone to error because the tests.
Algorithm to convert a For loop into a While loop:
a. The For loop is replaced by ‘While (loop_variable >= upper_bound) loop’ b. Decrement loop_variable by 1 c. Replace every occurrence of the For Loop counter by loop_variable d. Rest of program body
-- for for_loop_counter in lower_bound .. upper_bound for I in 1 .. N loop --loop body end loop;
-- replace with while Loop_Variable <= Upper_Bound loop Loop_Variable := Loop_Variable + 1; -- loop body end loop;
3) Sentinel and Flag loops? (4 students) What is their syntax? Are they special kinds of while loops? When is it good to use them? What real-life cases would one use sentinel loops? How is using a flag more effective than other techniques? Sentinel loops have a special variable called a sentinel. The loop variable is compared to the sentinel to decide the exit condition.
For example: Sentinel : constant Integer := 20; Number : Integer;
loop Ada.Text_IO.Put("Please Enter a number:"); Ada.Text_Io.Get(Number);
exit when Number = Sentinel; -- do something with the number; end loop;
Here, the loop will exit when the user enters ‘20’. If any other number is entered, the part after the exit condition will be executed. The idea of a sentinel loop is applicable to both general loops and While loops. The same code if written using a while loop will appear as follows: -- variable declarations Sentinel : constant Integer := 20; Number : Integer;
-- main program body Ada.Text_Io.Put("Please Enter a number:");
end if; end loop;
if (Aircraft_Too_Close) then -- Inform the pilot that aircraft too close -- Perform safety maneuver end if;
4) How would incorporate this into a package you are putting together? (1 student)
5) Back to the first slide, what is a "case"? (1 student) The case statement provides an elegant means of choosing between multiple options (the multiple if statement can be hard to debug).
The general format of the case statement is:
case selector is when value_list_1 => statement(s)_1; when value_list_2 => statement(s)_2; … when others => statement(s)_n; end case ;
For example: -- variable declaration type Possible_Choices is (1, 2, 3, 4, 5); package Choice_Io is new Ada.Text_Io.Enumeration_Io(Possible_Choices);
My_Choice : Possible_Choices;
--main program
Ada.Text_IO.Put("Please Enter Your Choice"); Choice_Io.Get(My_Choice);
case My_Choice is when 1 => --execute code for option 1 when 2 => --execute code for option 2 when others =>
-- execute code for other three options end case;
Here the others option allows the programmer to bundle the code for the options 3,4 and 5 together. The equivalent ‘if-statement’ implementation is shown below:
--main program
Ada.Text_IO.Put("Please Enter Your Choice"); Choice_Io.Get(My_Choice);
if My_Choice = 1 then --execute code for option 1 elsif My_Choice = 2 then --execute code for option 2 else --execute code for other three options end if;
In addition to being hard to read, the code above is hard to modify.
6) Comments on examples given on slides. (4 students) Was the code on the PowerPoint slides in Ada syntax? No, not on all of them, for example slide 15 "Sentinel-controlled loops" is not Ada syntax, rather "pseudo code".
The program didn't need a capital or lowercase Y or N neccessarily Not sure what this question really is asking, the program will keep on executing the statements within the loop until a 'y' or 'Y' or 'n' or 'N' is enetered as input to the program.
What was the reasoning why the example of the while loop would not be executed at all? Are you commenting on slide 15, "A while loop may not execute at all"?
Tot := 0; Put("Enter j (-1 to exit): "); Get(J); Skip_Line;
while (J /= -1) loop Tot := Tot + J; Put("Enter j (-1 to exit): "); Get(J); Skip_Line; end loop;
end loop; Can you use 'i' in operations in a loop? For example:
Bob := 2; for i in 1 .. val loop bob = new_number + i; end loop;
Loop variables can be used on the right-hand side of computations. You cannot change the value of the for-loop variable inside the body of the loop.
In a for statement, does the loop_var need to be defined in the declartions if it is given no value befor the loop? The loop variable does not have to be defined earlier in the program when using ‘for’ loops.
What is the point of having a package and its body in separate files? Having the package specification and body in separate files allows multiple programmers to work on the implementation of the same package and have the same view of the interfaces (subprogram calls, data structures).
Can a procedure call itself in Ada? Yes. A subprogram calling itself is how simple recursion is implemented. How can I clear the screen in Ada? Use the Screen package. There is a clear screen function that you can use in that package.
Why do you have to update the loop parameter for while loops? In a for-loop, the loop variable is incremented automatically. In a while loop however there is no implicit increment operation. It is the responsibility of the programmer to take care of the incrementing.
What happens when I write this: loop exit when variable in type; put(item => "please try again."); end loop; the ‘variable in type’ operation checks to see if the variable is of the specified type. The control will pass out of the loop if the variable is in the specified type.
10) "No mud" (21 students) Well, that is great!