Cannot enter if statement - if-statement

As a part of a larger script following if statement:
Logger.log(vr5);
if (vr5 == "zwak met 5-4 krt in ♡/♠"){
Logger.log("in if");
}
Logger gives: vr5: zwak met 5-4 krt in ♡/♠
But is does not enter the if Logger. "in if" does not show up
Second exampel.
Logger.log(vr7);
if (vr7 == "3♣ (6 krt ♡ en 5-7 p), 3♢ (6 krt ♠ en 5-7 p), 3♡ (6 krt ♠ en
8-10 p), 3♠ (6 krt ♡ en 8-10 p)"){
Logger.log("in if");
}
Also in this case Logger vr7: 3♣ (6 krt ♡ en 5-7 p), 3♢ (6 krt ♠ en 5-7 p), 3♡ (6 krt ♠ en 8-10 p), 3♠ (6 krt ♡ en 8-10 p) full fills the if condition, but does not enter the if.
What i am doing wrong? Who can help me?
PS vr5 and vr7 are answers in a googleform.

I read all the possibility of "How do I compare strings in Java" but none of them worked. The answer of Mohammed E. ManSour was (nr.3):
if (string1.compareTo(string2) == 0)
{
// this line WILL print
System.out.println("The two strings are the same.")
}
However google apps script does not recognize compareTo. So instead of compareTo I tried string1.equals(string2) == 0). See my first example.
if (vr5.equals("zwak met 5-4 krt in ♡/♠") == 0){
Logger.log("in if");
}
This works!

Related

Is the value of a Fortran DO loop counter variable guaranteed to be persistent after the loop ends? [duplicate]

This question already has answers here:
Why is the Fortran DO loop index larger than the upper bound after the loop?
(2 answers)
Closed 5 years ago.
How do DO loops work exactly?
Let's say you have the following loop:
do i=1,10
...code...
end do
write(*,*)I
why is the printed I 11, and not 10?
But when the loop stops due to an
if(something) exit
the I is as expected (for example i=7, exit because some other value reached it's limit).
The value of i goes to 11 before the do loop determines that it must terminate. The value of 11 is the first value of i which causes the end condition of 1..10 to fail. So when the loop is done, the value of i is 11.
Put into pseudo-code form:
1) i <- 1
2) if i > 10 goto 6
3) ...code...
4) i <- i + 1
5) goto 2
6) print i
When it gets to step 6, the value of i is 11. When you put in your if statement, it becomes:
1) i <- 1
2) if i > 10 goto 7
3) ...code...
4) if i = 7 goto 7
5) i <- i + 1
6) goto 2
7) print i
So clearly i will be 7 in this case.
I want to emphasize that it is an iteration count that controls the number of times the range of the loop is executed. Please refer to Page 98-99 "Fortran 90 ISO/IEC 1539 : 1991 (E)" for more details.
The following steps are performed in sequence:
Loop initiation:
1.1 if loop-control is
[ , ] do-variable = scalar-numeric-expr1 , scalar-numeric-expr2 [ , scalar-numeric-expr3 ]
1.1.1 The initial parameter m1, the terminal parameter m2, and the incrementation parameter m3 are established by evaluating scalar-numeric-expr1, scalar-numeric-expr2, and scalar-numeric-expr3, respectively,
1.1.2 The do-variable becomes defined with the value of the initial parameter m1.
1.1.3 The iteration count is established and is the value of the expression
MAX(INT((m2 –m1+m3)/m3),0)
1.2 If loop-control is omitted, no iteration count is calculated.
1.3 At the completion of the execution of the DO statement, the execution cycle begins.
2.The execution cycle. The execution cycle of a DO construct consists of the following steps performed in sequence repeatedly until
termination:
2.1 The iteration count, if any, is tested. If the iteration count is zero, the loop terminates
2.2 If the iteration count is nonzero, the range of the loop is executed.
2.3 The iteration count, if any, is decremented by one. The DO variable, if any, is incremented by the value of the incrementation parameter m3.

Strange behaviour while adding string to another string in C++ [duplicate]

This question already has answers here:
How can I repeat a string a variable number of times in C++?
(10 answers)
Closed 2 years ago.
one is 2, and ans is "000000".
string ans = "000000";
ans += string("1", one);
cout<<ans<<endl;
The output is:
0000001�
But I want the output:
00000011
What am I doing wrong?
string("1", one) does not do what you think it does. It does not duplicate the "1" string one number of times. It instead copies the 1st one number of chars from "1", which in this case is the '1' character and the '\0' null-terminator that follows it, which is where the � is coming from in the output. That is not what you want.
Use string(one, '1') instead. That will duplicate the '1' character one number of times, like you want, eg:
ans = "000000";
ans += string(one, '1');
cout << ans << endl;
Just use c++ strings and use + operator to catenate strings.

what does this line of code do? player = (player % 2) ? 1 : 2; [duplicate]

This question already has answers here:
What does the question mark character ('?') mean in C++?
(8 answers)
Closed 7 years ago.
I can`t understand this line
why we use question mark "?" in it.
there are 2 player 1 and 2 .
player = (player % 2) ? 1 : 2;
This is a conditional if, and is the same as:
if(player % 2)
player = 1; // Odd
else
player = 2; // Even
Another way to do this without an if branch:
player = 2 - (player & 0x01);
The least significant bit is zero for even numbers.
It's the ternary operator.
This line of code will set player to 1 if player originally was odd, and to 2 if it was even.
It's the ternary operator. It takes this form:
boolean expression ? a : b;
Which translates to:
If this expression is true, then a else b
It's often used as the right-hand expression in assignment operators. In your case player is being assigned 1 or 2 based on whether they are even or odd.
It meaning that if the Condition is true so Player have the Value 1 else it have 2.
if(player % 2) {
player = 1;
} else {
player = 2;
}

C++ If else not working as intended? (Really weird bug)

I could use some help because I am really desperate now. Here, take a look at this piece of my code and then look at the output. What is causing this bug? How am I supposed to fix it?
Thanks for any kind of help!
The code:
while (1)
{
while (1)
{
cout << "Choose your username: ";
cin >> username;
std::strcpy (username1, username.c_str());
if (isdigit(username1[0]) || isdigit(username1[1]) || isdigit(username1[2]))
{
cout << "The first 3 characters HAVE to contain only letters!\n\a";
}
else if (username1[3] < 0) // I don't actually know why, but this works as intended o.O
{
cout << "Your username HAS to contain atleast 3 letters!\n\a";
}
else if (username1[10] > 0) // Works - dunno why o.0
{
cout << "Your username CAN ONLY contain maximum 10 characters!\n\a";
}
else
break;
}
The output:
Choose your username: ko
Your username HAS to contain atleast 3 letters!
Choose your username: k
Your username HAS to contain atleast 3 letters!
Choose your username: kokokokokoo // Now that's > 10
Your username CAN ONLY contain maximum 10 characters! // Now this is okay BUT...
Choose your username: ko
Your username CAN ONLY contain maximum 10 characters! // Wrong error!
Choose your username: kok // This should be accepted!
Your username CAN ONLY contain maximum 10 characters! // Well, it is not... there should not be an error at all!
Pay attention to the comments I added in the output so you know what is wrong.
Thank you for all answers!
The stuff in square brackets isn't doing what you think it's doing.
When you use the username1[10] code, what you're actually saying is "get me the character at position 11 in the string (remember, indexing starts at zero).
So the following line of code...
...
else if (username1[3] < 0)
...
Is literally saying "is the character at position 4 in the string less than zero?", and that's clearly nonsense (ascii characters can't be negative).
What you likely intended to say is "Is the length of username1 less than three characters?", in which case you should use the ::strlen method, like this:
else if (::strlen(username1) < 3)
As another answer mentions however, if you're using pure C++, there might be no need to use username1 at all, you could just call std::string::size() on the original username variable. Like this:
else if (username.size() < 3)
The reason that your code is working in unexpected ways is because you're trying to look for characters in the string that aren't there.
Imagine the following:
username1: [ k | o | k | o | \0 | 4 | f | 6 | 7 | a | 3 | 3 ]
indexes: 0 1 2 3 4 5 6 7 9 9 10 11
The above is a basic image of some raw memory, as you can see, at location zero you have your string "koko", then a null terminator, and then lots of unrelated garbage that you haven't initialized. When you say username1[10], you're going past the end of the input string and into the garbage memory that has nothing to do with your string variable. So the if statement fails!
HOWEVER, because the characters in positions 5-11 are uninitialized, sometimes the character at position 10 could be zero, or it could be something else, or it could even cause your program to crash! This is what's known as undefined behaviour, and you should avoid it at all costs!
There's nothing setting username1[10] back to zero after you put the string "kokokokokoo" in it.
If you used strncpy instead of strcpy then (a) you would be safe from buffer overflows and (b) strncpy zero-pads the destination so your program would work as you hoped.
But really, as other comments have said, you should check the length of the string before you copy or use it.
After you entered
kokokokokoo
and then ebtered
ko
character array username1 contains the following
[k] [o] ['\0'] [o] [k] [o] [k] [o] [k] [o] [o] ['\0']
0 1 2 3 4 5 6 7 8 9 10 11
So this condition in the if statement
if (isdigit(username1[0]) || isdigit(username1[1]) || isdigit(username1[2]))
is false because neither of three characters [k] [o] ['\0'] is a digit.
This condition
else if (username1[3] < 0)
is also false because username1[3] is equal to 'o' which is greater than zero.
This condition
else if (username1[10] > 0)
is true because username1[10] is equal to 'o' that is greater than zero.
After you entered string literal
kok
username1 become to look as
[k] [o] [k] ['\0'] [k] [o] [k] [o] [k] [o] [o] ['\0']
0 1 2 3 4 5 6 7 8 9 10 11
In fact there was nothing changed relative to the conditions. As username1[10] is equal to 'o' that is greater than zero then you get message
Your username CAN ONLY contain maximum 10 characters!
Also it is not clear why you copy object of type std::string username into the character array username1.
You could do all checks using username instead of username1 and after that you could copy username into username1 if it is required. For example
if ( username.size() < 3)
{
cout << "Your username HAS to contain atleast 3 letters!\n\a";
}
else if ( username.size() > 10)
{
cout << "Your username CAN ONLY contain maximum 10 characters!\n\a";
}
else if ( isdigit( username[0] ) || isdigit( username[1] ) || isdigit( username[2] ) )
{
cout << "The first 3 characters HAVE to contain only letters!\n\a";
}
//...
The parts where you don't know why they work don't work. You most likely don't need username1 at all; what you should really be doing is checking the input length with username.length():
if (username.length() < 3) {
// "too short" error
else if (username.length() > 10) {
// "too long" error
What you're doing is examining the (possibly indeterminate garbage) characters at indexes 3 and 10 of the string. The strcpy doesn't wipe index 10 when it copies something shorter into the string.

How works this If statement? [duplicate]

This question already has an answer here:
Fortran IF statement with numbers/labels rather than another statement
(1 answer)
Closed 7 years ago.
I'm trying to teach myself fortran so I can unravel an old program and repurpose it for our own use. I can't figure out what this statement does:
if(s - fm) 198, 198, 197
s - fm isn't a condition that can be true or false, right?.
And when it passes control to the line marked 198, does it then continue to the end of the program? How does it know when to come back to execute 198 again and then 197?
This is an "archaic" form of IF:
IF (''arithmeticExpression'') ''firstLineNumber'', ''secondLineNumber'', ''thirdLineNumber''
In the second form, the arithmetic expression is evaluated. If the expression evaluates to a negative number, then execution continues at the first line number. If the expression evaluates to zero, then execution continues at the second line number. Otherwise, execution continues at the third line number.
It's a "three-way goto" depending on the sign of the expression.
In a more traditional C-like language it wood be
/* IF(a) label1, label2, label3 */
if(a > 0)
{
goto label3;
} else
if(a < 0)
{
goto label1;
} else
{
// a == 0
goto label2;
}
Your case contains two labels 198 which works like
if(s <= fm) { goto lbl198; } else { goto lbl197; }
Ref: wikibooks
This obsolete feature puzzles a lot of people:
FORTRAN compiler warning: obsolete arithmetic IF statement
Fortran strange IF
strange label usage