1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

For loop

Discussion in 'Microcontrollers' started by be80be, Aug 7, 2017.

  1. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    If i use a for loop my C not that great lol but if I figure this out I think i got it
    Code (text):


    for (; ;) {
            for int (i = 0;  i< 100 ; i++) {
           }
         return 0;   // this starts all over?  wrong or Right?
    }
     
    I can't find much how it works in xc8 guess I could just try it lol
     
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,015
    Likes:
    317
    Location:
    Brisbane Australia
    You need the int inside the brackets.
    for(int i=0;i<100;i++){

    You probalby want to change it to unsigned int so it's quicker.

    Edit, you do not want to return from main.

    Mike.
     
  3. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    This does what I think it start the loop all over
    Code (text):

    void main(void) {
        OSCCON = 0b01110110;
        for (; ;) {
         
            for(unsigned int i = 0;  i< 100; i++) {
           }
           return; // this starts all over?  wrong or Right?
    }
     
    }
     
    I was messing with some LCD code that had returns in it that was stopping it in one loop
    Been reading up and there not much about using it in a for loop
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn

    Mike I'm using mplab X v3.65 and the watch variables but it only updates if I stop it.
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,159
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    The for( ; ; ) is a forever loop... The return will reboot the system when i = 100 ...
     
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    I no about the for ( ; ; ) loop what I couldn't find is how return is working there nothing really about it in any thing I found on the xc8 nothing showing it used with a for loop .
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,159
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    I'm just saying... The return is ending the program.. On a PC the program will return to the opsys...
     
  9. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    What's happening is like you said the code runs to the return and the pic reboots cause as soon as it hits it the messes up the LCD
    I guess it's a poor way to restart main.
    But I don't no enough about xc8 LOL
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,159
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    It shouldn't be there.... It needs to be removed..
     
  11. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    In a if statement return a value but in the for loop it's just a loop so I couldn't figure why a return was being used.
     

Share This Page