What is the value of counter at the end of the loops What is

What is the value of counter at the end of the loops? What is the value of counter at the end of the loops? What is the value of n at the end of the loops? What is the value of v at the end of the loops? v = [3:4:14]; forj = 1:2 How many times of \"2\" counter is still > 10\' appear at the end of the loops? counter = 2 while (2\' counter

Solution

Description: Initially counter = 10, the loop variable index can vary from 4 to 13 with an increment of 3. During the first iteration of the ‘for loop,’ the value of the index is 4, which is less than 13 and the loop condition is true. As a result, the loop body will be executed and value of counter decremented by two and become 8. After that counter will be incremented by 3 and become 7. This is a true statement, and hence the body of the loop will be executed again and counter become 6. In the next iteration, the index becomes 10 and counter become 4. In the next iteration, the index becomes 13 and counter become 2. Then the index becomes 16, the loop condition failed and stopped iteration. Hence after the execution of the code counter = 2.

2.Answer:   counter = -24

Description:

LOOP

counter

iteration

Index

CONDITION

BODY

0

Index=index+3

Index <= 13

counter = counter- index

10

1

4

4 <= 13 True

counter = 10 - 4 = 6

6

2

7

7 <= 13 True

counter = 6 – 7 = -1

-1

3

10

10 <= 13 True

counter = -1 – 10 = -11

-11

4

13

13 <= 13 True

counter = -11 – 13 = -24

-24

Exit

16

16 <= 13 False

Will not execute

-24

Hence at the end of the loop counter will be -24.

Description: Initially n =2, The while loop will execute until n>=49. During each iteration of the loop n will be incremented by two. Hence up to n = 48 the loop will execute. When n = 48 the loop condition is true and in the body of loop n incremented by two and become 50. The loop is not true anymore, hence exit iteration. So after execution of the code n will be 50.

Description: initially v is a column vector with three elements, v(1) = 3, v(2) = 4 and v(3) = 14. The loop variable j vary from 1 to 2. During the first iteration the loop modify the first element of v as v(1) = 3*1 = 3. In the next iteration loop modify the second element of v as v(2) = 3*2 = 6. Hence after the execution of the program v will look like v = [3;6;14]

Description: Look at the program and question! The program will never print ‘2*counter is still >10’ , But it will print ‘2*counter is still <10’ three times in the screen ( look at the difference in the </> symbols in the Q & A). Initially counter = 2. 2*2 <10 is true hence while loop print the line ‘2*counter is still <10’ and increment count by 1 and become 3. Again 2*3< 10 is true, so while loop print the line ‘2*counter is still <10’ and increment count by 1 and become 4. Again loop will check 2*4 < 10 ? It is true, Again while loop print the line ‘2*counter is still <10’ and increment count by 1 and become 5. The loop condition is not true anymore ie 2*5<10 is not true, so loop will not execute anymore. Thus the code prints ‘2*counter is still <10’ three times in the screen.

Description: The loop variable ii have to be incremented like 2, 5,8,11. But if statement inside the loop become true when ii =5. The body of the if condition is a break statement, it will terminate the loop immediately and hence ii =5 after the execution.

LOOP

counter

iteration

Index

CONDITION

BODY

0

Index=index+3

Index <= 13

counter = counter- index

10

1

4

4 <= 13 True

counter = 10 - 4 = 6

6

2

7

7 <= 13 True

counter = 6 – 7 = -1

-1

3

10

10 <= 13 True

counter = -1 – 10 = -11

-11

4

13

13 <= 13 True

counter = -11 – 13 = -24

-24

Exit

16

16 <= 13 False

Will not execute

-24

 What is the value of counter at the end of the loops? What is the value of counter at the end of the loops? What is the value of n at the end of the loops? Wha
 What is the value of counter at the end of the loops? What is the value of counter at the end of the loops? What is the value of n at the end of the loops? Wha

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site