use of for loop in a code

Thread Starter

ect_09

Joined May 6, 2012
180
i want to run this code with for loop instead of
Code:
a=(++a)%10;
Code:
#include<htc.h>

__CONFIG(1,OSCSDIS & HSPLL);
__CONFIG(2,BORDIS & PWRTDIS &WDTDIS);
__CONFIG(3,CCP2RC1);
__CONFIG(4,LVPDIS & STVREN);
__CONFIG(5,UNPROTECT);
__CONFIG(6,WRTEN);
__CONFIG(7,TRU);

#define _XTAL_FREQ   20000000


void led_display(char a)
{
switch( a)

{
case 0: PORTB=0x01; break;
case 1: PORTB=0x02; break;
case 2: PORTB=0x04; break;
case 3: PORTB=0x08; break;
}

void main()
{
TRISB=0x00;
char a;

while(1)
{
led_display(a);
__delay_ms(1000);
//for(a=0;a<5;a++);
a=(++a)%10;
}

}
please help me in this way..
 

Thread Starter

ect_09

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

Thanks...
 

NorthGuy

Joined Jun 28, 2014
611
This is not a good code:

Code:
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:
a = (a+1)%10;
Or, more efficiently

Code:
if (++a > 9) a = 0;
 

takao21203

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

I use
Code:
 a++;if(a>9)a=0;
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++
 

takao21203

Joined Apr 28, 2012
3,702
This is not a good code:

Code:
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.
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:
++a%10
can be compiled; doesnt look good however.

Code:
if (++a > 9) a = 0;
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.
 

NorthGuy

Joined Jun 28, 2014
611
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).
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:
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:
a = (b++);
This is an incorrect way (and woouldn't compile anyway):

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

Code:
a = <original value of b>;
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:
b = b + 1;
a = <original value of b>;
It is up to the programmer to make sure these two don't interfere with each other.

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.
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:
incf a,w
sublw w,0x0a
skpnc
clrf a
 
Top