Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

which infinite loop is better ?

Status
Not open for further replies.

raja.bhavanam

Junior Member level 3
Joined
Nov 22, 2005
Messages
28
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
1,502
Infinite loops often arise in embedded systems. we can an infinite loop in C using while loop or for loop? which one is better ?if so why?

while(1)
{
---
---
}


for(;;)
{
---
---
}
 

That's quite the same.
A Compiler makes in both cases:

loop:
:
:
:
jmp loop


Gomez
 
All are same it's just a matter of personal choice. Guys please correct me if i am wrong.
 
while is better

in this case ,as it is easy to apply with infinite (uncalculable) times
 

my favourite infinite loop is for(;;) :)

while makes a lot of confusiosn sometimes

Randika K
 

Code:
for(initialization; condition; termination ){
  loop body;

}
is equivalent to:
Code:
initialization;
while(condition){
  loop body;
  termination;
}

Both infinite loops will be similar in assembly I think.
Regards,
Amraldo.
 

There is only one type of infinite loop with different names (endless, do forever ...) and different coding styles (while do, do while, for ()...), so the final result will be always same.
 

Well. Both of these loops do the same thing. I wanted to know if it is something specific to a particular compilar or ?? But as I have heard (sorry, forgot the reference) for( ; ; ) is a better option for embedded system. I am definitely curious to know why, if it is true.

Regards,
Bharath
 

Hey man why r u confused ?

Both the loops are generating the same compiled code with NOP() conditions for fixed times and with an infinite counter....so execution is giving the same result ... backend complexity is same only depends on how u r interpreting them .....

But if u are writing an infinite loop in µP programming then it will be obvious that it will be faster than the other codes ...because it is directly written in assembly language .

Chalo then .... bye !:D
 

i find while(1) is better for unconditional loop..
check the below quote i find in www.netrino.com

There are several solutions to this question. My preferred solution is:
while(1)
Many programmers seem to prefer: for( ; ; )
This construct puzzles me because the syntax doesn't exactly spell out what's going on.
Thus, if a candidate gives this as a solution, I'll use it as an opportunity to explore their
rationale for doing so. If their answer is basically, "I was taught to do it this way and I
haven't thought about it since," it tells me something (bad) about them. A third solution is
to use a goto:
Loop: goto Loop;
Candidates who propose this are either assembly language programmers (which is
probably good), or else they are closet BASIC/FORTRAN programmers looking to get
into a new field.
 

Hello,

If you realy want a infite loop that actually looks like an infite loop, you can write the following code:
Code:
#define ever ; ;

for(ever) {
  //your code
  }
;)

magixD
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top