Short Simple Exercise Please Explain Me

Hello. I dont understand this program. Even if I make changes in him , I still dont understand . Can u please explain it to me ...please.

1
2
3
4
5
6
7
#include<iostream>
using namespace std;
int main(){
int i,t=4;
for(i=3;i<=7;i++) t=t+i;
cout<<t;
}


The output result is 22 , and i DONT understand why .
Since the output is 29, I guess that would be confusing.

Perhaps you mis-read the <=7 as <7.

Anyway, to resolve confusion, add some debug printing.
1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
using namespace std;
int main(){
int i,t=4;
for(i=3;i<=7;i++) {
    cout << "Adding " << i << " to " << t << endl;
    t=t+i;
    cout << "T is now " << t << endl;
}
cout<<t;
}

sorry here the correct for.

for(i=3;i<=6;i++)
Thank you soo much !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! BE BLESS
Topic archived. No new replies allowed.