typestar

if y case en Delphi

Una señal de niebla elige su patrón según la visibilidad, y luego un mes escoge la guardia.

program IfCase;
var
  Visibility: Integer = 400;    // a global may carry an initial value
  Month: Integer = 11;
begin
  if Visibility < 200 then      // the first true test in a ladder wins
    WriteLn('horn: continuous')
  else if Visibility < 1000 then
  begin
    WriteLn('horn: every 30 seconds');
    WriteLn('lamp: full power');  // begin..end groups both statements
  end
  else                           // no semicolon before an else
    WriteLn('horn: silent');
  case Month of
    12, 1, 2: WriteLn('winter watch');
    3..5: WriteLn('spring watch');   // a case label may be a range
  else WriteLn('open season');
  end;
end.

Cómo funciona

  1. if Visibility < 200 then se evalúa primero, y el else if de abajo atrapa el 400.
  2. La rama del medio agrupa dos llamadas a WriteLn, y ningún punto y coma precede a else.
  3. case Month of coincide con 12, 1, 2 y con el rango 3..5 antes de su else.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
20
Caracteres a escribir
595
Tokens
103
Ritmo de tres estrellas
80 tpm

Al ritmo de tres estrellas de 80 tokens por minuto, este intento toma unos 77 segundos.

Escribe este fragmento

Paso 1 de 3 en Control de flujo; paso 7 de 27 en Fundamentos del lenguaje.

← Anterior Siguiente →