if (some condition) --suppose this gets true
....
....
elseif (some condition)--this also gets false
...
...
else
... --this else condition becomes false
...
if (some condition)
..
else
..
endif-- stops execution when it gets here,doesnt moves on to next endif
endif
now what I want to do is to skip the else part. For which I am skipping it until it gets an endif keyword. But it stops the execution when it gets the first endif in the else part. How to skip that endif so that it matches the exact endif of a if.
Its really confusing please help...
You need to get your conditional logic straight:
magicNumber = 42
If magicNumber = 42 Then
wscript.echo "Magic number is 42!"
ElseIf magicNumber > 23 Then
wscript.echo "Magic number is more than 23"
Else
wscript.echo "Magic number is 23 or less"
If magicNumber <= 0
wscript.echo "Woah! Magic number is negative!"
else
wscript.echo "Phew, at least Magic number is not zero or negative"
End if
wscript.echo "Bye!"
End if
wscript.echo "End."
' Output:
' Magic number is 42!
' End.
Because the first condition is True, none of the other conditions will be tested and executed.
Related
Why is the else statement is not allowed to have a then or other conditions?
Is it because it is the final condition within the else-if conditions it represents?
I am struggling to understand this concept since I'm a beginner who just learned about variables.
I'm asking this because I received an error with my else statement in the code:
message = 0
condition = 30
if condition <=10
message = “try harder”
elseif
condition <=20 then
message = "Almost learning"
else
condition = <=30 **—This is the line where I get the error message**
message = "Now you’re getting it"
end
print(message)
Would appreciate someone breaking down in laymen terms, why else is not allowed to have < or > or then or other conditions.
else condition = <= 30
(which is the way your code was originally formatted) would be a very unusual feature in a language.
The whole point of else on its own is to execute if none of the other conditions were true. So a condition on the else is absolutely useless.
The Programming in Lua book if statement shows the normal usage:
if op == "+" then
r = a + b
elseif op == "-" then
r = a - b
elseif op == "*" then
r = a*b
elseif op == "/" then
r = a/b
else
error("invalid operation")
end
However, your actual code (when formatted correctly) ends up looking like:
else
condition = <=30
which is correct in terms of the else but unfortunately makes the next line a statement. And this statement is very much incorrect syntax.
Now it may be that you meant to assign 30 to condition but, based on your other lines (that sort of match this as a condition), I suspect not. So it's probably best just to remove that line totally.
I am new to Fortran (I mean I started 5 mins ago) and i have been messing around and I know how to do prints start/end programs and the if part of the if statement. The problem I am having, though, is with the else part. It keeps coming up with the error:
ELSE() print *, "x did not = 1"
1
Error: Unexpected junk after ELSE statement at (1)
Here is my code:
program hello
x = 1
IF(x==1) print *, "Hello"
IF(x==1) x=2
IF(x==1) print *, "Oh it didnt work..."
ELSE() print *, "x did not = 1 yay it worked"
end program hello
The correct structure for your example code should be similar to:
program hello
x = 1
if( x == 1) print *, 'hello'
if( x == 1) x=2
25 if( x == 1) then
print *, "oh it didn't work..."
else
print *, "x did not = 1 yay it worked"
endif
end program hello
Notice that the if statement in line 25 is followed by the 'then' keyword and you options are divided by the else statement.
Also the line number is not necessary. I just used it here so I could reference the line in my answer.
I'm trying to determine if it's between the hours of 12 am and 1 am. Here is my if statement:
If InStr(Time,"12") AND InStr(Time,"AM") Then
' Do something
Else
' Do something else
End If
The problem is that this statement evaluates to false, even if both of the conditions are true. I know this because I have tried a nested if like this
If InStr(Time,"12") Then
If InStr(Time,"AM") Then
' Do something
...
And that works. This also works
If InStr(Time,"12")<>0 AND InStr(Time,"AM")<>0 Then
' Do something
...
But if it works as a nested if, why can't I test both of the nested if conditions in a single if statement?
I replaced the InStr function calls with the values that they return
If 1 AND 10 Then
' Do something
Else
' Do something else
End If
And the same thing happened: the if statement evaluated as false and the "Do something else" commands were executed instead. But when I nested the second condition as another if statement inside the first if statement, the "Do something" commands were executed.
Why is that and is there any way to do this without the <>0 and without nesting?
If Time() >= TimeValue("12:00:00") AND Time() <= TimeValue("23:59:59") then
'Do Something
ElseIf Time() >= TimeValue("00:00:00") AND Time() <= TimeValue("01:00:00") then
'Do the same
Else
'Do something different
End If
This should work :)
The problem you observed is caused by the fact that VBScript uses the same operators for boolean and bit operations, depending on the data type of the operands. The InStr function returns a numeric value unless one of the strings is Null, so the operation becomes a bitwise comparison instead of a boolean comparison, as JosefZ pointed out. The behavior is documented:
The And operator also performs a bitwise comparison of identically positioned bits in two numeric expressions and sets the corresponding bit in result [...]
Demonstration:
>>> WScript.Echo "" & (True And True)
True
>>> WScript.Echo "" & (6 And 1) '0b0110 && 0b0001 ⇒ 0b0000
0
>>> WScript.Echo "" & (6 And 2) '0b0110 && 0b0010 ⇒ 0b0010
2
To enforce a boolean comparison you need to use InStr(...) > 0 or CBool(InStr(...)) (both of which evaluate to a boolean result) instead of just InStr(...) (which evaluates to a numeric result).
Date and Time are stored as number of days, where midnight is 0.0, and 1 am is 1/24 :
If Time <= 1/24 Then ' or If Time <= #1am# Then
When you using Time() function and if result like that 10:12:12 AM in this way Instr will result Ture because Instr by default use vbbinarycompare looking For any 12in binary format in 10:12:12 AM and there is sec and min 12 so it will Return True .
just try this :
myHour=replace(Time,Right(Time,9),"") 'get only the hour from time
myAMPM=replace(Time,Time,Right(Time,2)) 'get only AM or PM from time
If InStr(1,myHour,12,1) > 0 AND InStr(1,myAMPM,"AM",1) > 0 Then
wscript.echo "True"
Else
wscript.echo "False"
End If
I keep getting an error "A string is required here" in my Crystal Report formula. The part that gets highlighted is the "else" portion, suggesting that is where the error is. I've tried so many different variations of brackets, no brackets, semi-colons, etc... sometimes I get a different error but none of it seems to work.
Local stringVar outputString := "";
if isNull({Evaluation.Strategic}) then
(
outputString := "None"
)
else
(
stringVar array x := split({Evaluation.Strategic},"|");
Local numberVar i;
For i:=1 to Count(x) do
(
outputString := outputString + x[i] + Chr(10)
)
);
outputString;
Can anyone please point out to me the correct syntax to do what I want to do? All the samples I've found online (there are few with multiple lines inside the if-else blocks) suggest this should work.
Thanks
Based on the below 2 sites I really thought you could have multiple statements inside an if-else but nothing I tried was working.
http://www.experts-exchange.com/questions/23735693/Multiple-line-IF-statement-in-Crystal-Reports.html
http://www.kenhamady.com/formulas/form08.shtml
I ended up getting this to work by restructuring my logic to be as shown below. It's a little clunky but it made the CR compiler happy.
//Syntactically correct - this worked:
//-------------------------------------
Local stringVar outputString := "";
stringVar array x := "";
Local numberVar i;
If isNull({Evaluation.Strategic}) Then
(
x := ""
)
Else
(
x := split({Evaluation.Strategic},"|");
);
If x <> "" Then
(
For i:=1 to Count(x) do
(
outputString := outputString + x[i] + Chr(10)
)
)
else
(
For i:=1 to Count(x) do
(
outputString := "None";
);
);
outputString;
Edited: And another site that suggests you can have more than one statement inside an If:
https://msdn.microsoft.com/en-us/library/ms225356(v=vs.80).aspx
IF ([enter first condition here] or [enter second condition here])
THEN ([enter first execution here]; [enter second execution here])
ELSE 0
worked for me (from links in the first answer)
You need to remember your semi-colons at the end of each statement including the after the if
For instance this will not validate -
if (myCondition) Then (
a:= "First"
b:= "Second"
)
if (myOtherCondition) Then (
a:= a + "Other First"
b:= b + "Other Second"
)
However if you add the semi-colons Then it will validate -
if (myCondition) Then (
a:= "First";
b:= "Second";
);
if (myOtherCondition) Then (
a:= a + "Other First";
b:= b + "Other Second";
);
While Crystal does not need the semi-colon after the final statement within a the scope it is probably good practice to add it at the end of every statement as it will help avoid issues like this.
I have the following code setup in a form and I'm getting the "Expected Statement" error. My first time doing this and thought I had the syntax correct, what am I missing?
<%
If Trim(oQuote("shipmeth"))="FREIGHT" Then
Response.Write "Freight"
ElseIf Trim(oQuote("shipmeth"))="THRSHLD" Then
Response.Write "Threshold"
End If
%>
When using nested 2-way conditionals, each conditional must be closed by its own End If:
If condition_A Then
action_A
Else
If condition_B Then
action_B
Else
If condition_C Then
action_C
Else
action_D
End If 'condition_C
End If 'condition_B
End If 'condition_A
Only an n-way conditional can be closed with a single End If (because it's just a single conditional):
If condition_A Then
action_A
ElseIf condition_B Then
action_B
ElseIf condition_C Then
action_C
Else
action_D
End If
However, this kind of n-way conditional only makes sense when you're checking different conditions, e.g.
If IsEmpty(a) Then
...
ElseIf b > 23 Then
...
When checking the same variable for different values it's better to use a Select statement as Alex K. suggested:
Select Case foo
Case "a"
'handle foo="a"
Case "b", "d"
'handle foo="b" as well as foo="d"
Case Else
'handle non-matches
End Select
The first statement following the If must be on a new line;
If Trim(oQuote("shipmeth"))="FREIGHT" Then
Response.Write "Freight"
Following conditions can be on the same line but must use ElseIf
ElseIf Trim(oQuote("shipmeth"))="THRSHLD" Then Response.Write "Threshold"
ElseIf ...
I would suggest a more readable Case;
select case Trim(oQuote("shipmeth"))
Case "THRSHLD"
Response.Write "Threshold"
Case "PREMTHRHLD"
Response.Write "Premium Threshold"
...
end select
Which has the additional advantage of only ever executing Trim(oQuote("shipmeth")) once.
I think "else if" should be one word, like elseif
You have nice answers here, I'll append just one more (lightly optimized) alternative.
strTest = Trim(oQuote("shipmeth"))
strResult = "Unexpected"
If strTest ="FREIGHT" Then strResult = "Freight"
If strTest ="THRSHLD" Then strResult = "Threshold"
Response.Write strResult
EDIT
And just to clarify my thought, I hate nested If..Then, unfortunately no GoTo Label in VBScript, therefore above double comparison could be optimized with function.
strResult = "Unexpected"
MyResponse Trim(...), strResult
Response.Write strResult
Sub MyResponse(strIn, strOut)
If strIn = "FREIGHT" Then
strOut = "Freight" : Exit Sub
End If
If strIn = "THRSHLD" Then strOut = "Threshold"
End Sub