Is it possible to include a while loop in a for loop as a nested loop? If so,I need help with explanations and examples please? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Is it possible to include a while loop in a for loop as a nested loop? If so,I need help with explanations and examples please?

Nested loops

22nd Apr 2020, 2:54 AM
Ronald Nkhoma Jr.
Ronald Nkhoma Jr. - avatar
2 Antworten
+ 3
Let see it , char *arr[10]= "sololearnl"; int i,j=0; for(i=0;i<10;i++ ) { while(arr[i]=='l') j++; } This fragment of code will count how many times letter l has appeared in above string, with use of for and while loop....... https://www.sololearn.com/discuss/1630088/?ref=app https://www.sololearn.com/discuss/1108882/?ref=app https://www.sololearn.com/discuss/1345746/?ref=app https://www.sololearn.com/discuss/444974/?ref=app https://www.sololearn.com/discuss/19885/?ref=app
22nd Apr 2020, 3:02 AM
Raj Kalash Tiwari
Raj Kalash Tiwari - avatar
0
Yes but why? While loops are used when you dont know how many iterations to make. For loops are for when you know exactly how many iterations to make The only reason i can see is too make cool looking prints... for i in range(20): while i < 10: if i == 9: print(i,'\n') else: print(i, end=' ') i += 1 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 3 4 5 6 7 8 9 4 5 6 7 8 9 5 6 7 8 9 6 7 8 9 7 8 9 8 9 9
22nd Apr 2020, 3:18 AM
Slick
Slick - avatar