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
if Visibility < 200 thense evalúa primero, y elelse ifde abajo atrapa el 400.- La rama del medio agrupa dos llamadas a
WriteLn, y ningún punto y coma precede aelse. case Month ofcoincide con12, 1, 2y con el rango3..5antes de suelse.
Palabras clave y builtins usados aquí
IntegerWriteLnbegincaseelseendifofprogramthenvar
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.
Paso 1 de 3 en Control de flujo; paso 7 de 27 en Fundamentos del lenguaje.