Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Indicate whether the following statements are true (T)or false (F). A break stat

ID: 675432 • Letter: I

Question

Indicate whether the following statements are true (T)or false (F). A break statement is used to immediately exit from a loop. The continue statement forces the next iteration of a loop. Find the value of sum after the following cod is executed for values of x as follows: 3, 7, 5, 11, 9. What would be the value of sum if we use a "continue" statement rather than a "break" in problem 2. What is the value of count after the nested for loops are executed: Determine the number of time that the for loop is executed

Explanation / Answer

1)

a) true

break is used to come out of for loop

b) true

continue statement is used to skip the execution to next iteration

statements after continue will not be executed in that iteration

2)

for(k=1;k<= 5 ; k++)

{

cin>>x;

if(x>10)

{

break;

}

sum=sum+x;

}

cout<<sum;

for each value x <=10 for loop is executed once and x is added to sum (k=1,2,3,4)

x = 3 sum = 3

x= 7 sum = 10

x = 5 sum = 15

x = 11 break;

output = 15

3)

if break is replaced with continue

x = 3 sum = 3

x= 7 sum = 10

x = 5 sum = 15

x = 11 continue sum=15

x= 9 sum = 24

output = 24

4)

for each value of K(-1,0,1,2,3) it nested for loop is executed once and

for each execution of nested for loop count is incremented by 3

hence after execution of nested for loops

count will be incremented by 15

hence count = 10 + 15 = 25

5)

for(k=-2;k<=15;k++)

will be executed 18 times (-2, -1, 0, 1-15)