1. What is the final value of x when the code for(int x=0; x<10; x++) is run?
A. 10
B. 9
C. 0
D. 1
Printable View
1. What is the final value of x when the code for(int x=0; x<10; x++) is run?
A. 10
B. 9
C. 0
D. 1
x will hit ten and the for loop will exit.
-edit- Sorry didn't respond to the question in the title. . . .
for(x=0; this part initializes x at zero to ctart counting.
x<10; this part will make the loop stop the count at ten.
x++;) this increments 'x' by one each time through the loop.
Is this a test??? :-) Yes... x increments to ten.
I'm not sure why you are asking (all questions are good, though). Anyway, the for statement is an important concept. I use it in virtually everything I write.
Oh! You asked why?? You have i initialized as int. Beginning at zero it will increment by one every time the loop is executed until i is no longer less than 10.
Technically though since x < 10 isn't the final value 9?
10 isn't < 10 therefore ten definately can't be the answer.
I'd say the answer's 9
Yeah but it goes through the loop one more time, I think, to check the value of 'x' one more time, it won't fail the x<10, until it is ten so, ending value of x is 10.
Yes the answer will be 9.
The for statement:
for(initialization; test; update) //notice there is no ; at the end of the for statment
{
statment;
statment;
//place as many statments here
//as needed.
}
This is the for loop. An int can only hold a whole number value between:
-2,147,483,648 and +2,147,483,647.
This type of variable takes up 4 bytes of memory.
What happens if you go over or under this number? BUFFER OVERFLOW!! heh.. just figured I would throw that in, since everyone has heard of a buffer overflow but not all understand what it really is.
Wouldn't x have to actually come to a point where it fails the X<10, though?? I mean if it stops at 9, it would still be true, wouldn't it??
_edit, ah wait I see, it fails at the test and is never incremented, heh heh.
Well, it would try 10,but when it does in a much less verbose fashion it says HEY JACKA55 10 isn't a logical value because it's not less than ten. Since the value 10 is False it has no value in the expression.
In the x++ part, that is essentially executed at the end of the loop, so if you look at what happens when X=9, it gets to the end of whatever you have in there, increments x, goes back to the conditional statement X < 10, which it isn't, so it goes on to whatever is after the for.Quote:
1. What is the final value of x when the codeis run?
A. 10
B. 9
C. 0
D. 1
Just a quick prog to show:
int i=0;
for(int x=0; x < 10; x++)
{
printf("Loop i: %i %i", i++, x);
}
Gives:
Loop i: 0 0Loop i: 1 1Loop i: 2 2Loop i: 3 3Loop i: 4 4Loop i: 5 5Loop i: 6 6Loop i: 7 7Loop i: 8 8Loop i: 9 9
/nebulus
Doh! You guys are right. The statement only executes for x LESS THAN ten! True it will go through the loop that tenth time - but, like you said, it won't actually cout ten.
OK! OK! I feel like I'm on a merry-go-round.
Tedob1, that might explain why this chunk of a program I'm turning in tonight works!!! I had the right answer....then starting reading the posts, got confused, then wondered: "If I am wrong, why does my code work??" Geeeez...I'm getting confused. :-) No more C posts for me!
Ahhhh...but you know I am kidding.
str.testScoresPtr = numTestScores;
for (int i = 0; i < numStudents; i++)
getStudentData(&str, x);
}
void getStudentData(StudentRecord *s, int scores)
{
float average;
cout << "Student name: ";
cin.ignore();
cin.getline(s->studentName, 80);
cout << "Student ID: ";
cin.ignore(); // To ignore leftover newline
cin >> s->studentID;
cout << "Please enter test scores: " << endl;
for (int i = 0; i < scores; i++)
{
cout << i+1 << ":";
cin >> s->testScoresPtr[i];
}