Suppose there is a normal case

 case a of 1 : ShowMessage('a=1'); 2 : ShowMessage('a=2'); 3 : ShowMessage('a=3'); 4 : ShowMessage('a=4'); else ShowMessage('no'); end; 

Is it possible to somehow implement this design:

 case a of 1 : ShowMessage('a=1'); b:=10; 2 : ShowMessage('a=2'); b:=20; 3 : ShowMessage('a=3'); b:=30; 4 : ShowMessage('a=4'); b:=40; else ShowMessage('no'); end; 

Here is something similar, since after the fulfillment of the condition a:=1; it is necessary to assign a value to the variable b .


PS Or in what way can you solve the situation here without translating case into if ?

  • 2
    This is the basic syntax: compound statement, it is begin..end . After the colon, put begin , then your code, then end; (with semicolon) - kami

3 answers 3

 case a of 1 : begin ShowMessage('a=1'); b:=10; end; 2 : begin ShowMessage('a=2'); b:=20; end; 3 : begin ShowMessage('a=3'); b:=30; end; 4 : begin ShowMessage('a=4'); b:=40; end; else ShowMessage('no'); end; 
  • corrected, hurried at the beginning - Sublihim
  • Thank! I already understood everything right away with the comment @kami. Only here is some sort of de-optimization. case 2 page case now swollen with SQL queries! - I_CaR
  • de-optimization would be if the code would become slower to execute. And these are just formatting costs, in addition, if there are large sections of begin...end in cases, you can put them into separate functions - Sublihim
  • @I_CaR remove all code from case elements into nested functions. If the SQL query differs only in parameters, you can put it in a constant and use Format. Well, and so on. There are lots of ways to optimize code for viewing, including {$ REGION XXX} - kami
  • Do not forget to set any value for b in the case of processing the else branch ;-) - Kromster

For the above code, case not needed at all.

 if a in [1..4] then begin ShowMessageFmt('a=%d', [a]); b := a * 10; end else ShowMessage('no'); 

    Another possible (and shorter if there is a lot of pairs) option, if you have a type or range of values ​​declared a - use an array of constants:

     type TPresetType = record Text: string; Value: Integer; end; const PRESETS: array [1..4] of TPresetType = ( (Text: '=10'; Value: 10), (Text: '=20'; Value: 20), (Text: '=30'; Value: 30), (Text: '=40'; Value: 40) ); begin if a >= Low(PRESETS) and a <= High(PRESETS) then begin ShowMessage(PRESETS[a].Text); b := PRESETS[a].Value; end else ShowMessage('no'); end; 

    or almost identical, but without a record type, with 2 arrays of constants:

     const PRESET_TEXT: array [1..4] of string = ('=10', '=20', '=30', '=40'); PRESET_VALUE: array [1..4] of Integer = (10, 20, 30, 40); begin if a >= Low(PRESET_TEXT) and a <= High(PRESET_TEXT) then begin ShowMessage(PRESET_TEXT[a]); b := PRESET_VALUE[a]; end else ShowMessage('no'); end; 

    What are the advantages of such a decision - reducing the possibility of a mistake:

    • Enumeration of all options and values ​​in 1 place
    • If you can select the type of the variable a (for example a: TTextAlign; let PRESET_TEXT: array [a] of string write), then adding a new value causes the compiler to force you to add ALL the values.