case statements

Top  Previous  Next

What is translated > Statements > case statements

The translation of Delphi case statements to C++ switch statements mostly is straightforward like:

 

   Case colour of

        Red : result := 1;

      Green : result := 2;

       Blue : result := 3;

     Yellow : result := 4;

   else result := 0;

   end;

 

->

 

  switch(colour)

  {

    case Red:

    result = 1;

    break;

    case Green:

    result = 2;

    break;

    case Blue:

    result = 3;

    break;

    case Yellow:

    result = 4;

    break;

    default:

    result = 0;

    break;

  }

 

 

In Delphi, not only single constant expressions can be used to define a case, but also lists and subranges of such constants.

Their elements must then be output as separate cases for C++.

 

  Case Key of

    #13, #27:

 

->

 

  switch(Key)

  {

    case L'\x0d':

    case L'\x1b':

 

 

However, if such subranges are very large (more than 256 elements) Delphi2Cpp moves them into the default section of the C++ switch-statement:

 

  Case Key of

    #32..High(WideChar):

      begin

        ...

      end;

    #8:

      ...

   else

     ...

 

->

 

  switch(Key)

  {

    case L'\x08':

    ...

    break;

    default:

    if(Key >= L'\x20' && Key <= 65535 /*# High(WideChar) */)

    {

      ...

    }

    else

    {

      ...

    }

 

 

 

 

 

 

 

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content