I was curious about what would be default nesting in the body of a for loop and while loop in cases with no brackets.
I.E.
while (condition) //without brackets
if (condition)
else
and
for (condition)
if (condition)
else
versus
while (condition)
for (condition)
if (condition
else
I know that for loop will nest an else if there is an if within it's body whenever there is no bracket presented. What happens with a while loop? Would it be the same? And also, including a while and a for loop within the condition? Would it turn out something like
while (condition) {
for (condition) {
if (condition)
else
} // for
} //while
What happens if there was another set of if and else in the for loop? Would it just be ignored into
for (condition)
if (condition)
else
if (condition)
else
Is there any other case I should watch for?
while (condition)
applies to the next statement. Often we use a compound statement, i.e., a line or more of code enclosed in curly braces:
while (condition)
{
/* code goes here */
}
but the statement doesn't have to be a compound statement:
while (condition)
std::cout << "I'm still in the condition.\n";
and sometimes the statement can be more complicated:
while (condition)
if (some_other_condition)
/* some code here */
else
/* some other code here */
and the statement can be a looping statement:
while (condition)
while (another_condition)
while (yet_another_condition)
/* whew, we got here! */