Pascal 中的双 'else' 语句

Double 'else' statement in pascal

当我偶然发现有问题的 "else else" 构造时,我正试图将以下 Pascal 代码翻译成 C++。我以前从未见过这个,所以有人能告诉我它的作用以及它的 C++(或 C)等价物是什么吗?

  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;

那是一些可怕的缩进。如果我们更好地缩进它,我们可以看到发生了什么:

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

在这里,第一个 else 适用于 if (Q>=A),但它是空的。

我刚刚复制到一个编辑器(例如 Komodo,在那里你可以 select Pascal 作为语法颜色突出显示的语言)并以我自己可以阅读的方式重新格式化你写的文本.

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;

你不觉得现在更容易理解了吗?

使用 beginend 对通常很有用,即使语法不需要它们,只是为了使代码更易读和更易理解。 (认为​​ begin 等同于 {end 等同于 };虽然您可以写 for(int i = 0; i < 10; i++) SomeCode();,但通常使用 for(int i = 0; i < 10; i++) { SomeCode(); }.

所以你发布的代码,在适当的地方添加了 beginend 对,删除了一个或两个空操作 else,并且更合适的格式似乎更多对我来说可读。

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;