Q:

Why are braces after a for() loop statement making my code behave different?

#include<iostream>

int main()
{
int n,t,i;
cout<<"input a number"<<endl;
cin>>n;
for(i=1;i<=10;++i)
t=n*i;
cout<<t<<endl;
return 0;
}

here if I input 10 on console screen then the the output I get is just 100,
while below when I input then the I get the whole table of 10 on my console. I don't understand what stopped my above code to show the below output. Was it the return?
#include<iostream>
int main()
{
int n,t,i;
cout<<"input a number"<<endl;
cin>>n;
for(i=1;i<=10;++i)
{
t=n*i;
cout<<t<<endl;
}
return 0;
}

in this I get whole table until 10. Why is that?

A:

If you indent your code properly, you can see the the first for loop only executes one statement:
#include<iostream>
int main()
{
    int n,t,i;
    cout<<"input a number"<<endl;
    cin>>n;
    for(i=1;i<=10;++i)
        t=n*i; // Executed within for loop
    cout<<t<<endl;
    return 0;
}

while 
#include<iostream>
int main()
{
    int n,t,i;
    cout<<"input a number"<<endl;
    cin>>n;
    for(i=1;i<=10;++i)
    {  // Every statement within the scope block is executed in the for loop
        t=n*i;
        cout<<t<<endl;
    }
    return 0;
}

Any conditional or loop construct (if,else,for,while,do) followed by a single statement will only execute that statement, if it's followed by a scope block ({}) all statements within the scope block will be executed.

A:

for(i=1;i<=10;++i)
t=n*i;
cout<<t<<endl;

is equivalent to:
for(i=1;i<=10;++i) {
    t=n*i;
}
cout<<t<<endl;

so, the first snippet computes t = n * i 10 times, but prints only last evaluation of t due to cout<<t<<endl; that is placed outside the loop. 

