Raku kurss / Pamati / Nosacījumu pārbaudes / Virkņu salīdzināšana

Tests 2 — nosacījums 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.

Kursa navigācija

Nosacījumu pārbaudes / Skaitļu salīdzināšana   |   Nosacījumu pārbaudes / Trīsvērtīgais operators


💪 Or jump directly to the exercises to this section.

Šī lapa ir automātiski tulkota ar ChatGPT 4o. Oriģinālais teksts angļu valodā.

Šīs lapas tulkojumi: EnglishDeutschEspañolItalianoLatviešuNederlandsБългарскиРусскийУкраїнська