0
Can i use while loop within if loop? Is it possible to hav 2 dfrnt loops n a singl prgrm...if so please xplain me with an eg
4 Réponses
+ 9
You certainly can! One of the nice things about program block structure is that it builds upon itself.
You can have nested loops, with some ifs and/or switches scattered throughout. It's all about how you need your program to flow.
One caveat, however: it's often a good idea to take some of that structure and make a function out of it, for clarity.
As for examples, there are tons out there. Any of the while(true) loops with an if or switch to break out is a commonly written one. Sounds of my recent demo programs would potentially help too...
https://code.sololearn.com/cBQC2WF3dl32/?ref=app
https://code.sololearn.com/cjgi3r63MG3G/?ref=app
+ 3
What do you mean by "if loop"?
+ 1
Yeah you can use loops however you want, just be careful to make sure your while loops have an ability to end somehow. For example
c++:
If ( x == 5 ){
while (x <10) {
++x;
}
}
Don't lock your program by using bad logic like this next example unless your program functions by an endless loop
If (x == 5){
while (x >= 5){
++x ;
}
}
0
general if loop in c++ @Rrestoring faith