Jump to content
Developer Wiki and Function Reference Links ×

IF Clause


matteoluigi

Recommended Posts

Quote

IF COUNT((R IN ['Wohnung']) & ('Wohnung'.'Whg-Nr'=NR) & ('Wohnung'.'Zimmertyp'='Wohnen') | ('Wohnung'.'Zimmertyp'='Zimmer') | (('Wohnung'.'Zimmertyp'='Arbeitszimmer'))=1) THEN EINZW := EINZW+1;
IF COUNT((R IN ['Wohnung']) & ('Wohnung'.'Whg-Nr'=NR) & ('Wohnung'.'Zimmertyp'='Wohnen') | ('Wohnung'.'Zimmertyp'='Zimmer') | (('Wohnung'.'Zimmertyp'='Arbeitszimmer'))=1) THEN ZWEIZW := ZWEIZW+1;
IF COUNT((R IN ['Wohnung']) & ('Wohnung'.'Whg-Nr'=NR) & ('Wohnung'.'Zimmertyp'='Wohnen') | ('Wohnung'.'Zimmertyp'='Zimmer') | (('Wohnung'.'Zimmertyp'='Arbeitszimmer'))=1) THEN DREIZW := DREIZW+1;
IF COUNT((R IN ['Wohnung']) & ('Wohnung'.'Whg-Nr'=NR) & ('Wohnung'.'Zimmertyp'='Wohnen') | ('Wohnung'.'Zimmertyp'='Zimmer') | (('Wohnung'.'Zimmertyp'='Arbeitszimmer'))=1) THEN VIERZW := VIERZW+1;
IF COUNT((R IN ['Wohnung']) & ('Wohnung'.'Whg-Nr'=NR) & ('Wohnung'.'Zimmertyp'='Wohnen') | ('Wohnung'.'Zimmertyp'='Zimmer') | (('Wohnung'.'Zimmertyp'='Arbeitszimmer'))=1) THEN FUENFZW := FUENFZW+1

 

however doesn't work

Link to comment

Instead of trying to get 6 to work at once, get one working to get the format then expand.

 

The basic format is

 

 IF(some condition) THEN Do Something Else Do Different Thing;


I often include Begin and End inside an If function even if I only have a single line of code in the thing I want to do so that if I need to add more code later the structure is already there.

 

IF(Some Condition) Then
  Begin
    Do Something;
  End
ELSE
  Being
    Do a Different Thing;
  End;

Note that there is no semicolon after the end before the ELSE. If you are using a single line you must not have a semicolon before the ELSE either.

 

IF COUNT((R IN ['Wohnung']) & ('Wohnung'.'Whg-Nr'=NR) & ('Wohnung'.'Zimmertyp'='Wohnen') | ('Wohnung'.'Zimmertyp'='Zimmer') | (('Wohnung'.'Zimmertyp'='Arbeitszimmer'))=1) THEN EINZW := EINZW+1;

 

So this looks to be fine syntax as long as you don't need an Else.

 

Are you sure that your conditions are being met? Try simplifying it and see if you can get EINZW to increment.  Maybe just count the objects that have a Zimmertyp='Zimmer'

 

The source for general syntax for Vectorscript is the Vectorscript Language Guide. It can be downloaded from this page of the Developer web site.

 

https://developer.vectorworks.net/index.php/VectorScript

  • Like 1
Link to comment

@matteoluigi ,

   There is a problem with your syntax of the 6 IF statements. Where you have  "  ))=1)  "  you should have  "  ))) =1  "

 

Your last parenthesis should be to the left of the "=". The syntax is essentially   " COUNT ( ... ) = aNumber ".

 

 

28 minutes ago, matteoluigi said:

does there also exist sth like else if?

 

Yes you can have statements lilke:

 

if ( ) then begin end

else if ( ) begin end

else if ( ) begin end

else begin end;

 

Notice there is only 1 semicolon at the end, and not after each else if clause.

 

HTH,

Raymond

Link to comment

@MullinRJ is correct as usual.

 

The caveat I would make is that it is not an ELSE IF. It is an ELSE that uses and IF as the if condition. Subtle difference but important.

 

There are languages which specifically have ELSEIF syntax. Vectorscript (and Pascal on which it is based) don't.

 

Nested IFs can be very useful, but also very complicated to debug. Be careful with your parentheses, Begin/End statements, and indentation.

 

I always use double indentation, one set for the nesting of a Begin/End pair and then the code inside the begin/end is indented again. That makes it easier for me to read the code.

 

 

  • Like 1
Link to comment

yeah, thanks, I got it all running, now 🙂

 

Now I would like, to write my results in a worksheet.

 

Still working on it (and maybe thinking about running the script within a worksheet)

 

{In this script, I count

a) how many objects are connected with the database "Wohnungsnummer" (number of flat/apartment)
b) in a for to loop i do loop through all apartments (in fact the loop runs as often as many rooms are in the building, not as many apartments, but, that's not my main priority)

- every loop the script counts the number of rooms which are living rooms, rooms or workrooms (it's the way you define flats in germany, a 3 room flat for example consists of a kitchen, a bathroom, a corridor, a living room and two sleeping rooms, you only count the living room and both sleeping rooms)}

PROCEDURE zaehlen;

VAR
NR:			INTEGER;	{counter for the running loop}
ANZAHL:		INTEGER;	{number of all rooms}
ZANZAHL:	INTEGER;	{number of rooms of the actually checked apartment}
EINZW:		INTEGER;	{number of all 1 room-appartments}
ZWEIZW:		INTEGER;	{number of all 2 room-appartments}
DREIZW:		INTEGER;	{number of all 3 room-appartments}
VIERZW:		INTEGER;	{number of all 4 room-appartments}
FUENFZW:	INTEGER;	{number of all 5 room-appartments}

BEGIN
ANZAHL := COUNT(((('Wohnung'.'Zimmertyp'='Zimmer')|('Wohnung'.'Zimmertyp'='Wohnen')|('Wohnung'.'Zimmertyp'='Arbeitszimmer'))));

{EINZW :=0;
ZWEIZW :=0;
DREIZW :=0;
VIERZW :=0;
FUENFZW :=0;}

FOR NR:=1 TO ANZAHL DO BEGIN	{Schleife, die alle Wohnungen "durchläuft" }

ZANZAHL := COUNT(((('Wohnung'.'Whg-Nr'=NR) & (('Wohnung'.'Zimmertyp'='Zimmer') | ('Wohnung'.'Zimmertyp'='Arbeitszimmer') | ('Wohnung'.'Zimmertyp'='Wohnen')))));
	{Zählt, wieviele Zimmerin der Wohnung sind mit der Wohnungsnummer NR.}

IF (ZAnzahl=1) 	{erhöht den Einzimmerwohnungs-Zähler um 1, wenn die Whg ein Zimmer hat.}
THEN
BEGIN
EINZW := EINZW+1;
END;

IF (ZAnzahl=2)		{erhöht den Zweizimmerwohnungs-Zähler um 1, wenn die Whg ein Zimmer hat.}
THEN
BEGIN
ZWEIZW := ZWEIZW+1;
END;


IF (ZAnzahl=3)		{erhöht den Dreizimmerwohnungs-Zähler um 1, wenn die Whg ein Zimmer hat.}
THEN
BEGIN
DREIZW := DREIZW+1;
END;

IF (ZAnzahl=4)		{erhöht den Vierzimmerwohnungs-Zähler um 1, wenn die Whg ein Zimmer hat.}
THEN
BEGIN
VIERZW := VIERZW+1;
END;

IF (ZAnzahl=5)		{erhöht den Fünfzimmerwohnungs-Zähler um 1, wenn die Whg ein Zimmer hat.}
THEN
BEGIN
FUENFZW := FUENFZW+1;
END;

END;


CreateText(Concat('Da sind ', EINZW, ' Einzimmerwohnungen, ', ZWEIZW, ' Zweizimmerwohnungen, ', DREIZW, ' Dreizimmerwohnungen ', VIERZW, ' Vierzimmerwohnungen und ', FUENFZW, ' Fünfzimmerwohnungen.'));


AlrtDialog(Concat('Da sind ', EINZW, ' Einzimmerwohnungen, ', ZWEIZW, ' Zweizimmerwohnungen, ', DREIZW, ' Dreizimmerwohnungen ', VIERZW, ' Vierzimmerwohnungen und ', FUENFZW, ' Fünfzimmerwohnungen.'));
		{Gibt aus, wieviele Wohnungen welchen Typs im Haus sind.}
END;
RUN(zaehlen);

 

  • Like 1
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...