The following question $i=1; do{echo"hello";}while(i<0); how does this code print hello once? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

The following question $i=1; do{echo"hello";}while(i<0); how does this code print hello once?

I think the right answr is none coz given that I=1 and I<0 is the condition (while loop) so it is false as 1!<0.please help me anyone..

7th Nov 2015, 1:13 PM
Metaphor_
Metaphor_ - avatar
7 Answers
+ 4
@NihalFarhan, the do...while loop will always execute the function one time before checking the while logic. So it always occurs, even if the first evaluation is false.
13th Dec 2015, 7:39 PM
Brian Fry
Brian Fry - avatar
+ 1
@NIHAL In programming Language code is always tested from top to bottom. Hence, first the echo command will be executed and later the condition will be checked. This is the reason why we call this exit_control loop.
31st Mar 2016, 3:27 AM
Ajay kumar
Ajay kumar - avatar
0
No, correct answer is " once " because it is already said--> echo "hello",so echo command wil be executed and you wil get hello once but with do and while, hello wil not anymore be executed because 0 is not greater then 1
9th Dec 2015, 7:34 PM
zile
zile  - avatar
0
the statement in do will execute once before it check in while operation
28th Dec 2015, 2:30 AM
Agung Kharisma Sukarno
Agung Kharisma Sukarno - avatar
0
Because no matterwhatthe code always runs at least once, even if false
11th Mar 2016, 3:35 AM
Matthew Wegenke
Matthew Wegenke - avatar
0
as name says, first do whatever the statement is then check the condition block.
2nd May 2016, 4:16 AM
Inder Sav
Inder Sav - avatar
0
when do...while loop runs for the first time, it doesn't check the condition bcs it always check the condition at the last of do... while loop. That's why the code inside the this loop will be expected at least once.
26th Dec 2016, 9:59 AM
Raniket Ram
Raniket Ram - avatar