SharpOrange
SUBHUMAN SHUBHAM
- Joined
- Jul 3, 2023
- Posts
- 1,383
- Reputation
- 2,839
for loop ex Print 1 to 10
while loop print till 10 stop after
do while loop
now for loop is used when you know how long you want to execute.
while has one condition till then it runs but can run infinite time also.
do while loop has also one condition but it will run one time for sure unlike while which has to satisfy the condition before it runs but do while will run one time.
as i am low iq so i got confused in do while loop so asked gpt for a eli5.
here you go
Kid asks:
“Is it safe to slide?”
If YES → slides
If NO → doesn’t slide at all
If the condition is false, it never runs
Kid says:
“I’m gonna slide once first…
then I’ll check if I should do it again.”
Slides at least one time, no matter what
Because sometimes you must do something once, even if the condition is false.
Password check
do {
ask for password;
} while (password is wrong);
You must ask at least once, right?
A while loop might not ask at all.
tomorrow i will do two sum and some pattern problems with loops on leetcode.
while loop print till 10 stop after
do while loop
now for loop is used when you know how long you want to execute.
while has one condition till then it runs but can run infinite time also.
do while loop has also one condition but it will run one time for sure unlike while which has to satisfy the condition before it runs but do while will run one time.
as i am low iq so i got confused in do while loop so asked gpt for a eli5.
here you go
while loop
Kid asks:“Is it safe to slide?”
If YES → slides
If NO → doesn’t slide at all
If the condition is false, it never runsdo-while loop
Kid says:“I’m gonna slide once first…
then I’ll check if I should do it again.”
Slides at least one time, no matter whatWhy do we use do-while?
Because sometimes you must do something once, even if the condition is false.
Simple real-life example
Password checkdo {
ask for password;
} while (password is wrong);
You must ask at least once, right?A while loop might not ask at all.