Doubt in for loop

why this line is causing error… the expression sqrt(n) will be calculated just once right?
in python (2nd link) it doesn’t raise any error !

hello @prerak_semwal
ss

here u r dividing n by 0 (i=0 in starting) thats why it is giving arithmetic expection error.

@aman212yadav

but why the loop not run for 10 times like in python

bro why will it excute? it will stop at the first step only becuase u are diving a number by 0. because of which the system will throw exception

in python u are starting from 1 onwards but here it is from 0.

@aman212yadav
I mean if we change i to 1 then it’s not running 10 times …unlike python

use <=


@aman212yadav

how would that matter…the loop still doesn’t run for 10 times (in contrast to python)

use while loop in python if u want similar behaviour.

in python the start and end of iteration get decided at the beginning of first iteration itself after that it does not change(evidentn from the value of i itself).

what do you mean by above ?

So in c++ at every iteration the ending condition is re-computed right? and then the variable i is updated (i++)

yeah …the condition get reevaluated after every iteration in c++.

i am saying by looking at the value of i , we can say that division of n has no impact on number of iteration