+ 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.
11 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.
+ 6
Actually, what is the correct answer? True or False
+ 6
Thanks Qwerty, Jaaziel and allđđđ
+ 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.
+ 4
@querty đđ
+ 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}
}
:)
+ 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.
+ 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?
+ 1
if should be used when no. of loops required are known.
while should be used when no. of loops required are not known
0
if should be used when no. of loops required are known.
while should be used when no. of loops required are not known