What is wrong with this if/else statement  [SOLVED]

Questions about MultiCharts and user contributed studies.
rkhan
Posts: 65
Joined: 16 Mar 2014
Has thanked: 1 time
Been thanked: 2 times

What is wrong with this if/else statement

Postby rkhan » 14 May 2014

I'm trying to code a simple if/else statement, i've coded it as below, and i get an error expecting end

But this is exactly how the example had it, so what could be wrong with the code below

Code: Select all

if high > high[1] then
isUpCloseDay = true;
Else
isUpCloseDay = false;
syntax error, unexpected 'else', expecting 'End'

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: What is wrong with this if/else statement  [SOLVED]

Postby TJ » 14 May 2014

try this:

Code: Select all

if high > high[1] then
isUpCloseDay = true
Else
isUpCloseDay = false;

User avatar
furytrader
Posts: 354
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 217 times

Re: What is wrong with this if/else statement

Postby furytrader » 15 May 2014

Where to put the semi-colon is the most confusing part of if / then / else!

In some languages, the different conditional branches are separate statements whereas here, they are really just one long statement.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: What is wrong with this if/else statement

Postby JoshM » 18 May 2014

Where to put the semi-colon is the most confusing part of if / then / else!
I find it intuitive:
  • Every statement ends with the semicolon;
  • The if-else statement is a single statement;
  • So it has no semicolon in it.
In some languages, the different conditional branches are separate statements whereas here, they are really just one long statement.
That depends I think on how someone views the `begin` and `end` statements.

For example, the if-else statement in PowerLanguage:

Code: Select all

if (High > High[1]) then
upCloseday = true
else
upCloseDay = false;
The equivalent in C#:

Code: Select all

if (High > High[1])
upCloseday = true
else
upCloseDay = false;
(Same semicolon behaviour here, and both are just one long statement)

Now the if-else-begin-end statement in PowerLanguage:

Code: Select all

if (High > High[1]) then begin
upCloseday = true;
end
else begin
upCloseDay = false;
end;
Or in C#:

Code: Select all

if (High > High[1])
{
upCloseday = true;
}
else
{
upCloseDay = false;
}
So:
  • The `begin` and `end` reserved words are the equivalent of the opening and closing braces in other programming languages (C# here);
  • Other programming languages (like C#) don't require a semicolon when the opening and closing braces are missing;
  • So from the standpoint of other programming languages it makes sense that the semicolon is not required with the if-else statement (which is a single statement) but is required with the if-else-begin-end statement (which are a block of statements).

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: What is wrong with this if/else statement

Postby bowlesj3 » 19 May 2014

Of the 11 languages I have worked with VBA is my favorite. I like the fact that there is no need for the semi-colon and the "End If" (and similar ending statements for the while statement and the until statement) line up nicely with the IF statement and the Else of course. All the compiler needs to make this a perfect world - LOL- is specialized tab function to jump from the "If" to the "Else" to the "End IF" and back to the "If" statement so the user could figure out the related components in huge if statements rather than having to do the tedious scroll down maneuver trying to make sure one does not loose the line-up of the components (see the trick below to work around this problem). It is when one creates those monster if statements that one starts to realize that a comment system is good to use to mark the related components.

VBA code sample:

Code: Select all

If strWhatever = "Y" Then
value1 = 1
Else
value1 = 2
End If
I have a very large number of these monster if statements in VBA and I often use this technique to move around them (they can be handy when copying similar code to another script too). You just have to remember to keep the comments up to date to match the if but even if you forget the number is there to ensure you can find the related "End If" quickly. It can be a big debugging time saver (as well as saving time when you want to go back and modify code). I use this in VBA and in power language. I just realized that my statement about the compiler above would not likely work if there was a missing component somewhere. So the number can also be handy when you accidentally are missing a component and the compiler goes a bit crazy as a result (especially if they are nested 5 or 6 deep).
viewtopic.php?f=5&t=7142


Return to “MultiCharts”