Hello,
I have a test coming up and my teacher is tricky guy he likes to make sure we know about braces. So I have a couple peices of code from a practice test that I would like to get straightened out.
1. What is the output of the following code?
for(int I=1; I > 2 ; I++)
if (6>= 2*3)
cout << "Hello";
cout << "There";
The answer is "There". I would like to know what is the rule for parenthesis if none are included. For instance I know that for an if statement like above only cout << " Hello "; is included. I would like to know what the rule is for "for loops". Thanks.
I would really like to know what is the way to use parenthesis when none are given in all cases because my teacher gives us code with no parenthesis so I have to know the default of how to assign a code parenthesis so I can know how to walk through it.
2. The following loop is supposed to read some numbers until it reaches a sentinel (in the case ,-1). It is supposed to add all of the numbers except for the sentinel , but it fails to do so. Correct it [Use this data if you wish, 12 5 30 48 -1]
int total =0, count =0, number =0;
do
{
cin>> number;
total =total+number;
count++;
}while (number != -1);
The solution we arrived at in my class was to place the cin >> number; before the do loop. Or another solution was to place cin >> number after total.
So if anyone can explain why this is it would be appreciated.
Thanks,
Thanks I have one more that is confusing the sh*t out of me regarding the braces I cannot quite figure out.
Here it is
What is the output of the following nested loop?
for(row=3; row >=1; row--)
for(col=1 ; col<= row; col++)
if(col <= 2)
cout << col <<'\t';
else
cout << row << '\t';
cout << endl;
I'm really having a hard time walking through this one. This is how my teacher gave it to us. So I can figure out how to walk through this without the computer and this statement is confusing to try and figure out where the braces go. Any help ? Thanks.
Then, if you take care with you indenting, remembering for and if only control the following statement or block statement (and there are no block statements here)