LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2005, 01:02 AM   #1
Gigantor
LQ Newbie
 
Registered: Oct 2005
Posts: 10

Rep: Reputation: 0
C Programming: Do and While loops


I have a problem to do and I'm not really sure where to begin. I have some written out but I'm lost. Code must be written in 'while loops' and then re-written as 'do while loops.' Here is the problem:

"Initialize totalGallons to zero.
Initialize totalMiles to zero.
Input the gallons used for the first tank.

While the sentinel value (-1) has not been entered for the gallons
Add gallons to the running total in totalGallons
Input the miles driven for the current tank
Add miles to the running total in totalMiles
Calculate and print the miles/gallon
Input the gallons used for the next tank
End while

Set totalAverage to totalMiles divided by totalGallons.
print the TotalAverage miles/gallon."


I'm not sure of the best way to approach the start. I haven't programmed in while loops yet. The book I have isn't the best at explaining it either. Any help would be appreciated.

Thank you.
 
Old 11-08-2005, 01:45 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
I'll explain the basics of while and do while loops to you and see if that helps.

While loops are in this form:

Code:
while(condition)
{
     /* Statements */
}

int x = 3; /* Just an example of something that could come after */
"Condition" is anything that evaluates to true or false (eg. x < 2, assuming you had x defined). When the program gets to the top of the loop, it checks to see if that condition is true or false. If it is false, the body of the while loop (those statements between the {}) is skipped and execution continues after the while loop ends (the "int x = 3;" line). If it is true, the statements are executed in order. Afterwards, the condition is checked again and the same happens. You can see that if it is false to begin with, the loop will be skipped over.

Do-while:

Code:
do
{
    /* Statements */
} while(condition)

int x = 3;
Here, the loop runs at least once. It starts from the top and works its way down the statements in the body. After that, the condition is tested. If it's true, then the body of the loop is executed again and if not, execution continues after the loop ends as before.

Does that help at all?
 
Old 11-08-2005, 03:35 PM   #3
Gigantor
LQ Newbie
 
Registered: Oct 2005
Posts: 10

Original Poster
Rep: Reputation: 0
Yes this does help. I'm going to tackle it some more tonight. Thank you for your input.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ksh and while loops pdc Linux - Newbie 2 10-29-2005 04:13 PM
while loops + while : blizunt7 Linux - General 3 12-04-2004 05:27 PM
foreach loops chunky Linux - General 2 07-02-2004 11:49 AM
loops JMK Linux - Newbie 11 04-09-2004 05:30 PM
Functions And Loops petercool Programming 14 08-08-2003 10:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration