[CSI] CS1A Practice Exam
Mikhail Nesterenko
mikhail at cs.kent.edu
Thu Oct 12 23:32:05 EDT 2017
> Hello Dr.Nesterenko
>
> I was looking at the practice exam and noticed something on question
> 4. Shouldn't the exact output for the code when break is in it be 1 2
> 3 4 5 6 because of the cout after the body of the while loop?
>
indeed it is. The output is
1 2 3 4 5 6
I fixed the answers. Thanks,
--
Mikhail
>
> 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.
>
> break - immediately exit the innermost loop
> continue - ends the execution of the body and re-evaluate the loop expression
>
> The above code increments i then checks if i is greater than 5 and break out
> of the loop if it is. The prints i again. The output is
>
> 1 2 3 4 5
>
> If break is replaced by continue, the output would be
>
> 1 2 3 4 5 10
More information about the cs13001
mailing list