



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Modern Programing Language is about different languages of today era. It explains pros and cons of some new languages and their differences with old ones. Languages like java, c sharp, c plus plus, c, fotran are included in this course. This lecture handout is about: Assignment, Statement, Variable, Case, Combination, Range, Sequence, Executing, equivalent, Separated
Typology: Exercises
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Assignment statement
Like all imperative languages, Ada also supports the assignment statement. However, in Ada the assignment is not an expression like C. The syntax of the assignment statement is as follows:
Variable := expression ;
Note that, in Ada, โ:=โ is used as the assignment operator whereas โ=โ is used as assignment operator in C.
If Statement
The Ada if statement is fully blocked. In fact, all Ada control structures are fully blocked. That means, we do not have two separate forms if we need only one statement to be executed if the condition for the if statement is true and if we want more than one statements. The body of the if statements is always in side a a complete block that starts with the then keyword and end with end if as shown below:
if condition then -- statement(s) end if ;
If an if statement has multiple else parts with separate conditions, then Ada uses the keyword elsif to indicate that. The else part is optional and comes at the end. Each if statement must have a corresponding end if in it. The following examples demonstrate this concept.
if condition1 then -- statement(s) elsif condition2 then if condition3 then -- statements end if; -- statement(s) โฆ else -- statement(s) end if ;
if condition1 then -- statements elsif condition2 then if condition3 then -- statements end if; -- statements โฆ else if condition4 then
-- this is a new if statement and hence must have its own end if -- statements end if; -- statements end if ;
Case Statements
Case statement is like the switch statement in C, but as compared to C, it is safer and more structured. Its syntax is as shown below:
case expression is when choice list => sequence-of-statements when choice list => sequence-of-statements when others => sequence-of-statements end case;
As opposed to C, the case statement is fully structured; there is no equivalent of the break statement and the control does not fall through to the next choice after executing the set of statements for the selected choice. The choice _list can have more than one values specified in the form of a range specified with the .. operator like 1..10, discrete values separated by | such as a | e | i | o | u, or a combination of both. The following example elaborates this concept.
case TODAY is when MON .. THU => WORK; when FRI => WORK; PARTY; when SAT | SUN => REST; end case;
All values in when branches must be static and all possible values of expression must be included exactly once. In Ada, the case expression must match one of the choices given in the when clause as an exception will be raised as run time if there is no match. when others => is like default in C and is used to cover all the rest of the choices not mentioned in the above when clauses.
There are three types of loops in Ada:
While and For Loops
Ada has only the pre-test while loop and does not support the post-test loop like the do- while statement in C. The while statement in Ada has the following structure.
while condition loop -- Loop body goes here end loop ;
The semantics of the while statement are exactly the same as its counterpart in C.
The for statement in Ada is however quite different here. In C, the for statement is virtually the same as the while statement as the condition in the C for statement is checked in each iteration and the number of iterations therefore cannot be determined upfront.
In Ada for statement, the number of iterations are fixed the first time the control hits the for statement and is specified by a range just like the choice_list in the case statement as shown below:
for variable in low_value .. high_value loop -- Loop body goes here end loop ;
The value of the loop variable can be used but cannot be changed inside the loop body. The loop counting can be done in the reverse order as well. This is shown below.
for variable in reverse high_value .. low_value loop -- Loop body goes here end loop ;
Exit statement
The exit statement can be used with our without a when condition as mentioned in the case of the loop statement. exit ; exit when condition;
It can only be used in a loop. It can use labels and can be used to specify the specific loop to exit as shown below.
Outer : loop Inner : loop โฆ exit Inner when Done; end loop Inner; end loop Outer;
Block Statement
Block statement in Ada is very similar to a block in C. It can be used anywhere and has the following structure:
declare -- declare section optional declarations begin statements exception -- exception section optional handlers end ;
Return statement
The return statement can only be used in subprograms and has the following form:
return ; -- procedure return expression ; -- function
Function must have at least one return.
Raise statement
Raise statement is like throw statement in C++ and is used to raise exception as shown below:
raise exception-name ;
Declaring and Handling Exceptions
Ada was the first language to provide a structured exception handling mechanism. C++ exception handling is very similar to Ada. In Ada, the programmer can declare, raise, and handle exception.
Declaring an exception Exceptions are declared just like any other variable before they can be raised. This is shown in the following example:
Error, Disaster : exception ;
Raising an exception
Like throw in C++, an Ada programmer can explicitly raise an exception and it strips stack frames till a handler is found.
raise Disaster;
Handling an exception