1

I'm working with Ada, and the lack of braces on the control statements have been getting me a bit mixed up. I have written the following if-else statement:

if i = 1 then
    largest := nextInteger;
else if i = 2 then
    if nextInteger > largest then
        secondLargest := largest;
        largest := nextInteger;
    else
        secondLargest := nextInteger;
    end if;
else
    if nextInteger > largest then
        secondLargest := largest;
        largest := nextInteger;
    else if largest > nextInteger and then nextInteger > secondLargest then
        secondLargest := nextInteger;
    end if;
end if;

The error message that I get is:

program_one.adb:15:05: missing "end if:" for "if" at line 3
program_one.adb:15:05: missing "end if;" for "if" at line 1

I'm struggling to find where I didn't close out an if statement. It's late and I've been working all day, so I may just be tired. Can anyone help?

mpowell48
  • 43
  • 8
  • `and then nextInteger > secondLargest then` you want to get rid of that first `then` – Paul Abbott Sep 22 '16 at 00:05
  • That did not get rid of the error messages. – mpowell48 Sep 22 '16 at 00:11
  • 1
    "and then" is a short circuit and operation - the second part is performed only in case first part is true. there is corresponding "or else" for short circuit or. As such getting rid of "then" after and doesn't give anything in particular - though you shouldn't use short-circuit forms unless you really need them. – darkestkhan Sep 22 '16 at 10:52
  • noted, and thank you. – mpowell48 Sep 22 '16 at 15:17

2 Answers2

6

try like this

if i = 1 then
    largest := nextInteger;
elsif i = 2 then
    if nextInteger > largest then
        secondLargest := largest;
        largest := nextInteger;
    else
        secondLargest := nextInteger;
    end if;
else
    if nextInteger > largest then
        secondLargest := largest;
        largest := nextInteger;
    elsif largest > nextInteger and then nextInteger > secondLargest then
        secondLargest := nextInteger;
    end if;
end if;
  • 2
    @mpowell48: You've misspelled `elsif`; see [*5.3 If Statements*](http://www.ada-auth.org/standards/12rm/html/RM-5-3.html) – trashgod Sep 22 '16 at 02:12
1

Because "else if" is not correct. Use "elsif".