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
2 ответов
+ 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
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