+ 1

How many times will this loop run?

for(int x=0; x=10; x++){}

19th Jan 2017, 2:02 PM
Waheed Rehman
Waheed Rehman - avatar
14 odpowiedzi
+ 21
infinite
19th Jan 2017, 2:07 PM
Frost
Frost - avatar
+ 19
Its like @Sakshi said.. there isnt a false condition
19th Jan 2017, 3:13 PM
Frost
Frost - avatar
+ 19
The value becomes 10 and does not increment after :/ Try this code in playground and see the output. for(int x=0;x=10;x++){ cout<<x<<endl; }
20th Jan 2017, 3:26 AM
Frost
Frost - avatar
+ 10
the answer is infinite only first X=0 then X gets incremented and X becomes 10 but then also it will go on increasing because in the condition it is mentioned( X=10 )and not (X<10) or ( X >10 )or( X<=10)or (X>=10)
19th Jan 2017, 2:29 PM
Sakshi👸
Sakshi👸 - avatar
+ 7
no no it will check X=0 then inside the loop see the incrementation X++ X increments up to 10 then it goes on incrementing because in condition it is not mentioned that X should be less than 10 or greater than 10 and the condition will never be false see the condition again it's mentioned X=3 and not X==3 , that means X has been just assigned the value 3 , this will always evaluate to true and hence the loop will run for ever
19th Jan 2017, 2:41 PM
Sakshi👸
Sakshi👸 - avatar
+ 7
thanks for the appreciation😊
19th Jan 2017, 3:36 PM
Sakshi👸
Sakshi👸 - avatar
+ 5
re: @Sakshi, S!d (for later visitors) int x = 9; if(x=10) cout << "true" << endl; else cout << "false" << endl; output: true Get bitten by this error enough times and you'll develop an uncanny instinct for conditionals vs assignment.
19th Jan 2017, 8:36 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
thanks SakshiGupta you made my day. this was one new thing in c++ i learned from you. i will follow you.
19th Jan 2017, 3:31 PM
Waheed Rehman
Waheed Rehman - avatar
+ 2
@S!d how?? condition is not true
19th Jan 2017, 2:13 PM
Waheed Rehman
Waheed Rehman - avatar
+ 1
@HashimNabreez how?? see the condition.
19th Jan 2017, 2:05 PM
Waheed Rehman
Waheed Rehman - avatar
+ 1
@DavidA your answer clicks to my mind. thanks. but i couldn't remember that error was an option in challenge.
19th Jan 2017, 2:22 PM
Waheed Rehman
Waheed Rehman - avatar
+ 1
@SakshiGupta loop will first initialize x=0 then will chek the condition which is x=10 which is not true. isn't the loop will be terminated at once??
19th Jan 2017, 2:35 PM
Waheed Rehman
Waheed Rehman - avatar
+ 1
3
11th Jan 2021, 12:04 PM
Fredrick Brighton
Fredrick Brighton - avatar
0
Error. Condition is not boolean.
19th Jan 2017, 2:20 PM
David A