[SOLVED] While and If Are Same?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

[SOLVED] While and If Are Same??

I played a Java challenge in my phone. The question: Are the codes equivalent? while(statement){ System.out.println("SL")!} if (statement){ System.out.println("SL")!} The correct answer shown in my phone is TRUE. However, when I encountered this question in my father's phone, the correct answer shown in that phone is FALSE.

15th Dec 2017, 4:23 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
10 Answers
+ 21
They're two different questions. The first one: Are the codes equivalent? while(statement) { System.out.println("SL"); } if(statement) { System.out.println("SL"); } Answer: false The second one: Are the codes equivalent? while(statement) { System.out.println("SL"); break; // There is a break. } if(statement) { System.out.println("SL"); } Answer: true You should look at the questions carefully next time. Most of the questions look the same but are actually different.
15th Dec 2017, 10:14 AM
qwerty
qwerty - avatar
+ 6
Actually, what is the correct answer? True or False
15th Dec 2017, 4:27 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 6
Thanks Qwerty, Jaaziel and all😂😂😉
15th Dec 2017, 10:15 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 4
It's False. "while" is a loop, "if " just checks once. They'd be the same if there was a "break" into the "while", cause it'd be just once like the " if". My suggestion: Send a mail to Sololearn.
15th Dec 2017, 4:46 AM
jaaxdev
jaaxdev - avatar
+ 4
@querty 👍👍
15th Dec 2017, 10:36 AM
Utkarsh Singh
Utkarsh Singh - avatar
+ 3
qwerty is correct, but Jaaziel Isai explains why. While (statement is true){do this} This is a loop that uses a conditional statement (if is implied in the statement) to determine when to stop (or not, in the case of an endless loop error). As long as the conditional statement is true (x>=100, check===false, a!=b, etc.), the program will do whatever is in the braces. As soon as the statement=false, it will go on to the next piece of code outside the while loop. if (statement is true) {then do this} This is a conditional, not a loop. The statement is checked to see if it's true and then does what's in the braces, or not. This occurs once only. You could nest an if conditional inside a while loop, or a while loop inside an if conditional. Pardon the pseudo-code. :) EX: while (statement){ if (entry=false) {ask for input} else{add answer to database entry} } EX: if (food===raw) { while (food!=cooked) {apply heat} else {turn off burner} } :)
15th Dec 2017, 8:15 PM
Glenn McGrew II
Glenn McGrew II - avatar
+ 2
I just wondering how many people actually moderate challenges and how they prevent duplicating, this may be the case. Or quiz was fixed after someone reported it.
15th Dec 2017, 4:26 AM
Dima Makieiev
Dima Makieiev - avatar
+ 1
I think it is a kind of unclear challenge, if statement and while loop are totally different, but since both statements contain same error on a same string it will give same error code, so what code (error or programming) author ment?
15th Dec 2017, 4:35 AM
Dima Makieiev
Dima Makieiev - avatar
+ 1
if should be used when no. of loops required are known. while should be used when no. of loops required are not known
15th Dec 2017, 4:14 PM
www
0
if should be used when no. of loops required are known. while should be used when no. of loops required are not known
15th Dec 2017, 4:16 PM
www