 |
www.elektronik.si Forum o elektrotehniki in računalništvu
|
Poglej prejšnjo temo :: Poglej naslednjo temo |
Avtor |
Sporočilo |
LoLek Član

Pridružen-a: Sre 25 Feb 2004 12:46 Prispevkov: 343 Aktiv.: 1.44
|
Objavljeno: Sre Maj 24, 2006 7:35 pm Naslov sporočila: while(1) vs. for(;;) |
|
|
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 |
|
 |
Sokrat Član


Pridružen-a: Čet 25 Avg 2005 11:00 Prispevkov: 5584 Aktiv.: 23.52
|
Objavljeno: Sre Maj 24, 2006 7:40 pm Naslov sporočila: |
|
|
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 |
|
 |
. Član

Pridružen-a: Pon 23 Avg 2004 16:16 Prispevkov: 16777190 Aktiv.: 70656.46
|
Objavljeno: Sre Maj 24, 2006 9:49 pm Naslov sporočila: |
|
|
Brisana vsebina odstranjenega uporabnika. |
|
Nazaj na vrh |
|
 |
. Član

Pridružen-a: Pon 23 Avg 2004 16:16 Prispevkov: 16777190 Aktiv.: 70656.46
|
Objavljeno: Sre Maj 24, 2006 9:55 pm Naslov sporočila: |
|
|
Brisana vsebina odstranjenega uporabnika. |
|
Nazaj na vrh |
|
 |
CarpeDiem Član


Pridružen-a: Pon 16 Feb 2004 17:24 Prispevkov: 333 Aktiv.: 1.40 Kraj: Vrhnika
|
Objavljeno: Sre Maj 24, 2006 10:01 pm Naslov sporočila: |
|
|
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 |
|
 |
|
|
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: 6 dni
Powered by phpBB © 2001, 2005 phpBB Group
|