If … Then ElseIf …

I have troubles withe the code below:

[code] // Day additions (use the RadioButton selection)
If RB_Kind_Daily.Value And Date_Run.DayOfWeek <> 1 Then
// Daily Strips only [No Sunday]
Date_Run.Day = Date_Run.Day + 1

  ElseIf RB_Kind_Sunday.Value Then
    // Sunday Strips only
    Date_Run.Day = Date_Run.Day + 7
    
  ElseIf RB_Kind_All.Value Then
    // Daily and Sunday Strips [7 days a week]
    Date_Run.Day = Date_Run.Day + 1
    
  Else
    // To avoid infinite loop (why Lord, why ?)
    Date_Run.Day = Date_Run.Day + 1
  End If[/code]

When I add the Else statement, the code runs correctly (Because in the Else I add 1 day and this is OK in that specific case: pure hazard).

RB_Kind are three RadioButtons. In the case above, the RB_Kind_Daily.Value is True.
The code is in a Do …/… Loop Until dates compares

Nota: there are 3 RadioButtons because there are three Cases: Monday-Saturday, Sundays and Monday -Sunday. That code was working until today. That is why I worked on it today.

Yes, I cleared the Xojo Cache.

Also, I add a log in a Listbox that report the important values: without the Else block, the loop is infinite :frowning:

The else is catching:

daily=true and DayOfWeek=1

when you end up on that you never move Date_Run.Day + 1 so you end up stuck in the loop

Julian already answered, I’m just testing and learning.

So your options are:
a) RB_Kind_All, always add 1 day
b) RB_Kind_Sunday, add 7 days for DayOfWeek = 1
c) RB_Kind_Daily, add 1 day except Sunday when you add 2

You have:
a) if it is Sunday, it could be 7 or 2 days to add (_Sunday or _Daily)
b) any other day you add 1 day

I think this code will work:

// Day additions (use the RadioButton selection) If Date_Run.DayOfWeek = 1 Then If RB_Kind_Sunday.Value Then Date_Run.Day = Date_Run.Day + 7 Elseif RB_Kind_Daily.Value Then Date_Run.Day = Date_Run.Day + 2 End If Else Date_Run.Day = Date_Run.Day + 1 End If

Edit: I think my code is different, you are not adding 2 to RB_Kind_Daily on Sunday. I think you code will add 1 if RB_Kind_Daily is selected and DayOfWeek <> 1, but it also will add 1 if RB_Kind_Daily is selected and DayOfWeek = 1 via Else, making it the same as RB_Kind_All.

Emile, I suggest you restructure the code a bit to help you think more clearly about it.

   // Day additions (use the RadioButton selection)
   If RB_Kind_Daily.Value Then
      If Date_Run.DayOfWeek <> 1 Then
         // Daily Strips only [No Sunday]
         Date_Run.Day = Date_Run.Day + 1
      Else
          // It's Sunday, what should I do???
      End If

   ElseIf RB_Kind_Sunday.Value Then
      // Sunday Strips only
      Date_Run.Day = Date_Run.Day + 7
        
   ElseIf RB_Kind_All.Value Then
      // Daily and Sunday Strips [7 days a week]
      Date_Run.Day = Date_Run.Day + 1
        
   Else
      // None of the RadioButtons are selected???
   End If

As Julian points out, if your loop depends on Date_Run being incremented, then you really only have 2 conditions if you want to avoid an infinite loop:

  1. RB_Kind_Sunday, add 7
  2. Everything else, add 1

Give some thought to how your loop should function, there may be a better way.

Thank you all for your answers.

No. There are two cases here: the condition includes Sundays OR excludes Sundays (rare but existant).

Julian: I am usure if I really understand what you wrote. I added the Else case (unneeded) just to be sure one day inc is done / the loop exits somewhere… Having a value of 60K or more after cmd-. is impossible. In my testings, I often use cases where I know the results before trying in unknow results.
THe real code does not had the Else statement.

Also, I may not be clear, bu the Else in my code was a despair attempt to make the code running (or better: to know why the Day is not incremented…)

This is a simple case with three possible solutions, but I have a blockage with it. I will try with Mon-Sat and Sunday only (two tests) to know what happens.

I make a new test and I was creative (!)

The RadioButton Value set to True is RB_Kind_Daily (the Else in the code below)

[code] If RB_Kind_Sunday.Value Then
// Sunday Strips only
Date_Run.Day = Date_Run.Day + 7

  Else
    // Daily Strips only
    Date_Run.Day = Date_Run.Day + 1
  End If[/code]

If the current day is not a Sunday I add 1 day to Date_Run.Day, else I add 7 days to Date_Run.Day…

This failed too !

FWIW, here’s the used data:

Matrix Story Start End Length 0 2016-01-01 2016-01-12 12 1 2017-01-01 2017-01-20 375 2 2018-01-01 2018-01-31 377

Obviously, there cannot be 12 days (in Row 0) between January 1st and January 12th, same apply for the two other Rows ! :frowning:

Doh ! I forgot to follow Tim’s advice. I will do that in some minutes after the current download is over.

Sometimes the loop does not exits !

Now the loop adds 1 day two times, then falls in a Sunday and continue that way not following the RadioButton.Value…

I give up for… sometimes until a good idea comes to mind !
(or a Muse tell something to my ear…)

This may be the whole method design who is wrong.

A “simple” day counter between two dates !

  Date_Run.Day=Date_Run+IF(RB_Kind_Sunday.value,7,1)

a whole lot less code to do the exact same thing

Dave, thank you, I didn’t know you can use an If statement like that. Definitely I learn a lot just by reading the forum.

Emile, maybe you can share a sample project with a little more information and not just the if…then elseif…, I think with that someone can tell you what is happening.

Don’t mind the code below. I wrote this before I completely read your last post: “Sometimes the loop does not exits !”. Just post a sample with the loop and I’m sure someone will help you.

Your original code check:
1.- If RB_Kind_Daily AND DayOfWeek <> 1, Date_Run.Day + 1
I think this will only happen when the radio button is in Daily and DayOfWeek <> 1 (2-7)
2.- if RB_Kind_Sunday, Date_Run.Day + 7
I think this will happen any day just for the radio button set to Sunday, even if DayOfWeek is 2-7
3.- if RB_Kind_All, Date_Run.Day+1
I think this will happen for every DayOfWeek

You had to add the Else to catch RB_Kind_Daily AND DayOfWeek = 1. Without it you will end in a loop that will no add 1 to Date_Run.Day.

Because you added the Else statement, now your If…Then ElseIf statement works just like:

If RB_Kind_Sunday.Value Then // This executes even if Date_Run.DayOfWeek <> 1 Date_Run.Day = Date_Run.Day + 7 Else Date_Run.Day = Date_Run.Day + 1 End If

Maybe someone with more experience can check this.

Dave. How does that “+” work?
Is it usable on only single IF THen Else statement?

  • is used to Add two values together… nothing magic there
    did you read about IF()?

result = If(expression, resultWhenTrue, resultWhenFalse)

I do not understand that. I will read it once more later, maybe…

I found two errors in my if blocks:

a. An Else is mandatory: when the day is a Sunday, the code do not add 1 day to go to the following Monday; that is why I get an infinite loop !

b. The ellapsed number of days addition have to be in the If block, else I get a +1 in each do Loop iteration…

The program seems to work now (at least for Dailies: I checked with a large file who already have the calculations in a column…).

BTW: the used If block had only two tests (Daily without Sunday and Sunday: I have to add the third one…)

More testings, but I think I get it.

FWIW: I totally rewrite from scratch the method (who compute a number of days between two dates with or without Sundays…)

Ok… let me see if I can explain a simple addition

Note : I did leave “.day” out by accident…

Date_Run.Day  =  Date_Run.day   +    IF(RB_Kind_Sunday.value,7,1)
  • Date_Run.Day on the LEFT of the EQUAL SIGN is the variable that will be affected by the equation on the RIGHT of the EQUAL
  • The Equation on the RIGHT of the EQUAL takes TWO arguments and ADDS (+) them together
  • The first argument is Date_Run.Day which is the value you wish to increase (ie. ADD to)
  • The Second argument is IF(RB_Kind_Sunday.value,7,1) which returns the value of 7 or 1 depending on if RB_KInd_Sunday.value is true or fale

Failing to see where the issue here is… a simple read of the Language Ref would have explained it just as well

I saw this come out in release notes or the XDEV magazine. The explanation for the If Operator (If Operator)does show

None of them use “+” so how do you do that?

How or where is this documented?

I am glad I saw this because I didn’t think I could get this on one line.

@Dave: I think I understand now. Thank you.

Where Dave says… Just have to type if in developer.xojo… see below

http://developer.xojo.com/if

Arthur… what on EARTH on you talking about?

  • is the every day normal ADDITION OPERATOR
x=3+2

in this case we replace 2 with Date_Run.Day
and we replace 3 with IF(RB_Kind_Sunday.value,7,1)

You get 7 if RB_Kind_Sunday.value is True, else 1.

Ahh. Adding the results together. Now I get it. I had never considered that uhmm addition of two statements.

Oh Yeah. I do that but not with adding on an IF statement.

Neat.

Grumble. Now I have more code to edit.

When result = If(expression, resultWhenTrue, resultWhenFalse) was added ?
I searched from 2015r2 (it does not exists in 2015r1) in the New Items parts of the Release Notes and found nothing.

Also, this information does not appears in the LR entry.

So, in what Xojo version result = If(expression, resultWhenTrue, resultWhenFalse) appears ?

Xojo 2014 Release 1

https://blog.xojo.com/2014/03/12/using-the-new-if-operator/