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];
}
the question is what is the final value of x, not what is the last value printed out by the loop. if you print 'x' again after the loop ends, you'll see the final value of 'x' is 10
#include <iostream>
using namespace std;
void main()
{
for (int x=0; x<10;x++)
{
cout << x <<endl;
}
cout <<endl<<endl; //show it seperated from the loop output
cout <<"the final value of x is: "<< x <<endl;
return;
}
i believe that if the loop were written
for(int i =0; i<10; ++i)
it would be 10, because it would increment and then test.
but the way harbir has it written, it will fail once i hits 10.
Hey Tedob, I wanted to give you greenies, but the AP system thinks I have been a little to generous to you...you are absolutely correct, the final value of x would indeed be ten, I only considered inside the loop, but now maybe a question...you defined x in the loop...x outside the loop would be zero no? (damn I wish I was at work, would test that quick like...)
/neb
EDIT: undefined is what I meant, it should be undefined...sorry have always associated that with zero.
the loop made the value of x = 10, then failed. the value remains 10 unless you reset it. I ran the code before i posted it, and the value of x outside the loop is 10.
The thing here is that, we are not concerned about the number of times the loop will run, All we are concerned is the final value of X, I have tested it also, The final value of X is 10.
Explanation:
Simple, if the value of X is nine, then the loop will run, it is onlt when X turns to 10 the condition turns False and the control comes out of the loop. Therefore the final value of X is 10.
First even i got confused, But now i am sure, This answer is even right LOGICALLY.
Ok dang it, you made me vpn in and test it :)
int main(int argc,char * * argv)
{
// int ;
int i=0;
for(int x=0; x < 10; x++)
{
printf("Loop i: %i %i", i++, x);
}
printf("Loop i: %i %i", i, x);
}
Using g++
test.c: In function `int main(int, char**)':
test.c:13: name lookup of `x' changed for new ISO `for' scoping
test.c:9: using obsolete binding at `x'
I guess it depends on your compiler, but x should be outside the scope of the for loop; however, depending on how the compiler references/dereferences the variable, the value of 10 was probably still out in the same memory address and x wasn't dereferenced and disrupted but other things writing to the same memory, then you would still get x=10 depending on if your compiler lets you do that...
/neb
Do this in C and ull get the value of x 10 ( after the lop is over ) like all the right ppl above said do this in Java and nebulus is right .....