While loop unclear, can someone help?

Joined
Dec 6, 2023
Messages
1
Reaction score
0
Hello everybody,

I am new to coding, and I am learning Javascript on my own. I have found the following mini-program and I don´t understand how the algorithm works.

let yourName;
do {
yourName = prompt("Who are you?");
} while (!yourName);
console.log(yourName);


This program will force you to enter a name. It will ask again and again until it gets something that is not an empty string.
Applying the ! operator will convert a value to Boolean type before negating it, and all strings except "" convert to true.

This means the loop continues going round until you provide a non-empty name.

Clearly the program works because I run it. However, I do not understand the logic of this while loop with the "logical NOT operator"

while (!yourName);
console.log(yourName);


When someone enters a (not empty) string value, the operator transforms it into a boolean and then negates it. Which means that in the end (!yourName) = false.
So my while loop seems to say:
"while the condition in the parentheses is false, print out yourName". What confuses me is that I believed that the condition in the while loop needs always to be truthy/true in order to take the action (print out in this case)
I was thinking that the logic is always

while (true) -----> do this action

I hope I clarified my point. Thank you in advance for this awesome forum and any help you can give me :)

Salva
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
In JavaScript, both while and do-while are loop structures used for repetitive tasks, but they have a key difference in when the loop condition is checked.

while loop:
  • checks the loop condition before the loop body is executed.
  • If the condition is initially false, the loop body will not be executed at all.
do-while loop:
  • on the other hand, checks the loop condition after the loop body is executed.
  • this means that the loop body is guaranteed to run at least once, even if the condition is initially false.
In summary:
  • while checks the condition before executing the loop body and may not execute the body at all.
  • do-while executes the loop body at least once, regardless of the initial condition, and then checks the condition.

In JavaScript, the exclamation mark ( ! ) is the logical NOT operator. It negates the truthiness of a value. When used in a condition, it flips the truthiness of the expression.

So, if (!false) means "if not false," which evaluates to true. This is because !false results in the boolean value true.
In this case, the code inside the if block will be executed because !false is true.
JavaScript:
if (!false) {
  console.log("This block will be executed because the condition is true.");
} else {
  console.log("This block will not be executed.");
}
 
Joined
Sep 21, 2022
Messages
122
Reaction score
15
There's more than one way to skin a rabbit. There's more than one way to write the same program.

The two statements

while (!frog)

and

while (frog == "")

are logically the same, but the second is more readable.

Readability is important when searching for bugs in large programs.

Just my opinion.
 
Joined
Sep 3, 2023
Messages
36
Reaction score
2
Maybe old basic could clarify how the logic that goes.

Code:
10 name$ = ""
20 rem DO{...} WHILE(...)
30 input "Enter your name"; name$
40 if name$ <> "" then goto 100
50 goto 20
100 rem DONE
110 print "Howdy " + name$

10 name$ = ""
20 rem REPEAT{...} UNTIL(...)
30 input "Enter your name"; name$
40 if name$ = "" then goto 20
100 rem DONE
110 print "Howdy " + name$

20 input "Enter your name"; name$
30 rem WHILE(...){...}
40 if name$ <> "" then goto 100
50 input "I don't get that. Re-enter your name"; name$
60 goto 30
100 rem DONE
110 print "Howdy " + name$

Note that the repeat/until loop requires inverted logic. And the regular while loop you have to duplicate the prompt code.
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
hello !

while the var "YourName" is length == 0 , it keep the loop step.

the var is define as it is and empty,
so the test on it result : false
if( youName ) // give false before you fill it by a string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top