Course of Raku / Essentials / Conditional checks / Comparing strings

Quiz 2 — the condition in if*

Correct the program and answer the question.

. my $password = prompt 'Enter your password: ';
eq = if $password ␣␣ '*&(#&$#Y' {
.     say 'Correct';
. }
. else {
.     say 'Incorrect';
. }

With the = in the test, what does this program do?

Always prints ‘Correct’ Your answer:  (: Always prints ‘Correct’, Always prints ’Incorrect’, Prints ’Correct’ for correct passwords and ‘Incorrect’ for incorrect ones, Prints ’Incorrect’ for correct passwords and ’Correct’ for incorrect ones :)
The program always prints ’Correct’ because $password = '*&(#&$#Y' is an assignment that sets the new value of the $password variable. As the value is neither zero nor an empty string, it is considered True. The password that the user enters is never checked in this case.

If you type ==, then the program will break as it tries to convert the strings (the user password and the correct password) to numbers and fails.

Course navigation

Conditional checks / Comparing numbers   |   Conditional checks / Ternary operator

💪 Or jump directly to the exercises to this section.