Quick question on continue

Pages: 12
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

why does the continue make it go straight to ten?
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.
it would make whatever i is go to 5. Umm if its just i > 5 wouldn't continue do nothing?

I mean I get what it kind of does just kind of confusing since it can do several things.
@OP,

You actually answered your own question in your original post.
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


The continue statement doesn't do anything to i. What is contained in the "remainder of the loop" that's getting bypassed?
closed account (1CfG1hU5)
https://www.google.com/url?q=http://www.cprogramming.com/tutorial/c/lesson3.html&sa=U&ei=i489VKGnBoX28QWQ7oGQDQ&ved=0CAUQFjAA&client=internal-uds-cse&usg=AFQjCNHTon7yYdkIHPtkcRYv-GSscZBMzQ

shows break; and continue; information loops and case
Last edited on
closed account (1CfG1hU5)
http://www.cplusplus.com/doc/tutorial/control/

another web site
closed account (1CfG1hU5)
break; quits a function, loop, case. continue; exactly what says. if some
statement is true or not true, then keep going.

updating:

break quits loops and switch functions. switch() and case inside. while, do while, for loops.

more information below.
Last edited on
Ah cool thanks guys!
break; quits a function

Incorrect. break does not quit a function.
closed account (1CfG1hU5)
if (i != 1)
{
// statement(s)
break;
}

you're saying the function would keep going?

I will try to remember if() is not a function


update above and below.

while, do while, for loops, and switch() functions are broken from, if() typically used to qualify yes or no to the break.
Last edited on
You have no function in that code snippet. The break statement doesn't interact with if statements either, only loops.
closed account (1CfG1hU5)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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;
     } else if (i == 6) printf("break was not executed");

 }
return 0;

}


if() is the one that checks, but while is the function being broken from.

Last edited on
closed account (1CfG1hU5)
.....
Last edited on
Incorrect. None of while, do while, for, or switch are functions; they are language constructs. Please do some research before making statements like that.
closed account (1CfG1hU5)
while is a keyword in my dos language. has () to use lines below loop, if not considered a function, looks like one.
Last edited on
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.
closed account (1CfG1hU5)
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

Last edited on
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.
how did you get arbitrarily?


You are going to argue semantics for his choice of words, but then immediately argue against that for your usage of the word function?

cannot deny that functions look similar to keywords with parentheses


Sure a novel and an instruction manual both have covers and words on the inside, but surely they aren't both novels?
closed account (1CfG1hU5)
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.
Last edited on
Pages: 12