### Braces and such

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,
a for statment will loop everything until the next semi-colon. We like to use indenting to demonstrate this:

 ``123`` ``````for (int i = 0; i < 123; ++i) cout << "This will be looped" << endl; cout << "This will not be looped" << endl;``````

 ``123`` ``````if (false) cout << "This statement will be skipped" << endl; cout << "This statement will not be skipped" << endl;``````

 ``1234`` ``````for (int i = 0; i < 123; ++i) if (true) cout << "This statement will be looped" << endl; cout << "This statment will not be looped" << endl;``````
1. Try reading the "Conditional structure: if and else" sections of this tutorial.

Control Structures
http://www.cplusplus.com/doc/tutorial/control/

The start of the section should answer your question about if-statements and {}s, etc. If it's still unclear, come back for clarification.

2. IF you move cin >> number before the loop, doesn't that mean you'll only read one number? And the loop will then add it to total for ever and ever?

Adding cin >> number; both before the do loop and after total=total+number would work.

But I'd prob. just protect the totalling with an if statement (this would need {}s).

Andy
Last edited on
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.

j
It helps if it's formatted.

How to use tags
http://www.cplusplus.com/articles/z13hAqkS/

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)

 ``1234567`` ``````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.e. you indent for one statement after a for or an it.

I personally avoid writing even a simple if-statement or for-loop on one line. That is, I would never write something like

`if(2 < x) biggerThanTwo = true;`

or

`for(int i = 0; 2 > i; ++i) cout << i << endl;`

always

 ``12`` ``````if(2 < x) biggerThanTwo = true;``````

and

 ``12`` ``````for(int i = 0; 2 > i; ++i) cout << i << endl;``````
Last edited on
Topic archived. No new replies allowed.