Recent

Author Topic: Case Statement?  (Read 3247 times)

RWC

  • Jr. Member
  • **
  • Posts: 92
Case Statement?
« on: October 01, 2016, 03:44:14 pm »
Does anyone know if the Case Statement checks every ‘case of’ entry or does it stop checking when it finds a case that satisfies the argument?
LAZARUS  : Lazarus-1.4.2-fpc-2.6.4-win32. OS   : Windows Vista 32bit Home Premium SP2.
CPU  : Intel Core2 Quad CPU Q6600 2.4GHz. RAM : 3GB. PCIE : NVIDIA GeForce GT610. Audo : NVIDIA HD Audio.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Case Statement?
« Reply #1 on: October 01, 2016, 03:54:10 pm »
Does anyone know if the Case Statement checks every ‘case of’ entry or does it stop checking when it finds a case that satisfies the argument?
Unlike C, it stops checking:
Quote
The compiler will evaluate the case expression. If one of the case constants’ value matches the value of the expression, the statement that follows this constant is executed. After that, the program continues after the final end.

Handoko

  • Hero Member
  • *****
  • Posts: 5153
  • My goal: build my own game engine using Lazarus
Re: Case Statement?
« Reply #2 on: October 01, 2016, 04:02:12 pm »
It has to stop checking once if finds the satisfied case, this is good for performance. If someone needs something similar to case statement but still checking then using multiple ifs is the solution:

Code: Pascal  [Select][+][-]
  1. TestVal := 2;
  2.  
  3. If TestVal = 1 then do Something1;
  4. If TestVal = 2 then do Something2;
  5. If TestVal = 3 then do Something3;
  6. If TestVal = 4 then do Something4;
  7. If TestVal = 5 then do Something5;

The code above is a bad code because it is not optimize for performance.

RWC

  • Jr. Member
  • **
  • Posts: 92
Re: Case Statement?
« Reply #3 on: October 01, 2016, 04:18:04 pm »
@engkin & @Handoko: That’s great news – saves me lots of if..then..else checking in this speed critical procedure – Thank you both for such a speedy reply too – Best regards, RWC.
LAZARUS  : Lazarus-1.4.2-fpc-2.6.4-win32. OS   : Windows Vista 32bit Home Premium SP2.
CPU  : Intel Core2 Quad CPU Q6600 2.4GHz. RAM : 3GB. PCIE : NVIDIA GeForce GT610. Audo : NVIDIA HD Audio.

 

TinyPortal © 2005-2018