Results 1 to 5 of 5
Thread: change do-while loop to while
- 12-26-2012, 04:40 AM #1
Member
- Join Date
- Dec 2012
- Location
- Ottawa
- Posts
- 4
- Rep Power
- 0
change do-while loop to while
Hi i know its probably an easy one but i just cant figure that one out i might be tired ... i need to change the Do-while loop for while.
never-mind the french ...Java Code:import java.util.*; public class DoWhile2 { public static void main (String[] args) { final int AGE_MIN = 0, AGE_MAX = 125; int age ; boolean valide ; String tmp; Scanner clavier = new Scanner(System.in); // saisir et valider l'âge entre 2 bornes do { System.out.println("Tapez l'âge d'une personne entre "+ AGE_MIN + " et " + AGE_MAX + ": "); age = clavier.nextInt(); valide = (age >= AGE_MIN && age <= AGE_MAX); if (!valide) System.out.println(age + " est hors bornes"); } while (!valide); System.out.println("Age saisi : " + age + " an(s)"); char genre ; // saisir et valider le genre (parmi f, F, m et M) do { System.out.println("Tapez f, F, m ou M pour le genre : "); tmp = clavier.next(); genre = tmp.charAt(0); valide = (genre == 'f' || genre == 'F' || genre == 'm' || genre == 'M'; if (! valide) System.out.println("Le caractère saisi " + genre + " est invalide"); } while (!valide); System.out.println("Caractère saisi pour le genre : " + genre ); } }Last edited by pbrockway2; 12-26-2012 at 04:59 AM. Reason: code tags added
- 12-26-2012, 05:10 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: change do-while loop to while
Hi Souverain, welcome to the forums!
When you post code, use the "code" tags. In the pane where you compose your post it's a little # sign next to the speech bubble you used to "quote" the code. Or you can enter the tags by hand: put [code] at the start of the code, and [/code] at the end.
The following are equivalent (I think):
Java Code:boolean cond; do { // some code cond = some_condition; // more code } while(!cond);Personally I prefer a while loop (unless it's really contrived to have one) since it states the "governing" condition right at the start.Java Code:boolean cond = false; while(cond { // some code cond = some_condition; // more code }Last edited by pbrockway2; 12-26-2012 at 05:13 AM.
- 12-28-2012, 03:04 AM #3
Member
- Join Date
- Dec 2012
- Location
- Ottawa
- Posts
- 4
- Rep Power
- 0
Re: change do-while loop to while
Thank you ! will do for the code
- 12-29-2012, 05:57 AM #4
Member
- Join Date
- Dec 2012
- Posts
- 11
- Rep Power
- 0
Re: change do-while loop to while
No, a do-while loop will execute one time before checking the condition.
Your while code example would not even execute once, because the condition is always false.Java Code:do { System.out.println("This line will be printed once, before the condition is checked."); } while (false);Last edited by Nouish; 12-29-2012 at 05:59 AM.
- 12-29-2012, 10:51 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Change a program to a LoopTest, and change a LoopTest to an Array List???
By Valerie1067 in forum New To JavaReplies: 12Last Post: 04-20-2012, 05:43 PM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
change text colors through nested loop
By Bader in forum Java AppletsReplies: 1Last Post: 05-09-2010, 02:24 AM -
Finding Most Effecient Distribution of Change -- Won't Enter Loop
By Cod in forum New To JavaReplies: 18Last Post: 11-29-2009, 11:58 PM -
Change my for loop
By javaplus in forum New To JavaReplies: 4Last Post: 12-12-2007, 11:00 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks