+ 6

What's the output of this code?

Our teacher told us to find the output of this code in c++, I thought that the output will be: 0 , but the output doesn't apear in SoloLearn. What do you think? This is the code: #include <iostream> using namespace std; int main() { int i; for(i=0;i;0) cout<<i; return 0; }

6th Jan 2019, 10:14 AM
Janna Al-Ward
Janna Al-Ward - avatar
16 Respuestas
+ 4
No output. Because its like int i = 0 if(i) {cout << i;} the 0 = false so the code does nothing
6th Jan 2019, 11:46 AM
John Ds
John Ds - avatar
+ 2
Also your teacher's formating is horrible. I would personally show the for loop body in curly brackets, even if it's only one line to prevent confusion, or at least include a line between the cout statement and return statement.
6th Jan 2019, 10:41 AM
Rain
Rain - avatar
+ 2
Pirate_Programmer Yes, it is fine, but it isn't a matter of "can you." It's a matter of "should you." Can other people reading your code know what's going on? Not including the brackets looks dirty, even for 1 liners, especially in bigger projects, so why adopt the habit? I think it's better to avoid such habits.
7th Jan 2019, 4:53 PM
Rain
Rain - avatar
+ 1
Doing for loop with no curly brackets is fine if it isn't over 1 line of code(I learned the hard way).
7th Jan 2019, 4:47 PM
Pirate_Programmer
Pirate_Programmer - avatar
+ 1
It's True in bigger projects it's best to avoid this, but in this case is a very small program. It isn't something with like over 100 lines of code. But I get what you mean.
7th Jan 2019, 4:56 PM
Pirate_Programmer
Pirate_Programmer - avatar
+ 1
I think it was something like chalenge
7th Jan 2019, 5:00 PM
John Ds
John Ds - avatar
+ 1
Ofc is not good idea to code a big program like this
7th Jan 2019, 5:01 PM
John Ds
John Ds - avatar
0
No output. The program is basically saying "i equals 0. While i does not equal i, print i. Then end the program." Obviously, we can immediately tell that "Hey, i equals i no matter what. That makes no sense." The for loop does not run once. For loops are similar to while() loops in that they check the condition before executing. If the condition is already met, it'll go right over it. Hope this explanation helps.
6th Jan 2019, 10:39 AM
Rain
Rain - avatar
0
No output.instead of using i declare a condition and increment the value..
6th Jan 2019, 3:18 PM
sailakshmi
sailakshmi - avatar
0
John Ds I would say you are wrong on this one. It's not a matter if 0 meanst true or false. This isn't an if statement, this is a for loop. Even if the variable i was 5, it wouldn't run once. For loops don't check boolean.
6th Jan 2019, 4:38 PM
Rain
Rain - avatar
0
It's basically nothing, as there is literally nothing to loop. So it returns 0; (no output).
7th Jan 2019, 4:45 PM
Pirate_Programmer
Pirate_Programmer - avatar
0
Pls i think the c online compiler on sololearn is wrong..dis code is suppose to provide the user with input instead it just produces output of 10 even when d user has not yet chosen his two numbers..#include <stdio.h> int main() { int a, b; printf("Enter two numbers:"); scanf("%d %d", &a, &b); printf("\nSum: %d", a+b); return 0; }
8th Jan 2019, 11:40 AM
Kelvin Erhabor
0
Here: #include <stdio.h> int main() { int a = 0, b = 0; printf("Enter two numbers:"); scanf("%d %d", &a, &b); printf("\nSum: %d", a+b); return 0; } ^ Assign a and b to 0. If you do not, when you enter in nothing, the compiler still prints a and b. There will be random numbers in a and b, because you didn't change then before adding them together. Just a little fact for you. You will 99.99% of the time be the one in the wrong, not the compiler. If this was a common issue, then the programming language wouldn't be exactly functional if it didn't compile right when used as you did. This has to do with memory allocation adn variables. Time to go study, mate.
8th Jan 2019, 1:27 PM
Rain
Rain - avatar
0
Thanks
8th Jan 2019, 7:02 PM
Kelvin Erhabor
0
There is no output in this code because there is no output command in it
9th Jan 2019, 3:07 AM
MichaelNoLimit
MichaelNoLimit - avatar
0
how can we talk about output as 'cout' is placed inside the for loop and for the false value as '0' the loop doesn't run
3rd Mar 2019, 10:12 AM
Parikshit Sharma
Parikshit Sharma - avatar