Velocity Template Language: Connect two conditions in one #elseif() statement - if-statement

In my example the parameters are:
$p2.length
which has the value "0"
and the boolean
$suspectYesNo which is set to "false".
I would expect, that in my code the second #elseif will run due to my set parameters, but however it shows up that "Code 4 runs"
I guess, that my mistake is the connection of both conditions using "&&", but I have no clue how to correctly define the line with two conditions.
That brings me to the question: How to correctly combine the two conditions ($p2.length()==0) AND ($suspectYesNo.value.contains("true")) in one #elseif().
Or did I define the boolean wrong?
#if($p2.length()>0)
Code 1 runs
#elseif($p2.length()==0 && $suspectYesNo.value.contains("true"))
Code 2 runs
#elseif($p2.length()==0 && $suspectYesNo.value.contains("false"))
Code 3 runs
#else
Code 4 runs
#end

Thanks to user7294900 for providing me a reference with which I could solve the problem.
It did not read the boolean value as a string, which could be fixed by adding "toString()" method.
With this code here it works as desired:
#if($p2.length > 0)
Code 1 runs
#elseif($p2.length == 0 && $suspectYesNo.value.toString().contains("true"))
Code 2 runs
#elseif($p2.length == 0 && $suspectYesNo.value.toString().contains("false"))
Code 3 runs
#else
Code 4 runs
#end

Related

Bug in a fixed If - Else statement

this is my first post in this site.
As the title says, I encountered a bug in an If - else if - else block of codes.
What the problem is that, I am pretty confident that I have covered all 9 possible outcomes that there should be.
It is quite hard to explain more, so if you guys could take a look at my .cpp file and run it ( hopefully it runs because I used c++ on a mac), you may find an easier understanding of my problem.
source code cpp file
If you browse through my action();, in the else statement, I purposely displayed that you encountered a bug so just in case there were bugs, I'd be informed.
What I am trying to do is like this:
userinput | randomAImove | outcome
A 1 statement
A 2 statement
A 3 statement
D 1 statement
D 2 statement
D 3 statement
W 1 statement
W 2 statement
W 3 statement
else 1||2||3 statement
There are corresponding statements to each conditions met.
'A', 'W', 'D' are user input. I used a toupper command to force the compiler to just check on 'A' 'W' 'D'. However, the main problem I find is that, even if the toupper works(which I confirmed), the program displays the correct statement often but still somehow manages to bug even if the user(I tried it) input from A, W, D (not-case sensitive cuz of toupper).
Please just try it and after a few tries you may encounter it also. I wrote "you encountered a bug" if you ever do encounter it.
Please help me because I can't really see any other way to find the mistake. Maybe its in my plain sight but I can't seem to notice which one is it.
Thank you!
Change the AImove function to
void AImove()
{
turn = rand () % 3 + 1;
}
(add + 1), or you may get 0 in turn and it will lead you "encounter a bug".

Two && in a while condition?

I was trying to help a friend with a problem, he asked me how can he make the GCD of 3 numbers using the Nicomachus method. Everything went great until I tried to enter this condition:
while (a!=b && b!=c && a!=c)
But the problem is it would only execute once. I changed it into this:
while (a!=b && b!=c)
I know it results into the same thing but I was just wondering why the first one can't work ? I can't add 2 && in the same condition ?
You certainly can have 2 && in the same condition - you can have as many as you want. The fact that the loop stops depends on something else.
By the way, if you had a=10, b=20, C=10, the first condition (while (a!=b && b!=c && a!=c)) would stop (because a != c would be false), but the second one (while (a!=b && b!=c)) would continue. Probably this is what happened.
Transforming 3 conditions into just 2 can be done in the opposite case, that is, if you want to make sure they are all equal: a ==b && b == c automatically implies that a == c (see Transitive relation of equality), so adding this 3rd condition or not doesn't make any difference. But the same is not true for inequality, as I have shown.

If statement compatibility issue in Swift (it worked before)

i reopened a project i stop working on for some days and without edditing any line, when i tried to run it, it gave me two errors i never saw before and i persoanlly think they make no sense. Please help me, the code is like this:
if normals == true && numberOfTaps > highScoreN.integerForKey("highscoren") || normals == 1 && highScoreN.integerForKey("highscoren") == 0
It gives me this error: "Cannot invoke '==' with an argument list of type '($T16, $T31)'"
and in another line, which is practically the same:
if pros == true && numberOfTaps > highScoreP.integerForKey("highscorep") || pros == 1 && highScoreP.integerForKey("highscorep") == 0
It gives me this error: "Cannot invoke '==' with an argument list of type '($T16, $T31)'"
I repeat, it had worked properly some days before, i dont know why it isn't..
Thank you so much!
PSD:// highScoreP and highScoreN are NSUserDefault type
The error is on the == in both lines, so that narrows it down. The only place you have == is comparing the normals variable to true and 1. Swift is very type safe, so your normals variable cannot be both true (a boolean) and 1 (an Int). Check the type of your normals variable and proceed form there.

"Invalid expression, assumed zero" error on if-statement

I'm getting the following error whenever I compile my code: "Error 029: Invalid expression, assumed zero"
The error is thrown on the following line:
if ((PlayerInfo[playerid][ADMINLevel])) || (IsPlayerAdmin(playerid))
I want the if-statement to check if "ADMINLevel" is above zero or if the player is logged in as an RCON admin.
You're constructing your if-statement wrong. The correct way to do it is
if(PlayerInfo[playerid][ADMINLevel] > 0 || IsPlayerAdmin(playerid))
{
/* Put your desired script here */
}
Your code was nearly correct (although it did have some unnecessary brackets), you just need to actually add a comparison to the ADMINLevel check. An if-statement should be like a question ("is admin level more than 0", rather than just "is admin level"). You can find more information about if-statements in Pawn here, and I think it will be useful for you to read.
PlayerInfo[..][..] does not return a boolean. Add > 0 to fix it

Set Visual Studio (conditional) breakpoint on local variable value

I'm trying to debug a method which among other things, adds items to a list which is local to the method.
However, every so often the list size gets set to zero "midstream". I would like to set the debugger to break when the list size becomes zero, but I don't know how to, and would appreciate any pointers on how to do this.
Thanks.
Why not use conditional breakpoints?
http://blogs.msdn.com/saraford/archive/2008/06/17/did-you-know-you-can-set-conditional-breakpoints-239.aspx
in C#
if(theList.Count == 0){
//do something meaningless here .e.g.
int i = 1; // << set your breakpoint here
}
in VB.NET
If theList.Count = 0 Then
'do something meaningless here .e.g.
Dim i = 1; ' << set your breakpoint here
End If
For completeness sake, here's the C++ version:
if(theList->Count == 0){
//do something meaningless here .e.g.
int i = 1; // << set your breakpoint here
}
I can give a partial answer for Visual Studio 2005. If you open the "Breakpoints" window (Alt + F9) you get a list of breakpoints. Right-click on the breakpoint you want, and choose "Condition." Then put in the condition you want.
You have already got both major options suggested:
1. Conditional breakpoints
2. Code to check for the wrong value, and with a breakpoint if so happens
The first option is the easiest and best, but on large loops it is unfortunately really slow! If you loop 100's of thousands iterations the only real option is #2. In option #1 the cpu break into the debugger on each iteration, then it evaluates the condition and if the condition for breaking is false it just continiues execution of the program. This is slow when it happens thousands of times, it is actually slow if you loop just 1000 times (depending on hardware of course)
As I suspect you really want an "global" breakpoint condition that should break the program if a certain condition is met (array size == 0), unfortunately that does not exist to my knowledge. I have made a debugging function that checks the condition, and if it is true it does something meaningless that I have a breakpoint set to (i.e. option 2), then I call that function frequently where I suspect the original fails. When the system breaks you can use the call stack to identify the faulty location.