4
votes

I was trying to translate the following Pascal code to C++, when I stumbled upon the "else else" construction in question. I've never seen this before, so could anybody tell me what it does and what are it's C++ (or maybe C) equivalents?

  Procedure Force(Q:Int64;V,K:Integer);
   Var i,j,t:Integer;
    begin
     if K<=0 then
      if (Q>=A)and(Q Mod KK =0)and(V>=S)and(V<=F)then Out:=Out+1 else else
       For i:=0 to 9 do
        if (Q+(i+1)*h[k-1]>=A)and(Q+i*h[k-1]<=B) then
         if (Q+(i+1)*h[K-1]<B)and(Q+i*h[k-1]>=A) then
          Begin
           M:=(Q+i*h[k-1]) Mod KK;
           For j:=0 to 9*(K-1) do
            For t:=0 to KK-1 do
             if D[K-1,j,t]>0 then
              if (V+i+j>=S)and(V+i+j<=F)and((t+M) Mod KK=0) then
                 Out:=Out+D[K-1,j,t];
           end else
            if Odd(N-K+1) then Force(Q+i*h[k-1],V+i,K-1) else
                               Force(Q+i*h[k-1],V+i,K-1);
    end;
3
It's an empty else statement, see the last snippet here. - emlai
Line up each else with an if you will see there is no magic. - Martin York
I see, thank you very much. I can't believe I overlooked something like that, though. - Дмитрий Румянцев

3 Answers

3
votes

I've just copied to an editor (for instance Komodo, where you can select Pascal as language for syntax color highlighting) and reformatted the text you've wrote in a way I can read it myself.

procedure Force(Q:Int64;V,K:Integer);
var 
  i,j,t:Integer;
begin
  if K<=0 then
    if (Q>=A) and (Q Mod KK =0) and (V>=S) and (V<=F) then
      Out:=Out+1
    else
  else
    for i:=0 to 9 do begin
      if (Q+(i+1)*h[k-1]>=A) and (Q+i*h[k-1] <= B) then
        if (Q+(i+1)*h[K-1]<B) and (Q+i*h[k-1] >= A) then begin
          M := (Q+i*h[k-1]) Mod KK;
          for j:=0 to 9*(K-1) do begin
            for t:=0 to KK-1 do begin
              if D[K-1,j,t] > 0 then
                if (V+i+j >= S) and (V+i+j <= F) and ((t+M) mod KK = 0) then
                  Out:=Out+D[K-1,j,t];
            end; {for t}
          end; {for j}
        end else
          if Odd(N-K+1) then
            Force(Q+i*h[k-1],V+i,K-1)
          else
            Force(Q+i*h[k-1],V+i,K-1);
      end;
    end;
end;

Don't you think it is more understandable now?

1
votes

It's quite often useful to use begin and end pairs even when they're not required by the syntax, just to make the code more readable and understandable. (Think of begin being the equivalent of { and end being the equivalent of }; while you can write for(int i = 0; i < 10; i++) SomeCode();, it's usually more clear to use for(int i = 0; i < 10; i++) { SomeCode(); }.

So the code you've posted, with begin and end pairs added where appropriate, a no-op else or two removed, and more appropriate formatting seems much more readable to me.

Procedure Force(Q: Int64; V, K: Integer);
Var
  i, j, t: Integer;
begin
  if K <= 0 then
  begin
    if (Q >= A) and (Q Mod KK = 0) and (V >= S) and (V <= F) then
      Out := Out + 1;
  end
  else
  begin
    For i := 0 to 9 do
    begin
      if (Q + (i + 1) * h[K - 1] >= A) and (Q + i * h[K - 1] <= B) then
      begin
        if (Q + (i + 1) * h[K - 1] < B) and (Q + i * h[K - 1] >= A) then
        begin
          M := (Q + i * h[K - 1]) Mod KK;
          For j := 0 to 9 * (K - 1) do
          begin
            For t := 0 to KK - 1 do
            begin
              if D[K - 1, j, t] > 0 then
              begin
                if (V + i + j >= S) and (V + i + j <= F) and
                   ((t + M) Mod KK = 0) then
                  Out := Out + D[K - 1, j, t];
              end;
            end;
          end;
        end
        else if Odd(N - K + 1) then
          Force(Q + i * h[K - 1], V + i, K - 1)
        else
          Force(Q + i * h[K - 1], V + i, K - 1);
      end;
    end;
  end;
end;
0
votes

That is some horrible indentation. If we indent it better we can see what's going on:

if K<=0 then
    if (Q>=A)and(Q Mod KK =0)and(V>=S)and(V<=F) then 
        Out:=Out+1 
    else 
else
   For i:=0 to 9 do

Here, the first else applies to the if (Q>=A), but it's empty.