Why it is not work? Cpp | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

Why it is not work? Cpp

Were is the proplem in this code ? https://code.sololearn.com/c9a3KDgJW2sY/?ref=app

27th Nov 2023, 6:42 PM
Yazan Alhallack
Yazan Alhallack - avatar
6 Respuestas
+ 7
Y-Kh first let's not blame python for a c++ code and look at the "forloop" as Lothar mentioned x won't get to 1000 until x reaches 1000 but you forgot curl brackets { after the for conditional iteration for(x=1;x<=1000;x++){ // then you are asking to print "cout" after each iteration... I added the "\n"; to address x on each line until you reach 1000 cout<<x<<"\n"; // We need a closing curl bracket for the "forloop" to properly loop } // Then our return 0; with a closing curl bracket for our main return 0; }
27th Nov 2023, 8:44 PM
BroFar
BroFar - avatar
+ 8
Y-Kh , > the condition in the loop can not get true, so no output is given. probably you mean: ... for(x=1;x<=1000;x++) ^^^ > an other optimization could be to add a space after each number in output: ... cout<<x<<" "; ^^^^^
27th Nov 2023, 6:51 PM
Lothar
Lothar - avatar
+ 7
Rizwan ali your copy and paste web code has absolutely nothing to do with this question, as it is spam.
28th Nov 2023, 10:15 PM
BroFar
BroFar - avatar
+ 4
Bob_Li if they only use it this was for(x=1;x==1000;x++){ } cout << x ; The quick answer is 1
27th Nov 2023, 10:04 PM
BroFar
BroFar - avatar
+ 2
The middle value in the for loop is the condition that should be TRUE while running. The for loop stops when it is False. it is not the target condition. Think of it as a form of while-loop. x==1000 is false (because x=1, and 1==1000 is false). That's why there is no output. x<=1000 is true (because x=1 and 1<=1000 is true) This will make the loop run and increment x until the condition is false. Then you output 123456....1000 The loop stops when the middle condition is false.
27th Nov 2023, 10:00 PM
Bob_Li
Bob_Li - avatar
27th Nov 2023, 9:02 PM
Angelo Abi Hanna
Angelo Abi Hanna - avatar