4.[15] Describe how "break" and "continue" statements work with
looping constructs. Consider the following code fragment:
int i=0;
while (i < 10){
i++;
if (i > 5) break;
cout << i << " ";
}
cout << i;
What out would this code generate? How would the output change
if "break" is replaced by "continue"? Explain your answers.
For both cases write out the exact output.
1.) Break causes the loop to stop, continue jumps back to the top of a loop earlier than normal, which can be used to bypass the remainder of the loop for an iteration
2.)1 2 3 4 5 6
3.)1 2 3 4 5 10
Can you try and describe what actually happens on each iteration of the loop when i > 5, after you've changed it to use continue? Once you've done that, it should be clear why it's behaving the way it does.
If you don't know what "continue" actually does, I'm sure your textbook will tell you.
checked one of my languages. break is for while, do while, for, and switch statements (under case from what i've seen). break should work if within one of the other 4.
#include <stdio.h>
int main()
{
int i = 0;
while (i <= 9)
{
i++;
if (i == 5) {
printf("the value of i is %d\n", i);
printf("breaking from while loop");
break;
} elseif (i == 6) printf("break was not executed");
}
return 0;
}
if() is the one that checks, but while is the function being broken from.
Incorrect. None of while, do while, for, or switch are functions; they are language constructs. Please do some research before making statements like that.
That is incorrect. The term 'function' is a technical one with a specific meaning; you should not just arbitrarily change these definitions or no one will understand you.
arbitrarily is not correct. http://dictionary.reference.com/browse/arbitrarily?s=t
does not fit this definition as to what was done. how did you get arbitrarily?
while is a keyword. kinda looks like a function with (). I stay with keyword now.
while (i < 9)
{
// statements
}
void mult_by_three(int x)
{
// statements
}
cannot deny that functions look similar to keywords with parentheses
I disagree. You have decided of your own will with no legitimate support that since those two look similar and since one is a function, they may both be referred to as such, against the actual definition of the term.
quote:
Zhuge (4103)
I disagree. You have decided of your own will with no legitimate support that since those two look similar and since one is a function, they may both be referred to as such, against the actual definition of the term.
endquote:
i have changed my naming from function to keyword for while, do while, for, switch as keywords. the first 3 are also loops. loops look somewhat like function format as I have shown above. spare me the technical bs. keyword 'while' uses parentheses, has braces sometimes, has statement(s).
actually does look like a function kinda. maybe you cannot see how both look similar. now calling specifically a keyword a keyword and a function a function. hope this ends now.