0

Plz give me step by step execution of pgm given

main() { int i=1; for(;i<6;I+=2){ i=i*i; cout<<i ;} }

9th Jan 2017, 6:34 PM
Sachin bhosale
Sachin bhosale - avatar
10 Réponses
+ 3
In the 'for' parenthesis you write: (;i<6;I+=2) I guess the lowercase "L" is a typing error? And we must read "i"... else your loop is an infinite loop, because the condition ( i<6 ) is always true: in the body loop, only the i=i*i; is apt to modify the value, but don't ever change since is initialized with the value of 1 ( and 1 times 1 equals... 1 ). So, if the "i" is really updated in incrementation part of the 'for' parenthesis: - on start of 2nd iteration, i=1+2 == 3 ( at end of the 1st iteration ), so condition still true, - loop is executed: i=3*3 == 9, '9' is printed. - end of the loop, i=9+2 == 11 - start of 3rd iteration, i == 11 < 6, so loop is no more executed
9th Jan 2017, 6:55 PM
visph
visph - avatar
+ 3
Yes, on first iteration i value is 1 and is printed. Print don't append a new line to the output, so the output is well 19 ( not '19', but '1' nexted by '9' ;) )
9th Jan 2017, 6:59 PM
visph
visph - avatar
+ 1
thank you sir... I don't know ur name but thanks really
9th Jan 2017, 7:09 PM
Sachin bhosale
Sachin bhosale - avatar
0
first of all you assign value 1 to i when it entered in the loop compiler will check that the value of i is less than 6 or not. if it is less tan 6 then the loop run.....and i will be multiply with i but if it is equal to or greater than 6 then it exit the loop means the loop runs again and again until the value of 6 is less than 6
9th Jan 2017, 6:39 PM
Jonty Bamrah
0
I know the first iteration plz explain another iteration
9th Jan 2017, 6:40 PM
Sachin bhosale
Sachin bhosale - avatar
0
I want step by step evaluation of this pgm I tried hard but getting difficult to find
9th Jan 2017, 6:42 PM
Sachin bhosale
Sachin bhosale - avatar
0
hey in for loop did u write l+=2 or i+=2
9th Jan 2017, 6:46 PM
Jonty Bamrah
0
yes it's i+=2
9th Jan 2017, 6:53 PM
Sachin bhosale
Sachin bhosale - avatar
0
yep thats my ans also but it produce 19 as answer
9th Jan 2017, 6:57 PM
Sachin bhosale
Sachin bhosale - avatar
0
if u excute this code it produce 19 As an answer
9th Jan 2017, 6:58 PM
Sachin bhosale
Sachin bhosale - avatar