Just a) c) d) e) Thanks! 1) For the following program segments identify how many
ID: 3582821 • Letter: J
Question
Just a) c) d) e) Thanks! 1) For the following program segments identify how many times each loop will execute? Also identify what is the output in each case. Assume all variables are intergers. Write your answers to the right of each program segment. Good idea to show any applicaple method for calculating. Output int i 2; int temp 1 while (i 6) temp temp (i 1); i i 1; i and t tempo endl; cout "i 2- 3 Lt s temp 3 4 Output int num 0; while (num num 6 num) 2 34 num num 1; cout num cout endli 2 3 4 5 output x 5; y 50: x x 10: while (x y) cout x y endli
Explanation / Answer
a) Here in this while loop it executes the loop untill the condition which is given is true.Whenever the condition is not met then only the it gets out from the loop which is the very next statement to the while loop.
In this given code snippet, i =2 i.e. i is initialised to 2 and in the while loop the given condition is i<6, so it executes the loop untill the condition is false.
Here the while loop is executed for i= 2,3,4,5,6 so totally 5 times the loop is executing and in the 5th time the condition met false which is (6<6) this is not true, so the execution of loop terminated and comes to the very next statement which is 'cout " statement
By this time the i value is 6 so it get printed and temp value will be 24 i.e. here temp = temp *(i-1),
Firtst iteration: i =2, Temp= 1*(2-1)==>1
second iteration i=3, Temp=1*(3-1)==>2
third itration i=4, Temp=2*(4-1)==>6
fourth iteration i=5,Temp=6*(5-1)==>24
fifth iteration i=6, which the condition is false, so execution gets out from the loop and goes to the cout statement
In this the loop will be executed for 5 times,The final out which it gets printed is i=6 and Temp=24.
c) Do while is executed atleast once whether the condition is met or not because first the body of the loop will get executed and then condition is checked.
here x=5,y=50 and in the body x=x+10; the condition is (x>y)
First iteration x=5+10==>15, condition satified again execution goes to the start of loop
Second Iteration x=15+10==>25. condition satified again execution goes to the start of loop
third iteraion x=25+10==>35, condition satified again execution goes to the start of loop
fourth iteration x=35+10==>45, condition satified again execution goes to the start of loop
fifth iteration x=45+10 ==>55, flase or Condition is not satisfied so it gets out from the loop
In this totally The loop will be executed for 5 times and the output will be 55 50
d) This is again the while loop its functionality is explained in the (a) bit.Please go through that, thefor the execution steps see below
in this x=5, y=30, condition is x<=y, in body statemen is x=x*2.
First iteration x=5*2==>10, Condition satified again goes to start of loop
Second Iteration x=10*2==>20, condition satified again execution goes to the start of loop
Third iterstion x=20*2==>40,false or condition not satified so it gets out from the loop
Here in this the loop will be executed for 3 times and the final output it print is 40 30.
e) For loop consists of (initialisation, test expression, updated statemet)
In this the initialisation statement is executed is only once and then test expression is evaluated untill false, when each time test expression is true body is executed and the update statement is updated.
In this code snipped num, i=1 condition is i<5 and the statement in body is num=num*(5-i); The iterarion of loop follows like this
First iteration : i<5 is satisfied then it tests codition: num=1*(5-0)==>5, 5 will print because of cout statement and i will increment to 1 and then execution goes to testcase of the loop.
Second iteration :i<5 is satisfied then it tests condition: num=5*(5-1)==>20,20 will print because of cout statement and i will increment to 2 and then execution goes to testcase of the loop.
Third iteration: i<5 is satisfied then it tests condition; num=20*(5-2)==>60,60 will print because of cout statement and i will increment to 3 and then execution goes to testcase of the loop.
Fourth iteration:i<5 is satisfied then it tests condition; num=60*(5-3)==>120, 120 will print because of cout statement and i will increment to 4 and then execution goes to testcase of the loop.
Fifth Iteration:-i<5 is satisfied then it tests condition;num=120*(5-4)==>120, 120 will print because of cout statement and i will increment to 5 and then execution goes to testcase of the loop.
Sixth Iteration:- i<5 is not satisfied, execution of the loop is terminated because the condition is false.
Here the loop is iterated for 5 times the final output is 5 20 60 120 120 .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.