Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Break-statement in TP 5.5

Posted on 2003-10-27
4
243 Views
Last Modified: 2010-04-16
Hi experts
i'm using TB 5.5 (free pascal)
the problem is;
i need to write a function who compares two arrays of 4 shortint to see if 2 numbers are the same
take for example those 2 arrays
1 2 3 4
2 2 2 8

i wrote this code

FUNCTION Case1:Boolean;
Var Temp, i, j:ShortInt;
Begin
Temp:=0;
FOR i:=1 TO 4 DO
 FOR j:=1 TO 4 DO
  IF Digit[i]=Guess[j] THEN
   Temp:=Temp+1;
IF Temp>=2 THEN Case1:=TRUE;
IF Temp<2 THEN Case1:=FALSE;
End;

the problem is that because there are in the second array more than one '2' the output = TRUE but that shouldn't be
because there is only 1 number in the two arrays who are the same

i thaught i could use the BREAK-statement like this

FUNCTION Case1:Boolean;
Var Temp, i, j:ShortInt;
Begin
Temp:=0;
FOR i:=1 TO 4 DO
 FOR j:=1 TO 4 DO
  IF Digit[i]=Guess[j] THEN
   begin
   Temp:=Temp+1;
   BREAK;
   end;
IF Temp>=2 THEN Case1:=TRUE;
IF Temp<2 THEN Case1:=FALSE;
End;

from an algoritmic point of view that should be good I THINK
but the BREAK statement (which is used to abort the actual loop) doesn't work in TP 5.5
could someone help me out ???
 thanks in advance

Elvis
0
Comment
Question by:Elvis1
  • 3
4 Comments
 

Author Comment

by:Elvis1
ID: 9628417
or maybe you can just give me a way to go out or to skip the current loop
an alternative to the BREAK statement

Elvis
0
 

Accepted Solution

by:
Elvis1 earned 0 total points
ID: 9628474
SORRY EVERYONE
i found the answe myself
to "break" a while loop i did

FOR i:=1 TO 10 DO
 Begin
 WriteLn(i);
 IF i=5 THEN i:=10;
 End;

The Output is
1
2
3
4
5

sorry
Elvis
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9628691
Doesn't matter. However, personally I consider the use of the Break statement something that you should avoid at all costs. Like the Goto statement and some other statements the Break statement makes the source a bit less readable.
Instead of a for-next loop you could also use a While-Do loop like this:

var
  I:Integer;
  EndOfLoop: Boolean;
begin
  I := 0;
  EndOfLoop := False;
  while (I < 10) and not EndOfLoop do begin
    I := I + 1;
    WriteLn(I);
    if (I=5) then begin
      EndOfLoop := True;
    end else begin
      // Perhaps do some other code.
    end;
  end;
end;
0
 

Author Comment

by:Elvis1
ID: 9629893
thanks i will remember
but now i got it
thanks
Elvis
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Twsocket as Twsocketserver 6 1,649
Delphi XE, DataSnap, JSon and java 8 1,065
ROUND() from fbudf.dll only gives integers 2 1,059
How to use WebDispatcher to retrieve data 3 402
ConnectWise and their customers need to ensure critical alerts automatically reach the right person at the right time. MSP superheros efficiently respond to these alerts key is providing automatic, intelligent alerting that generates a complete audi…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question