if then endif

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am having a dispute at work over endifs, and cannot find any treatise on
this. So I am hoping that I can get a lot of correspondece confirming my
belief.

Here it is: I believe the following: (1)If a language has an "endif", then
every "if" statement requires an endif, and (2) every "endif" goes with the
last "if".

For example,

If .....
if
endif

should not compile because there must be two "endif"s and the last "endif"
goes with the last "if". The actual nested if statement is far more complex,
and it is just being used in a presentation that is not being compiled
(psuedo code).

Finally, were can I find this? I don't care the language, although some
version of Basic would be best since that is what my friend understands.

Thanks for helping
 
lawjake said:
I am having a dispute at work over endifs, and cannot find any treatise on
this. So I am hoping that I can get a lot of correspondece confirming my
belief.

Here it is: I believe the following: (1)If a language has an "endif", then
every "if" statement requires an endif, and (2) every "endif" goes with the
last "if".

For example,

If .....
if
endif

should not compile because there must be two "endif"s and the last "endif"
goes with the last "if". The actual nested if statement is far more complex,
and it is just being used in a presentation that is not being compiled
(psuedo code).

Finally, were can I find this? I don't care the language, although some
version of Basic would be best since that is what my friend understands.

Thanks for helping

If does not always require an End If. For example:

If myCondition = True Then x += 1
 
Well, in VB you can have:

If someCondition Then DoSomething()

There is no endif here. This is a perfectly valid statement
 
I understand that the syntax can be "if ...then", or "if..then...else". What
I need the answer to is if the syntax is "if..then..else...endif", can you
create the code that I described above which uses and endif. Or, is there
code that is "if...then...else...[endif].

Here is the exact logic that I want to know if it is correct (the
indentations are the actual author's:

If HoursWorked < NormalMaxHours,
If HoursWorked > 0.
Then Display PartTimeHoursMessage
End If
Else If HoursWorked = 0,
Then display NoHoursMessage
Else If HoursWorked > NormalMaxHours,
Then display OvertimeMessage
Else
Display RegularTimeMessage
End if


I believe that the final endif goes with the last if?

Thanks
 
http://msdn2.microsoft.com/en-us/library/752y8abs(d=ide).aspx

[i still have some doubt you are mocking us :) ]

-t

lawjake ha scritto:
I understand that the syntax can be "if ...then", or "if..then...else". What
I need the answer to is if the syntax is "if..then..else...endif", can you
create the code that I described above which uses and endif. Or, is there
code that is "if...then...else...[endif].

Here is the exact logic that I want to know if it is correct (the
indentations are the actual author's:

If HoursWorked < NormalMaxHours,
If HoursWorked > 0.
Then Display PartTimeHoursMessage
End If
Else If HoursWorked = 0,
Then display NoHoursMessage
Else If HoursWorked > NormalMaxHours,
Then display OvertimeMessage
Else
Display RegularTimeMessage
End if


I believe that the final endif goes with the last if?

Thanks
Marina Levit said:
Well, in VB you can have:

If someCondition Then DoSomething()

There is no endif here. This is a perfectly valid statement
 
I'm not sure what language this is, but it's not VB.NET. This sort of thing
wouldn't compile.

Honestly, I'm not sure what you are asking. The way VB.NET syntax is
structure, there is never really a question as to what statements are part
of what 'if' or 'else'. Especially if you indent properly, which visual
studio does for you.

If you have the following example, it should be really obvious which
statements will get executed when:

If something Then
If otherCondition Then
CallMethod()
End If
ElseIf x Then
DoSomething()
ElseIf y Then
DoSomethingElse()
ElseIf z Then
DoAnotherThing()
End If


lawjake said:
I understand that the syntax can be "if ...then", or "if..then...else".
What
I need the answer to is if the syntax is "if..then..else...endif", can you
create the code that I described above which uses and endif. Or, is there
code that is "if...then...else...[endif].

Here is the exact logic that I want to know if it is correct (the
indentations are the actual author's:

If HoursWorked < NormalMaxHours,
If HoursWorked > 0.
Then Display PartTimeHoursMessage
End If
Else If HoursWorked = 0,
Then display NoHoursMessage
Else If HoursWorked > NormalMaxHours,
Then display OvertimeMessage
Else
Display RegularTimeMessage
End if


I believe that the final endif goes with the last if?

Thanks
Marina Levit said:
Well, in VB you can have:

If someCondition Then DoSomething()

There is no endif here. This is a perfectly valid statement
 
lawjake said:
I am having a dispute at work over endifs, and cannot find any treatise on
this. So I am hoping that I can get a lot of correspondece confirming my
belief.

Here it is: I believe the following: (1)If a language has an "endif",
then
every "if" statement requires an endif, and (2) every "endif" goes with
the
last "if".

Not really. Every 'If...Then' block needs an 'End If', but VB also provides
a single-line 'If...Then...' statement.
 
I know it sounds too basic to be a question, but after programming for over
30 years, somethings I know, but don't know where the documentation is
anymore. I have not had to look this up in almost 30 years and did not have
any books with the actual syntax. So I appreciate your help. Of course, I
would have betting things to do than mock my job.

Thanks again.

http://msdn2.microsoft.com/en-us/library/752y8abs(d=ide).aspx

[i still have some doubt you are mocking us :) ]

-t

lawjake ha scritto:
I understand that the syntax can be "if ...then", or "if..then...else". What
I need the answer to is if the syntax is "if..then..else...endif", can you
create the code that I described above which uses and endif. Or, is there
code that is "if...then...else...[endif].

Here is the exact logic that I want to know if it is correct (the
indentations are the actual author's:

If HoursWorked < NormalMaxHours,
If HoursWorked > 0.
Then Display PartTimeHoursMessage
End If
Else If HoursWorked = 0,
Then display NoHoursMessage
Else If HoursWorked > NormalMaxHours,
Then display OvertimeMessage
Else
Display RegularTimeMessage
End if


I believe that the final endif goes with the last if?

Thanks
Marina Levit said:
Well, in VB you can have:

If someCondition Then DoSomething()

There is no endif here. This is a perfectly valid statement

I am having a dispute at work over endifs, and cannot find any treatise on
this. So I am hoping that I can get a lot of correspondece confirming my
belief.

Here it is: I believe the following: (1)If a language has an "endif",
then
every "if" statement requires an endif, and (2) every "endif" goes with
the
last "if".

For example,

If .....
if
endif

should not compile because there must be two "endif"s and the last "endif"
goes with the last "if". The actual nested if statement is far more
complex,
and it is just being used in a presentation that is not being compiled
(psuedo code).

Finally, were can I find this? I don't care the language, although some
version of Basic would be best since that is what my friend understands.

Thanks for helping
 
lawjake said:
Here is the exact logic that I want to know if it is correct (the
indentations are the actual author's:

If HoursWorked < NormalMaxHours,
If HoursWorked > 0.
Then Display PartTimeHoursMessage
End If
Else If HoursWorked = 0,
Then display NoHoursMessage
Else If HoursWorked > NormalMaxHours,
Then display OvertimeMessage
Else
Display RegularTimeMessage
End if

This code will not compile in any version of VB that I am familiar with.
The ','s and '.'s on the ends of lines and the total lack of 'Then's
render it invalid.

If every you're in doubt, expand every instance of If .. End If /in
full/ with associated indentation.

I /think/ this is something close to what you want:

If HoursWorked < NormalMaxHours Then
If HoursWorked > 0 Then
Display PartTimeHoursMessage
End If

ElseIf HoursWorked = 0 Then
Display NoHoursMessage

ElseIf HoursWorked > NormalMaxHours Then
Display OvertimeMessage

Else
Display RegularTimeMessage

End If

HTH,
Phill W.
 
lawjake wrote:
Here it is: I believe the following: (1)If a language
has an "endif", then every "if" statement requires an endif,
and (2) every "endif" goes with the last "if".
<snip>

And then added:
I understand that the syntax can be "if ...then", or "if..then...else". What
I need the answer to is if the syntax is "if..then..else...endif", can you
create the code that I described above which uses and endif. Or, is there
code that is "if...then...else...[endif].
<snip>

If the language has an "ElseIf" construct, as most languages do, then
you may have several ElseIf's which are part of the original If, and
all will be closed by a single EndIf:

If Condition Then
Statements
ElseIf Condition Then
Statements
Else
Statements
End If

On the other side, if there's no "ElseIf" construct, or the If is
stated as a new statement, then yes, each if will need its own EndIf,
and each EndIf will close the most recent unclosed If:

If Condition1 Then
StateMent
Else
If Condition2 Then
Statement
Else
If Condition3 Then
Statement
End If 'Condition3
End If 'Condition2
End If 'Condition1

Now, notice that a language, which is the case of VB, may allow single
line If's, and then you may have:

If Condition Then
If Condition Then Statement
End If

where the EndIf closes the first If.

Regards,

Branco.
 
lawjake said:
I understand that the syntax can be "if ...then", or "if..then...else". What
I need the answer to is if the syntax is "if..then..else...endif", can you
create the code that I described above which uses and endif. Or, is there
code that is "if...then...else...[endif].

Here is the exact logic that I want to know if it is correct (the
indentations are the actual author's:

If HoursWorked < NormalMaxHours,
If HoursWorked > 0.
Then Display PartTimeHoursMessage
End If
Else If HoursWorked = 0,
Then display NoHoursMessage
Else If HoursWorked > NormalMaxHours,
Then display OvertimeMessage
Else
Display RegularTimeMessage
End if


I believe that the final endif goes with the last if?

I think you realize by now that there is a big difference between:

If ConditionA Then
DoSomething
Else
If ConditionB Then
DoSomethingElse
Else
If ConditionC Then
DoSomethingElseDifferent
End If
End If
End If

AND:

If ConditionA Then
DoSomething
ElseIf ConditionB Then
DoSomethingElse
ElseIf ConditionC Then
DoSomethingElseDifferent
End If

ElseIf as opposed to Else makes the structure simpler, but is more
restrictive on what you can do in the general Else condition path.
 
Thanks everyone for your help. Not only was your input very informative, but
incredibly quick. I now have the evidence to show my friend.
 
The confusion comes from C. In C, the following is perfectly valid:

if (condition1) {
block1
} else if (condition2) {
block 2
} else {
elseblock
}

Note the elseif is actually two words - else and then if. There is only a
single closing brace for the overall if statement. In VB this would be

if condition1 then
block1
elseif condition2 then
block2
else
elseblock
end if

In variants of Basic without elseif (VMS BASIC for example), this would be

if condition1 then
block1
else
if condition2 then
block2
else
elseblock
end if
end if

Note the two endifs at the end of the if statement.

Mike Ober.


Branco Medeiros said:
lawjake wrote:
Here it is: I believe the following: (1)If a language
has an "endif", then every "if" statement requires an endif,
and (2) every "endif" goes with the last "if".
<snip>

And then added:
I understand that the syntax can be "if ...then", or "if..then...else".
What
I need the answer to is if the syntax is "if..then..else...endif", can
you
create the code that I described above which uses and endif. Or, is
there
code that is "if...then...else...[endif].
<snip>

If the language has an "ElseIf" construct, as most languages do, then
you may have several ElseIf's which are part of the original If, and
all will be closed by a single EndIf:

If Condition Then
Statements
ElseIf Condition Then
Statements
Else
Statements
End If

On the other side, if there's no "ElseIf" construct, or the If is
stated as a new statement, then yes, each if will need its own EndIf,
and each EndIf will close the most recent unclosed If:

If Condition1 Then
StateMent
Else
If Condition2 Then
Statement
Else
If Condition3 Then
Statement
End If 'Condition3
End If 'Condition2
End If 'Condition1

Now, notice that a language, which is the case of VB, may allow single
line If's, and then you may have:

If Condition Then
If Condition Then Statement
End If

where the EndIf closes the first If.

Regards,

Branco.
 
In addition to the other comments:

| Here it is: I believe the following: (1)If a language has an "endif",
then
| every "if" statement requires an endif, and
False

| (2) every "endif" goes with the
| last "if".
False

Consider the following:

If x < y Then
If y < z Then Debug.WriteLine("Hello World")
End If

The first If is a multi-line if, it requires an endif. The second If is a
single line If, no endif allowed. You can tell its a multiline If by the
fact there is nothing after the Then statement, likewise you can tell its a
single line If by the fact there *is* something after the Then statement.

The single line If is inside a multiline If, the End If completes the
multiline If, as the single line If is on (you guessed it) a single line!

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|I am having a dispute at work over endifs, and cannot find any treatise on
| this. So I am hoping that I can get a lot of correspondece confirming my
| belief.
|
| Here it is: I believe the following: (1)If a language has an "endif",
then
| every "if" statement requires an endif, and (2) every "endif" goes with
the
| last "if".
|
| For example,
|
| If .....
| if
| endif
|
| should not compile because there must be two "endif"s and the last "endif"
| goes with the last "if". The actual nested if statement is far more
complex,
| and it is just being used in a presentation that is not being compiled
| (psuedo code).
|
| Finally, were can I find this? I don't care the language, although some
| version of Basic would be best since that is what my friend understands.
|
| Thanks for helping
|
 
Hello lawjake,
(1)If a language has an "endif", then every "if" statement requires an
endif

This is not the case as you can see, there is only one "End If" (in VB):

If a = 1 Then
If b = 2 Then c = 3 Else c = 4
End If

So you need only one "End If". But for readability it is usually advised
to use If...End If constructs, so it would then look like this:

If a = 1 Then
If b = 2 Then
c = 3
Else
c = 4
End If
End If

(2) every "endif" goes with the last "if".

No, every "End If" closes the its "If" block, e.g.

If a = 1 Then
If b = 2 Then
If c = 3 Then
If d = 4 Then

Msgbox "Test"

End If ' d = 4
End If ' c = 3
End If ' b = 2
End If ' a = 1

Best Regards,

HKSHK
 
Back
Top