for loop

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1239 Views - Last Post: 01 January 2019 - 09:19 AM Rate Topic: -----

#1 RyanMco   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 93
  • Joined: 27-December 18

for loop

Posted 31 December 2018 - 08:18 AM

Hi !
can someone please explain to me exactly how the for loop work?! I mean when exactly the "i++" modified? when entering the loop or once returning from the loop it gets modified? also is it first gets modified and then check the condition of for loop or first checking the condition and then adding the "i"?

what's the difference between: "++i", "i++";

also what's the difference between those:
for(int i=0; i<10;i++);
for(int i=0;i<10,++i);
a[i]=a[i++];
a[++i]=a[i];
a[i++]=a[i];
:code:

thanks in advance and much appreciate your help!!

This post has been edited by modi123_1: 31 December 2018 - 09:41 AM
Reason for edit:: In the future please use the [code] tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: for loop

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2706
  • View blog
  • Posts: 4,315
  • Joined: 21-June 11

Re: for loop

Posted 31 December 2018 - 08:35 AM

View PostRyanMco, on 31 December 2018 - 04:18 PM, said:

what's the difference between: "++i", "i++";


Both increment i by one, but ++i evaluates to the incremented value of i, whereas i++ evaluates to the value that i had before incrementing it. When you don't use the value of the expression (e.g. when you use it as an expression statement like i++; on a line of its own or in the third section of a for-loop header), then there is no difference.

Quote

also what's the difference between those:
for(int i=0; i<10;i++);
for(int i=0;i<10,++i);


There is no difference between these two.


Quote

a[i]=a[i++];
a[++i]=a[i];
a[i++]=a[i];


All of those invoke undefined behavior because you're reading and writing i without a sequence point in between.
Was This Post Helpful? 2
  • +
  • -

#3 RyanMco   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 93
  • Joined: 27-December 18

Re: for loop

Posted 31 December 2018 - 08:38 AM

simple, I'm not understanding the concept of for loop.
for example here:
for (int i=0 ;i<10;i++)
{
i+=5;
}

at first iteration i=0 but there's "i++" so i=1, then i+=5 means to add the value of i by 5 so i=6 .. but that's wrong because the compiler showing me different outputs !

any explanation please? what's confusing me, after reading the row code "i+=5" and return to the for itself, will the "i" be increased by value (i+5) +1? meaning after returning to for itself, will it start from the final update of value i?
Was This Post Helpful? -2
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 934
  • View blog
  • Posts: 3,741
  • Joined: 13-June 14

Re: for loop

Posted 31 December 2018 - 08:39 AM

Out of curiosity, how are you learning whichever language (i.e. C or C++)? Most of that is pretty fundamental knowledge and should be covered by a decent book.
Was This Post Helpful? 3
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11374
  • View blog
  • Posts: 19,406
  • Joined: 19-March 11

Re: for loop

Posted 31 December 2018 - 08:39 AM

View PostRyanMco, on 31 December 2018 - 10:18 AM, said:

Hi !
can someone please explain to me exactly how the for loop work?! I mean when exactly the "i++" modified? when entering the loop or once returning from the loop it gets modified?


This is a great question for the experimentalist in you to have a go at. What sort of code could you write that would answer this question for you?
Was This Post Helpful? 2
  • +
  • -

#6 RyanMco   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 93
  • Joined: 27-December 18

Re: for loop

Posted 31 December 2018 - 08:43 AM

Thank you for your help sepp2k, but I'm not understanding when exactly the increment of i happen once returning to for itself, is it before checking the condition of for itself or what? meaning is the pc while returning to for itself, first "increment" i by 1 and then check the condition of for?!

View Postndc85430, on 31 December 2018 - 08:39 AM, said:

Out of curiosity, how are you learning whichever language (i.e. C or C++)? Most of that is pretty fundamental knowledge and should be covered by a decent book.



I just faced this on c programming because I'm programming in that language, but may you tell me the decent book to read this fundamentals?!

thanks !!

This post has been edited by ndc85430: 31 December 2018 - 08:44 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? -1
  • +
  • -

#7 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2706
  • View blog
  • Posts: 4,315
  • Joined: 21-June 11

Re: for loop

Posted 31 December 2018 - 08:46 AM

View PostRyanMco, on 31 December 2018 - 04:38 PM, said:

at first iteration i=0 but there's "i++" so i=1


The first part of the loop's header (i.e. int i=0; in your code) will be executed once before the first iteration of the loop. The third part of the for loop's header (i.e. the i++ in your code) will be executed after each execution of the loop body.

So the first time the loop body is executed, i will be 0 and then 5 after the i += 5; is executed. Only then is the i++ executed for the first time.
Was This Post Helpful? 1
  • +
  • -

#8 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 934
  • View blog
  • Posts: 3,741
  • Joined: 13-June 14

Re: for loop

Posted 31 December 2018 - 08:48 AM

View PostRyanMco, on 31 December 2018 - 03:43 PM, said:

but I'm not understanding when exactly the increment of i happen once returning to for itself


I suggest you do as Jon suggested and write an example for loop with some print statements before, during and afterwards so you can see what's going on.

Quote

I just faced this on c programming because I'm programming in that language, but may you tell me the decent book to read this fundamentals?!


But how are you learning the language in the first place? You must have a book or some learning material. If you need a book, make sure you get something relatively recent covering the latest standard (C11), like this one (I learnt how to program from Horton's books...).

Also, there's no need to quote previous posts in full, since that just wastes space.. If you need to quote, then trim the quote to the minimum you want to reply to. Otherwise, please use the "Reply" button further down the page.

This post has been edited by ndc85430: 31 December 2018 - 09:02 AM

Was This Post Helpful? 1
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6664
  • View blog
  • Posts: 22,744
  • Joined: 05-May 12

Re: for loop

Posted 31 December 2018 - 09:14 AM

*
POPULAR

View PostRyanMco, on 31 December 2018 - 10:18 AM, said:

Hi !
can someone please explain to me exactly how the for loop work?! I mean when exactly the "i++" modified? when entering the loop or once returning from the loop it gets modified? also is it first gets modified and then check the condition of for loop or first checking the condition and then adding the "i"?

I really suggest that you find a book to learn C or C++ instead of taking what seems to us, a random cherry picking approach.

Anyway, a book would have told you that in C and C++, a for loop that looks like this:
for(int i = 0; i < 10; i++)
    printf("%d", i);


is just syntactic sugar for this:
{
    int i = 0;
    while (i < 10)
    {
        printf("%d", i);
        i++;
    }
}


With that expanded version, that should answer your question regarding where the i < 10 happens before or after the i++.
Was This Post Helpful? 6
  • +
  • -

#10 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 934
  • View blog
  • Posts: 3,741
  • Joined: 13-June 14

Re: for loop

Posted 31 December 2018 - 10:42 AM

You should also indent the code within the braces, as it makes it easier to read.
Was This Post Helpful? 1
  • +
  • -

#11 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11374
  • View blog
  • Posts: 19,406
  • Joined: 19-March 11

Re: for loop

Posted 31 December 2018 - 11:35 AM

I suggest you read this essay and use it to help you shape your questions for better results. Its target audience is the user of some open-source project seeking advice, but if you generalize it's very good for anyone seeking technical help. In particular, read the section headed "Be precise and informative about your problem" and think about how you can adapt those suggestions to make your posts draw good and informative answers.

Also, I am tempted to merge this thread with your very similar one about for loops, so do not be surprised if that happens.

On the other hand, thank you for taking up the suggestion of writing code to explore the language behavior you're curious about. That's a good thing, well done!
Was This Post Helpful? 3
  • +
  • -

#12 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,019
  • Joined: 05-December 13

Re: for loop

Posted 31 December 2018 - 12:38 PM

Just to blandly illustrate what was said and what's happening. (int i = 0; sets the var to 0 and thus to enable the loop to execute, i < 10; i needs to be less than 10 for { i += 5; } to execute and increment from the value it currently is + 5, and once this succeeds; i++ will increment by 1. Which brings you to 6, and so bringing you back to i < 10; to check i is not more than 10. And since i only is 6, i < 10; is still true and so { i += 5; } executes again bringing you to i = 11, and so i++ executes again bringing you to 12, and since i is greater than 10 and not less than 10, your condition is no longer true, brining your statement to and end with with i being 12.

This gif will help you grasp it more clearly.

Posted Image
Was This Post Helpful? 1
  • +
  • -

#13 german-one   User is offline

  • New D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 36
  • Joined: 05-August 18

Re: for loop

Posted 31 December 2018 - 01:03 PM

That's what I usually take to explain a for loop.
Attached Image

Can't beat your GIF though :)
Was This Post Helpful? 1
  • +
  • -

#14 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2279
  • View blog
  • Posts: 4,362
  • Joined: 30-May 10

Re: for loop

Posted 31 December 2018 - 11:21 PM

*
POPULAR

It's a troll / help vampire / idiot.

cprogramming.com has nearly 500 posts of nonsense from this mental shrub.
https://cboard.cprog...ipulations.html
Was This Post Helpful? 5
  • +
  • -

#15 RyanMco   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 93
  • Joined: 27-December 18

Re: for loop

Posted 01 January 2019 - 07:07 AM

Not trollig; there's a big differences between trolling and between learning; so please don't be like ...

This post has been edited by Skydiver: 01 January 2019 - 07:19 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2