Курс Раку / Основи / Умовні перевірки / Порівняння рядків
Тест 2 — умова в 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.Навігація по курсу
← Умовні перевірки / Порівняння чисел | Умовні перевірки / Тернарний оператор →
💪 Or jump directly to the exercises to this section.
Ця сторінка автоматично перекладена за допомогою ChatGPT 4o. Оригінальний текст англійською мовою.
Переклади цієї сторінки: English • Deutsch • Español • Italiano • Latviešu • Nederlands • Български • Русский • Українська