www.elektronik.si Seznam forumov www.elektronik.si
Forum o elektrotehniki in računalništvu
 
 PomočPomoč  IščiIšči  Seznam članovSeznam članov  SkupineSkupine  StatisticsStatistika  AlbumAlbum  DatotekeFilemanager DokumentacijaDocDB LinksPovezave   Registriraj seRegistriraj se 
  PravilaPravila  LinksBolha  PriponkePriponke  KoledarKoledar  ZapiskiZapiski Tvoj profilTvoj profil Prijava za pregled zasebnih sporočilPrijava za pregled zasebnih sporočil PrijavaPrijava 

while(1) vs. for(;;)

 
Objavi novo temo   Odgovori na to temo   Printer-friendly version    www.elektronik.si Seznam forumov -> Programska oprema
Poglej prejšnjo temo :: Poglej naslednjo temo  
Avtor Sporočilo
LoLek
Član
Član



Pridružen-a: Sre 25 Feb 2004 12:46
Prispevkov: 343
Aktiv.: 1.44

PrispevekObjavljeno: Sre Maj 24, 2006 7:35 pm    Naslov sporočila:  while(1) vs. for(;;) Odgovori s citatom

while(1) {
}

in

for (;; ) {
}

sta obe neskončni zanki. Zdaj me pa zanima razlika med njima? V čem je poanta?


Nazadnje urejal/a LoLek Sre Maj 24, 2006 7:57 pm; skupaj popravljeno 1 krat
Nazaj na vrh
Odsoten Poglej uporabnikov profil Pošlji zasebno sporočilo
Sokrat
Član
Član



Pridružen-a: Čet 25 Avg 2005 11:00
Prispevkov: 5584
Aktiv.: 23.52

PrispevekObjavljeno: Sre Maj 24, 2006 7:40 pm    Naslov sporočila:   Odgovori s citatom

Vpisi oboje v program, program prevedi in obcuduj assembly listing - morda ni razlike, morda je (in ce je, bos videl kaksna je). O tem, kaksen bo rezultat, odloca edinole compiler in mozno je, da razlicni compilerji dajo razlicne rezultate.
Nazaj na vrh
Skrit Poglej uporabnikov profil Pošlji zasebno sporočilo
.
Član
Član



Pridružen-a: Pon 23 Avg 2004 16:16
Prispevkov: 16777190
Aktiv.: 70658.97

PrispevekObjavljeno: Sre Maj 24, 2006 9:49 pm    Naslov sporočila:   Odgovori s citatom

Brisana vsebina odstranjenega uporabnika.
Nazaj na vrh
Odsoten Poglej uporabnikov profil Pošlji zasebno sporočilo
.
Član
Član



Pridružen-a: Pon 23 Avg 2004 16:16
Prispevkov: 16777190
Aktiv.: 70658.97

PrispevekObjavljeno: Sre Maj 24, 2006 9:55 pm    Naslov sporočila:   Odgovori s citatom

Brisana vsebina odstranjenega uporabnika.
Nazaj na vrh
Odsoten Poglej uporabnikov profil Pošlji zasebno sporočilo
CarpeDiem
Član
Član



Pridružen-a: Pon 16 Feb 2004 17:24
Prispevkov: 333
Aktiv.: 1.40
Kraj: Vrhnika

PrispevekObjavljeno: Sre Maj 24, 2006 10:01 pm    Naslov sporočila:   Odgovori s citatom

Koda:

3.5 Loops -- While  While and Forand For 
We have already encountered the while and for loops. In 
 
   while (expression)
       statement
the expression is evaluated. If it is non-zero, statement is executed and expression is re-
evaluated. This cycle continues until expression becomes zero, at which point execution
resumes after statement. 
The for statement 
 
   for (expr1; expr2; expr3)
       statement
is equivalent to 
 
   expr1;
   while (expr2) {
       statement
       expr3;
   }
except for the behaviour of continue, which is described in Section 3.7. 
Grammatically, the three components of a  for loop are expressions. Most commonly,
expr1 and expr3 are assignments or function calls and expr2 is a relational expression.
Any of the three parts can be omitted, although the semicolons must remain. If expr1 or
expr3 is omitted, it is simply dropped from the expansion. If the test, expr2, is not present,
it is taken as permanently true, so 
 
   for (;;) {
       ...
   }
is an ``infinite'' loop, presumably to be broken by other means, such as a break or
return. 
Whether to use while or for is largely a matter of personal preference. For example, in 
 
   while ((c = getchar()) == ' ' || c == '\n' || c = '\t')
       ;   /* skip white space characters */
there is no initialization or re-initialization, so the while is most natural. 

    61
The for is preferable when there is a simple initialization and increment since it keeps
the loop control statements close together and visible at the top of the loop. This is most
obvious in 
 
   for (i = 0; i < n; i++)
       ...
which is the C idiom for processing the first n elements of an array, the analog of the
Fortran DO loop or the Pascal for. The analogy is not perfect, however, since the index
variable  i retains its value when  the loop terminates for any reason. Because the
components of the  for are arbitrary expressions,  for loops are not restricted to
arithmetic progressions. Nonetheless, it is bad style to force unrelated computations into
the initialization and increment of a for, which are better reserved for loop control
operations.
Nazaj na vrh
Odsoten Poglej uporabnikov profil Pošlji zasebno sporočilo Pošlji E-sporočilo Obišči avtorjevo spletno stran
Pokaži sporočila:   
Objavi novo temo   Odgovori na to temo   Printer-friendly version    www.elektronik.si Seznam forumov -> Programska oprema Časovni pas GMT + 2 uri, srednjeevropski - poletni čas
Stran 1 od 1

 
Pojdi na:  
Ne, ne moreš dodajati novih tem v tem forumu
Ne, ne moreš odgovarjati na teme v tem forumu
Ne, ne moreš urejati svojih prispevkov v tem forumu
Ne, ne moreš brisati svojih prispevkov v tem forumu
Ne ne moreš glasovati v anketi v tem forumu
Ne, ne moreš pripeti datotek v tem forumu
Ne, ne moreš povleči datotek v tem forumu

Uptime: 5 dni


Powered by phpBB © 2001, 2005 phpBB Group