For loop(HELP)

I did this int x =0; for (int i=10, y=10; i <=50 ; i+=y) { x += i; } Console.WriteLine(x); But the condition didn’t work and want him to give me 50 one time (I know that I can just print it easily but I want to understand the for loop)BTW the output is 150 :(

1/12/2019 10:53:58 AM


13 Answers

New Answer


Hi, D_Stark explained it very well. Aoma, your code should look like this: int x =0; for (int i=10, y=10; i <=50 ; i+=y) { x = i; } Console.WriteLine(x); As you can see i modified only one caracter: before: x += i; after: x = i; You were close, just pay more atention to details. When you write x += i you add the value of i to what value x had before evaluatng the expression. You initialized the variable x with 0 and at after the first iteration of the loop the x stored the value 10. For the second iteration x came with the value of 10, i became 20 and at the end of the iteration x became 30, and so on. Hope I could help shed some light over it, and try to read again D_Stark's answer.


i+=y adds 10 to i each loop x+=i adds the value of i+=y each loop loop0. i=10 y=10 x=10 loop1. i=20. y=10 x=30 loop2. i=30. y=10 x=60 loop3. i=40. y=10 x=100 loop4. i=50. y=10 x=150 loop5; tests condition and exits you test if i<=50 (see loop 4) which is true so the value of x=150 is printed to console Can you see were you went wrong?


Yeah but when the value of i reach 50 it should stop the loop and then the x value will be 50 and then i can print it right ? or it is just i did not understand the for loop.


I amended my question maby you can understand that better? remeber += adds the value to is current value so if x = 10 and you use x+=10 the value of x equals 20


Aoma for (statement 1; statement 2;statement 3) {   // code block to be executed // الكود } Statement 1 is executed (one time) before the execution of the code block. تتنفذ مرة واحدة قبل ان يتنفذ الكود Statement 2 defines the condition for executing the code block. يعرف شرط لتنفيذ الكود Statement 3 is executed (every time) after the code block has been executed. يتنذف في كل دورة بعد ان يتنفذ الكود في البداية.. قيمة i و y تساوي 10 ثم يتنفذ الكود الي داخل x += 10 الحين صارت x = 10 بعدين.. يشوف الشرط i <= 50 طبعا i = 10 يعني بيكمل الـ statement 3 في الـ statement 3 بتضيف 10 لـقيمة i لأن قيمة y تساوي 10 الحين صار معنا x = 10 i = 20 y = 10 بعدين يرجع ثاني ويصير معنا x = 30 ( x + i ) i = 20 y = 10 لحد تصير i = 50 راح يكون x = 150


great !!!


Ok now i understand 🌚 thank you all


Hlo everyone,im here to tell u that there is also one more kind of loop which is the variation of for loop for the iteration of array in c++,see the code below: https://code.sololearn.com/cBqEMh7dECZJ/?ref=app


Hlo Aoma..if u have studied loops then i'll must say u should also know about what r foreach loops..so i have derived foreach loop in c++ using #define preprocessor directive https://code.sololearn.com/cl5dRAoh6kaa/?ref=app


#explained! int x=0; for(int i=10, y=10; i<=50; i+=y) { x=i; } c Console.WriteLine(x);



if you know how many times the loop will iterate for loop will be more effective syntax: for(init;condition;inc/dec) { }


Please i need a personal teacher to help me more understand programming. Any help please will be very grateful.