•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,272 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,383 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 558 | Replies: 10
![]() |
FOR or WHILE
as in,
the FOR loop will perform <stuff> MAX number of times before continuing with the rest of the program. note that the variable "count" is incremented once each time the loop is executed, until it exceeds the value MAX. "MAX" doesnt have to be a variable. it could simply be hardcoded as 10 or 20 or 1000 or whatever; i just put it as a variable for illustration.
WHILE works similarly, but in this case the variable "count" is modified somewhere in the block of the code (<stuff>)... make sure that "count" actually can become greater than MAX, or you will be stuck in the while loop forever. infinite loops are a common programming bug.
WHILE loops are often used with various conditions such as
while(reverseCount > 0)
or
while(flag != 1)
etc.
.
as in,
c Syntax (Toggle Plain Text)
for (count=1; count<= MAX; count++) { <stuff> } ... while(count <= MAX) { <stuff> }
the FOR loop will perform <stuff> MAX number of times before continuing with the rest of the program. note that the variable "count" is incremented once each time the loop is executed, until it exceeds the value MAX. "MAX" doesnt have to be a variable. it could simply be hardcoded as 10 or 20 or 1000 or whatever; i just put it as a variable for illustration.
WHILE works similarly, but in this case the variable "count" is modified somewhere in the block of the code (<stuff>)... make sure that "count" actually can become greater than MAX, or you will be stuck in the while loop forever. infinite loops are a common programming bug.
WHILE loops are often used with various conditions such as
while(reverseCount > 0)
or
while(flag != 1)
etc.
.
Last edited by jephthah : May 14th, 2008 at 12:36 pm.
Why so serious?
•
•
Join Date: May 2008
Posts: 34
Reputation:
Rep Power: 1
Solved Threads: 0
Here is what I have so far, I have corrected what I had orginally though I am sure it needs more tweeking to be correct.
Main Module
Declare Currency Type as Integer
Declare Foreign Currency as Real
Declare US Dollar as real
Do while User continues
Call Display Menu Module
Call Get Foreign Currency Value Module
Call Convert Currency Module
Call Display Results Module
End Loop
End Main Module
Display Menu Module
Loop goes here Not sure about it though??
Display "Currency Converter"
Display "Select a Currency"
Display "1 Canadian Dollars"
Display "2 Mexican Pesos"
Display "3 English Pounds"
Display "4 Japanese Yen"
Display "5 French Francs"
Display "6 Done"
Input Foreign Currency Type
if Foreign currency type ≤ 5 continue to Calculate Rate
Else
display "Invalid Data Type Please Select again"
End if
End Display Menu Module
Get Foreign Currency Value Module
Display " Enter a Currency value (0-100,000)"
Input Foreign Currency value
if Foreign Currency Value <0 or Foreign Currency Value >100,000
Else Display "Invalid Entry, Please try again"
End Get Foreign Currency Value Module
Convert Currency Module
Select Foreign Currency Type
Case 1: Canadian
Set Rate= 1.4680
Case 2: Mexican
Set Rate= 9.5085
Case 3: English
Set Rate=1.6433
Case 4: Japanese
Set Rate = 104.9200
Case 5: French
Set Rate = 6.2561
US Dollar Value=Foreign Currency Value*Rate
End Convert Currency
Display Results
Declare Currency as String
Case 1: Canadian
Currency="Dollars"
Case 2: Mexican
Currency="Pesos"
Case 3: English
Currency="Pounds"
Case 4: Japanese
Currency="Yen"
Case 5: French
Currency="Francs"
Display" The Value of Foreign Currency Value" is "US Dollar Value" "Dollars"
End Display Results
Main Module
Declare Currency Type as Integer
Declare Foreign Currency as Real
Declare US Dollar as real
Do while User continues
Call Display Menu Module
Call Get Foreign Currency Value Module
Call Convert Currency Module
Call Display Results Module
End Loop
End Main Module
Display Menu Module
Loop goes here Not sure about it though??
Display "Currency Converter"
Display "Select a Currency"
Display "1 Canadian Dollars"
Display "2 Mexican Pesos"
Display "3 English Pounds"
Display "4 Japanese Yen"
Display "5 French Francs"
Display "6 Done"
Input Foreign Currency Type
if Foreign currency type ≤ 5 continue to Calculate Rate
Else
display "Invalid Data Type Please Select again"
End if
End Display Menu Module
Get Foreign Currency Value Module
Display " Enter a Currency value (0-100,000)"
Input Foreign Currency value
if Foreign Currency Value <0 or Foreign Currency Value >100,000
Else Display "Invalid Entry, Please try again"
End Get Foreign Currency Value Module
Convert Currency Module
Select Foreign Currency Type
Case 1: Canadian
Set Rate= 1.4680
Case 2: Mexican
Set Rate= 9.5085
Case 3: English
Set Rate=1.6433
Case 4: Japanese
Set Rate = 104.9200
Case 5: French
Set Rate = 6.2561
US Dollar Value=Foreign Currency Value*Rate
End Convert Currency
Display Results
Declare Currency as String
Case 1: Canadian
Currency="Dollars"
Case 2: Mexican
Currency="Pesos"
Case 3: English
Currency="Pounds"
Case 4: Japanese
Currency="Yen"
Case 5: French
Currency="Francs"
Display" The Value of Foreign Currency Value" is "US Dollar Value" "Dollars"
End Display Results
•
•
Join Date: May 2008
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Sounds like an "existential matter" instead of a programming issue...
the correct approach of a for loop is "when you know how may iterations you have to do" this is, if you're going to repeat the piece of code just 10 times, use a for
the approach of a while loop is more than a "conditional loop" this is, when you don't know the total amount of iterations, but when to stop, then you may either use a while (if there's a condition to loop) or a do... while (if you know you should do the job at least once.
The performance is almost the same for both for, and while loops the compiler always do an "internal while" (even when exists an internal for-like cpu instruction loop) the compiler likes to make a goto-break statement, this is because the for can also be used to simulate whiles, and infinite loops (this isn't always a mistake... everybody have a infinite loop on his/her os, called idle process), this is also used by schedulers and background processes (naturally, to break an infinite loop you use a signal) the term may be "indetermined loops"
the correct approach of a for loop is "when you know how may iterations you have to do" this is, if you're going to repeat the piece of code just 10 times, use a for
the approach of a while loop is more than a "conditional loop" this is, when you don't know the total amount of iterations, but when to stop, then you may either use a while (if there's a condition to loop) or a do... while (if you know you should do the job at least once.
The performance is almost the same for both for, and while loops the compiler always do an "internal while" (even when exists an internal for-like cpu instruction loop) the compiler likes to make a goto-break statement, this is because the for can also be used to simulate whiles, and infinite loops (this isn't always a mistake... everybody have a infinite loop on his/her os, called idle process), this is also used by schedulers and background processes (naturally, to break an infinite loop you use a signal) the term may be "indetermined loops"
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Need Advice on for loops with vowels (Java)
- Loops (C++)
- Need some help with my do-while and while loops (Java)
- Help with loops (Java)
- Program Help Using For Nested Loops (C++)
- merged:nesting loops (C++)
- help for program involving switch loops and file (C++)
Other Threads in the C Forum
- Previous Thread: Problem in finding the greatest no. from array
- Next Thread: help w/ matrix!!please!!


Linear Mode