C For Dummies, 2nd Edition by Dan Gookin
Author:Dan Gookin [Gookin, Dan]
Language: eng
Format: epub, pdf
Tags: Team DDU
Published: 0101-01-01T00:00:00+00:00
}
while( condition);
23 570684 Ch18.qxd 3/31/04 3:00 PM Page 228
228 Part III: Giving Your Programs the Ability to Run Amok The condition is a true-or-false comparison that while examines â the same type of deal you find in an if comparison. If the condition is true, the statements in the loop are repeated. They keep doing so until the condition is false, and then the program continues. But, no matter what, the statements are always gone through once.
An important thing to remember here is that the while at the end of the loop requires a semicolon. Mess it up and itâs sheer torture later to figure out what went wrong.
One strange aspect of the do-while loop is that it seriously lacks the starting, while-true, and do-this aspects of the traditional while and for loops. It has no starting condition because the loop just dives right into it. Of course, this sentence doesnât mean that the loop would lack those three items. In fact, it may look like this:
starting;
do
{
statement(s);
do_this;
}
while( while_true);
Yikes! Better stick with the basic while loop and bother with this jobbie only when something needs to be done once (or upside down).
ß The condition that while examines is either TRUE or FALSE, according to the laws of C, the same as a comparison made by an if statement.
You can use the same symbols used in an if comparison, and even use the logical doodads (&& or ||) as you see fit.
ß This type of loop is really rare. It has been said that only a mere 5 percent of all loops in C are of the do-while variety.
ß You can still use break to halt a do-while loop. Only by using break, in fact, can you halt the statements in the midst of the loop. Otherwise, as with a while or for loop, all the statements within the curly braces repeat as a single block.
A flaw in the COUNTDWN.C program
Run the COUNTDWN program again. When it asks you to type a number, enter 200.
There isnât a problem with this task; the program counts down from 200 to 0 and blasts off as normal. But, 200 is out of the range the program asks you to type.
23 570684 Ch18.qxd 3/31/04 3:00 PM Page 229
Chapter 18: Do C While You Sleep
229
Use your operating systemâs scroll-back buffer to view the lines that have scrolled off the screen. Hopefully, the DOS prompt or terminal window that youâre using has this feature.
What about typing 0 or a negative number? Try that now; run the program and typeâ5. You see something like this:
T-minus -5
Zero!
Blast off!
The do while loop is executed at least once, so the -5 is displayed. Ack! It isnât one of the great boo-boos of modern programming history, but itâs bound to startle the astronauts, who are expecting a leisurely though suspenseful takeoff sequence.
The way to guard against this faux pas is to write a special loop to ensure that the value that is typed is kosher. Yes, itâs a kosher number loop. That is handled quite brilliantly by do-while.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26621)
The Fifty Shades Trilogy & Grey by E L James(19100)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19083)
Shot Through the Heart by Mercy Celeste(18955)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17139)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17027)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16901)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16841)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16470)
The Subtle Art of Not Giving a F*ck by Mark Manson(14386)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14160)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13679)
Scorched Earth by Nick Kyme(12788)
Drei Generationen auf dem Jakobsweg by Stein Pia(10985)
Suna by Ziefle Pia(10905)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(10703)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(10661)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(10646)
Scythe by Neal Shusterman(10372)
