break statement let u come out of the for loop only...or is it applicable in the while and other loops also? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

break statement let u come out of the for loop only...or is it applicable in the while and other loops also?

13th Sep 2019, 2:48 PM
palak srivastava
palak srivastava - avatar
6 Answers
+ 3
Sure, you can use it anywhere. Although in a while loop you would (usually) terminate the loop when it hits a certain condition
13th Sep 2019, 3:08 PM
HNNX 🐿
HNNX 🐿 - avatar
+ 2
Yes, even the while, and even switch etc.
13th Sep 2019, 2:52 PM
KfirWe
KfirWe - avatar
+ 2
ok
13th Sep 2019, 2:53 PM
palak srivastava
palak srivastava - avatar
+ 2
p_palak When the compailer read the break statement he at the same time exit the loop without doing anything else than exit the loop.
13th Sep 2019, 3:17 PM
KfirWe
KfirWe - avatar
0
And what if the loop contains an if-else bock? Will it come out of the if-else block or the loop?
13th Sep 2019, 3:11 PM
palak srivastava
palak srivastava - avatar
0
okay..... thank u so much
13th Sep 2019, 3:19 PM
palak srivastava
palak srivastava - avatar