use of for loop in a code

Discussion in 'Programmer's Corner' started by ect_09, Oct 29, 2014.

  1. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    i want to run this code with for loop instead of
    Code (Text):
    1. a=(++a)%10;
    Code (Text):
    1. #include<htc.h>
    2.  
    3. __CONFIG(1,OSCSDIS & HSPLL);
    4. __CONFIG(2,BORDIS & PWRTDIS &WDTDIS);
    5. __CONFIG(3,CCP2RC1);
    6. __CONFIG(4,LVPDIS & STVREN);
    7. __CONFIG(5,UNPROTECT);
    8. __CONFIG(6,WRTEN);
    9. __CONFIG(7,TRU);
    10.  
    11. #define _XTAL_FREQ   20000000
    12.  
    13.  
    14. void led_display(char a)
    15. {
    16. switch( a)
    17.  
    18. {
    19. case 0: PORTB=0x01; break;
    20. case 1: PORTB=0x02; break;
    21. case 2: PORTB=0x04; break;
    22. case 3: PORTB=0x08; break;
    23. }
    24.  
    25. void main()
    26. {
    27. TRISB=0x00;
    28. char a;
    29.  
    30. while(1)
    31. {
    32. led_display(a);
    33. __delay_ms(1000);
    34. //for(a=0;a<5;a++);
    35. a=(++a)%10;
    36. }
    37.  
    38. }
    please help me in this way..
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    What are you trying to do?
     
  3. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    i want to just turn on PORTB PIN 0,1,2,3 one by one.
    i just tried for practice to use switch .

    Thanks...
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Then you should probably put the code, delay, and display into a for loop.
     
    ect_09 likes this.
  5. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    i tried this but not working
    Code (Text):
    1. while(1)
    2.  
    3. {
    4.     for(a=0;a=5;a++)
    5.     {
    6. led_display(a);
    7.  
    8. delay_sec(1);
    9. }
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    ......not working how?
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    for(a=0;a=5;a++)
    a=5 is not a comparison...
     
  8. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    This is not a good code:

    Code (Text):
    1. a = (++a)%10;
    ANSI C doesn't say when ++ operation is to be performed. So, depending on the compiler, it may work, or it may simply do ++a.

    You probably want:

    Code (Text):
    1. a = (a+1)%10;
    Or, more efficiently

    Code (Text):
    1. if (++a > 9) a = 0;
     
  9. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    thats really just a no-brainer, a is incremented, then the while loop is repeated.

    I use
    Code (Text):
    1.  
    2.  a++;if(a>9)a=0;
    3.  
    while i dont know what %10 is doing I guess its something similar.

    You dont normally want to use a for loop since you want to repeat it all over,
    A for loop could be used but its more akward.

    It is a Modulo Assignment by the way
    http://en.wikipedia.org/wiki/Operators_in_C_and_C++
     
  10. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Doesnt it? I was thinking LS increment is always executed before Operand is evaluted further.
    It is enforced too with ();

    It is an arithmetic prefix increment operator; () overrides operator precedence, which is higher anyway than Modulo assigment (Compound assignment operator).

    Dont know if
    Code (Text):
    1. ++a%10
    can be compiled; doesnt look good however.

    Thats the equivalent of a modulo assignment, but takes a branch, some CPUs may have a modulo instruction or the compiler may have a means to resolve it without taking a branch.
     
  11. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    The operator precedence (along with parantheses) only determine what operands the operantion is applied to. They have nothing to do with when the actions are performed.

    For example.

    Code (Text):
    1. a = b++;
    There are two ways to it can be executed depending on the operator precedence.

    This is the correct way, because ++ has higher precedence than =.

    Code (Text):
    1. a = (b++);
    This is an incorrect way (and woouldn't compile anyway):

    Code (Text):
    1. (a = b)++;
    With the correct precedence, two actions are to be performed:

    Code (Text):
    1. a = <original value of b>;
    2. b = b + 1; // ANSI C calls this a side effect
    C standard doesn't say it what order they should be performed. So, the compiler is free to do

    Code (Text):
    1. b = b + 1;
    2. a = <original value of b>;
    It is up to the programmer to make sure these two don't interfere with each other.

    I don't know of any CPU that would execute division faster than a branch. I don't say they don't exist. I only say that I haven't heard of any.

    Not every CPU will need to take a branch. For example, PIC16, which the OP uses can do it like this:

    Code (Text):
    1. incf a,w
    2. sublw w,0x0a
    3. skpnc
    4. clrf a
     
    takao21203 likes this.
Loading...