Read the description(C++) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Read the description(C++)

Let's say I have a for loop: for(i=0; i<5; i++) In the for loop I have a while loop: while(condition == true) How can I break ONLY from the while loop?

19th Jul 2018, 10:19 AM
Dror Dahari
Dror Dahari - avatar
3 Answers
+ 1
Look at this: It only breaks the while loop. The break statement always just applies to the most inner loop, where it was called. int j; for(int i=0; i<10; i++){ cout<<i<<endl; j=5; while(j>0){ cout<<" j: "<<j<<endl; if(j==2){ break; } j--; } }
19th Jul 2018, 10:26 AM
Matthias
Matthias - avatar
0
Put the break statement in the while loop
19th Jul 2018, 10:24 AM
TurtleShell
TurtleShell - avatar
0
Thanks alot! I thought it break from the biggest loop...
19th Jul 2018, 10:28 AM
Dror Dahari
Dror Dahari - avatar