+ 4

I have a doubt in code, according to my code's output? In C

Please help me to understand the output of this code...... https://code.sololearn.com/cL2f460tn8dq/?ref=app

27th Nov 2020, 12:26 PM
Yogeshwaran P
Yogeshwaran P - avatar
5 odpowiedzi
+ 7
when, j=0, k=0 count=1 and the condition becomes true ,so the second loop breaks. So then j becomes 1 k again becomes 0 and continue until k=10, count increases 10 then j becomes again becomes 2 and again k becomes 0 and continue until k=10 so if we calculate, the total count will be: 1+10+10+10+10+10+10+10+10+10=91. so the answer is 91 Edit: if you want the answer 1 then you have to again rewrite this condition(given below) in the first loop and have to predefine the j and k . if(b[j] == a[0] && b[k] == a[1]){// break; } Then the code would be like that: https://code.sololearn.com/cBaYqWWsQcO9/#c
27th Nov 2020, 12:38 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 7
If you write ++counter inside the if statement then your output will be 1
27th Nov 2020, 12:43 PM
Hrutik Bhalerao
Hrutik Bhalerao - avatar
+ 5
in short you're only break the inner loop in its first iteration. to get the result of 1, you'll also need to break the outer loop
27th Nov 2020, 12:41 PM
Rei
Rei - avatar
+ 5
The code is equivalent to int counter = 1; for(int j=1;j<10;j++) for(int k=0;k<10;k++) ++counter; printf("%d",counter); Effectively it counts 1+9*10 = 91.
27th Nov 2020, 12:43 PM
Brian
Brian - avatar
+ 5
Thank you very muchHrutik Bhalerao ,Rei ,Zatch bell and Brian for clearing my doubt...😃
27th Nov 2020, 1:19 PM
Yogeshwaran P
Yogeshwaran P - avatar