+ 2

Analyze this program please and explain which is the right one?

For this code below: int main(int argc, char *argv[]) { int i =1; for (;i<7; i+=2) { i = i*i } cout<<i; } Which one is the right one? Remember, the comparation of current counter value to the terminated value I think should be take place at the top inside for (initVal; terminatedVal; incrementInterval) <--- here the comparation takes place. // version #1 int main(int argc, char *argv[]) { int i =1; for (;i<7;) { cout<<i<<endl; i=i+2; cout<<i<<endl; i=i*i; cout<<i<<endl; } cout<<i; } //version 2 int main(int argc, char *argv[]) { int i =1; for (;i<7;) { cout<<i<<endl; i=i*i; cout<<i<<endl; int main(int argc, char *argv[]) { int i =1; for (;i<7;) { cout<<i<<endl; i=i+2; cout<<i<<endl; i=i*i; cout<<i<<endl; } cout<<i; } } cout<<i; } Thank you all.

25th Mar 2023, 2:09 AM
Oliver Pasaribu
22 odpowiedzi
+ 5
Oliver Pasaribu please clarify as it looks like you have 4 different versions as version 2 and what looks like version 3.. and at the top you have a version as well.. This appears confusing to anyone viewing your question.
25th Mar 2023, 2:24 AM
BroFar
BroFar - avatar
+ 5
Oliver Pasaribu First time is one ( 1 ) i=1 Second time is three ( 3 ) i=1 +2 i=3 So it still isn't greater than 7 One more cycle is +2 exit i = 9+2 (11) On the second cycle it hadn't yet become greater than 7 but on the third and final cycle i += 2 = 11 1 inside loop 3 inside loop 11<7 inside loop exit loop
25th Mar 2023, 3:57 AM
BroFar
BroFar - avatar
+ 5
Oliver Pasaribu This produces the same exact .. in your last one you put i += 1 not 2 ( correction ) int i = 1; for (; i<7;) { i = i*i; cout<<i; i = i+2; } cout<<i; } 1,3,9,11 https://code.sololearn.com/c9UUNIEQcoha/?ref=app
25th Mar 2023, 1:33 PM
BroFar
BroFar - avatar
+ 5
Oliver Pasaribu #include <iostream> using namespace std; int main(int argc, char *argv[]) { int i =1; int c = 0; for (; i<7;i+=2) {cout << (i>7?"true":"false"); c+=1; cout << " loop " << c << endl; i = i*i; cout<<i << endl; } cout << (i>7?"true":"false") << " outside loop result " << endl; cout<<i << endl; } https://code.sololearn.com/cwMNe2w06k4y/?ref=app
25th Mar 2023, 2:04 PM
BroFar
BroFar - avatar
+ 5
Tina 🇺🇦🇮🇷 what he I believe is getting at is why when i ( 9 ) is greater than 7 or i>7?0:1 He is not recognizing that i at 9 is not evaluated against 7 yet goes to exp3
25th Mar 2023, 4:06 PM
BroFar
BroFar - avatar
+ 4
Oliver Pasaribu first cycle is i = 1*1 ( i = 1 ) and second cycle is i = 3*3 ( i = 9 ) Remember it will cycle one more time +2 then 11 https://code.sololearn.com/cB16IYaqAkke/?ref=app
25th Mar 2023, 2:52 AM
BroFar
BroFar - avatar
+ 2
Oliver Pasaribu there is a reason that they incuded 'for' in the language structure, so you can write loops easier. You're making it hard for yourself by trying to abuse it, confusing yourself. So: "Which one is right one?" None. You are trying to use 'for' like a 'while' in some cases. It is absolutely possible, but bad practice. BroFar already explained what's going on, I will do once more: in the loop: for(i=1;i<7;i+=2) { i = i * i } when i = 3, 3 is < 7, so you it goes into loop body, which then i = 3 * 3 = 9. loop body is done, so the i+=2 executes, therefore: i = 9 + 2 = 11. loop condition gets evaluated: i < 7 ? , no, i is 11 and 11 < 7 is false so loop body doesn't get execute and final value of i is 11. Now if you're still insisting on using 'for' like that or modify it the way you like, you're welcome to do so if you have a clear vision of what's going on and at the price of bad, hard to read, prone to bug code.
25th Mar 2023, 12:19 PM
Tina
Tina - avatar
+ 2
Oliver Pasaribu BroFar Why you guys are doing this? It's just a simple for loop! 🤦‍♀️🤦‍♀️ for(exp1; exp 2; exp 3) statement if all three expressions are present it will map to exec exp1; eval exp2, if true, do the statement exec exp3; jump to eval. in case of: int i=1; for(i=1; i<7; i+=2) { i = i * i; } The assembly code (gcc att) is: movl $1, -4(%rbp) #int i=1 movl $1, -4(%rbp) #for(i=1 jmp .L2 # jump to eval condition .L2 .L3: movl -4(%rbp), %eax # i imull %eax,%eax # i * i movl %eax, -4(%rbp) # i*i -> i addl $2, -4(%rbp) # i += 2 .L2: cmpl $6,-4(%rbp) # compare i , 6 jle .L3 # jump to .L3 if i was <= 6 That's all and can't be broken to smaller steps. so i is first 1 1 is <= 6 i = 1*1 i += 2 3 is <= 6 i = 3*3 i += 2 11 is not <= 6 end. that's it! 9 never gets into evaluation and can't be the final value of i, and i is 11 because of exp3.
25th Mar 2023, 3:54 PM
Tina
Tina - avatar
+ 1
You both right. I have look at Brofar code. Very good explaination. Brofar use inverted or reverse logic. But I prefer using standard, non-reversing logic to help us understand, especially to evaluate at the point when i+=2 is executed; before or after i=i*i? Everything clear now. Thank you. Oliver Pasaribu #include <iostream> using namespace std; int main(int argc, char *argv[]) { int i =1; int c = 0; for (; i<7;i+=2) {cout << (i>7?"true":"false"); c+=1; cout << " loop " << c << endl; i = i*i; cout<<i << endl; } cout << (i>7?"true":"false") << " outside loop result " << endl; cout<<i << endl; }
26th Mar 2023, 5:10 AM
Oliver Pasaribu
+ 1
All, please read again deitel c++ how to program page 168-169. You will find the answer there. This is clearly explained by Deitel in his book.
27th Mar 2023, 4:46 AM
Oliver Pasaribu
+ 1
Java
31st Mar 2023, 5:44 PM
Jay Patel
Jay Patel - avatar
+ 1
Jay Patel please do not spam and please remove your comment. This question has nothing to do with html tags.
2nd Jul 2023, 3:05 PM
BroFar
BroFar - avatar
0
Brofar: original problem in c++: int i=1; for(i=1; i<7; i+=2) { i = i * i; } cout << i; Brofar, do you think what number is the exact result of this code? Please explain it to me in a detail maneer for each step, per iteration.
25th Mar 2023, 2:32 AM
Oliver Pasaribu
0
Brofar, could you please explain why i become 11 when it goes outside the loop? We can see, when i=1, i=i*i = 1, then this i Will be compared to terminatedValue i<7? 1<7? This Will yield true. Then since q<7; then i=i+2 = 3. When i is 3, it Will execute inside for loop. Next: i= i*i = 3*3=9. At this step, the value of i = 9; then this value i =9 will be compared to terminated value inside the for parentheses. Does 9 < 7, false. 9 > 7. Since the condition is false, then the statements inside for loop bracket Will not be executed. It Will stop iteration and goes out from the scope of for loop. At this point, the value stored in i is still 9. I don't know, if there is a possibility, when i=9 before right }, it stop current iteration and continue next new iteration, so the value of i is not 9, instead, it Will be 3+2 = 5? Brofar?
25th Mar 2023, 3:44 AM
Oliver Pasaribu
0
Brofar your explanation: Second time or secon iteration, i= 3; Test condition, compare to final terminate condition, 3 is less than 7, enter for loop: Inside for loop: i = i*i i = 3*3 = 9 i= 9; do 3,-rd iteration? Test if i<i terminated: This current i < i terminated? 9 < 7? No. False. So it world not enter the for loop. Program transfer control and goes out from for loop. Here, the value stored in i is still 9. So the result must be 9. What do you think? Maybe Joy, rik witkopp, coding for fun, AJ, or anybody can make it clear for us?
25th Mar 2023, 6:00 AM
Oliver Pasaribu
0
Tina, wait,wait a minute. As I explained, at the end of 2nd iteration, the value of i is 9 since 3*3=9. Statement inside for loop i=i*i=3*3=9 is executed. Then, this i= 9 will be tested again to loop-,terminated condition i<7, to make a decision whether next iteration Will be performed. Let's do it. 9 < 7 no, so it Will return false . Since i9<7 is evaluated as false, the logic flow Will not enter the loop repetition again, instead, it jump out from the for loop and executes next statement outside for loop scope, which is first statement after we go out from the loop. Here, the statement is: print(i) and display 9 on the user I/I device or screen. Can you understand my explanation? Is it clear enough?
25th Mar 2023, 1:16 PM
Oliver Pasaribu
0
Tina, Brofar, let test it: once again: int i = 1; for (i=1; i<7;) { i = i*i; cout<<i; i = i+1; } cout<<i;
25th Mar 2023, 1:21 PM
Oliver Pasaribu
0
Tina, Brofar, please test this following two code, you Will understand later by yourself. The problem is timing and sequence, when the statement i+=2 is executed. Check it out and smile:)
25th Mar 2023, 1:33 PM
Oliver Pasaribu
0
Tina, Brofar, please test this following two code, you Will understand later by yourself. The problem is timing and sequence, when the statement i+=2 is executed. Check it out and smile:) // version 1 #include<iostream> using namespace std; int main(int argc, char *argv[]) { int i=1; for (i=1; i<7;) { i = i*i; // increment of i to 2 takes place here // at the end of each single iteration. i = i+2; // can be i++ or ++i. } cout<<i; } // version #2 #include<iostream> using namespace std; int main(int argc, char *argv[]) { int i=1; for (i=1; i<7;) { i = i+2; // can be i++ or ++i. i = i*i; } cout<<i; }
25th Mar 2023, 1:33 PM
Oliver Pasaribu
0
Exactly:) have tested it to find the right explanation. Yes, i= i+2.
25th Mar 2023, 1:43 PM
Oliver Pasaribu