Cursus Raku / Elementa / Conditiones comprobationes / Comparando catenae
Quiz 2 — condicio 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
If you type
$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.Cursus navigatio
← Conditiones comprobationes / Comparando numeri | Conditiones comprobationes / Ternarius operator →
💪 Or jump directly to the exercises to this section.
Haec pagina automatice translata est per ChatGPT 4o. Originalis Anglicus textus
Translationes huius paginae: English • Deutsch • Español • Italiano • Latviešu • Nederlands • Български • Русский • Українська