+ 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; }
16 odpowiedzi
+ 4
No output. Because its like
int i = 0
if(i) {cout << i;}
the 0 = false so the code does nothing
+ 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.
+ 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.
+ 1
Doing for loop with no curly brackets is fine if it isn't over 1 line of code(I learned the hard way).
+ 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.
+ 1
I think it was something like chalenge
+ 1
Ofc is not good idea to code a big program like this
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.
0
No output.instead of using i declare a condition and increment the value..
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.
0
It's basically nothing, as there is literally nothing to loop. So it returns 0; (no output).
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;
}
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.
0
Thanks
0
There is no output in this code because there is no output command in it
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