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.
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 );
}
}
never-mind the french ...
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):
Code:
boolean cond;
do {
// some code
cond = some_condition;
// more code
} while(!cond);
Code:
boolean cond = false;
while(cond {
// some code
cond = some_condition;
// more code
}
Personally I prefer a while loop (unless it's really contrived to have one) since it states the "governing" condition right at the start.
Re: change do-while loop to while
Thank you ! will do for the code
Re: change do-while loop to while
Quote:
Originally Posted by
pbrockway2
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):
Code:
boolean cond;
do {
// some code
cond = some_condition;
// more code
} while(!cond);
Code:
boolean cond = false;
while(cond {
// some code
cond = some_condition;
// more code
}
Personally I prefer a while loop (unless it's really contrived to have one) since it states the "governing" condition right at the start.
No, a do-while loop will execute one time before checking the condition.
Code:
do {
System.out.println("This line will be printed once, before the condition is checked.");
} while (false);
Your while code example would not even execute once, because the condition is always false.
Re: change do-while loop to while
Opps! Yes, you're right: the while loop should have said "while(!cond){". Thanks for the correction.
Using the OP's variable names it would look like:
Code:
boolean valide = false;
while(!valide {
// some code
valide = some_condition;
// more code
}