can u show me an idea when and where na sya ibutang kay maungot ra gyud ko ana nga condition or statement. den- a ko kasabot unsay sunod.
need ure help master!
salamat
can u show me an idea when and where na sya ibutang kay maungot ra gyud ko ana nga condition or statement. den- a ko kasabot unsay sunod.
need ure help master!
salamat
unsa pasabot nimo na mo ungot ka? mo ungot imo program?
ang for ug while kay the same raman tingali ilang buhaton, ang difference lang kay ilang syntax.
is equivalent to:Code:for(int i = 0; i < n; i++) { // Do something with n }
Just use whatever you feel like using.Code:int i = 0; while(i < n) { // Do something with n i++; }
Unya sa do while kay mo check sa condition after i run ang body. This means that it'll execute at least once unlike sa uban. Example:
Code:int i = 1; do { System.out.println("This will be printed."); } while(i < 0); for(; i < 0; i++) { System.out.println("This WON'T be printed."); } while(i < 0) { System.out.println("This also WON'T be printed."); }
Last edited by doomsweek; 11-22-2009 at 03:00 AM.
for loop - kung imong loop is fixed ang length sa looping niya.
while loop - kung ang loop is undefined ang length sa loop.
do-while loop - is same sa while loop pero ang difference niya kay before siya mag-start sa loop mo run sa siya ka-usa ug mga statements sulod sa do-while.
how abouthow aboutCode:for(;;){ }its a finite limitation.while(*mem < 2048){}
for loop (initialization, condition, increment), thats make the difference.
while loop = check expression before execution, there can be no execution happened.
do while loop = execute first before checking the expression. the statement is executed at least once, whatever happens in while.
FORLOOP can be while, and dowhile.
WHILE and DO-WHILE cannot be FORLOOP, you need to add additional codes for initialization and increment.
Last edited by MarkCuering; 11-22-2009 at 09:33 AM.
careful sad paggamit ana kay tingali muungot sad imong program kay dako ra ug running time.
avoid nested loops.
According to Mehran Sahami:
Use for loop for definite iteration. Generally, we know how many times we want to iterate.
Use while loop for indefinite iteration. Generally, don't know how many times iterate beforehand.
Are you counting up to something some number of times, or are you just gonna do something until some
condition is true? That’s, kind of, the differentiator there. -Mehran SahamiVSCode:while(rs.next()) { // Do something with ResultSet }
They both do the same thing but, IMO, mas 'limpyo' tan.awn ang while loop for this example. We don't know beforehand kung ika pila ta mo loop.Code:for(;rs.next();) { // Do something with ResultSet }
VSCode:int i = 0; while(i < n) { System.out.println(n); i++; }
They still both do the same thing, pero mas 'limpyo' tan.awn ang for loop kay ang init, condition, and step kay naa ra sa usa ka line compared sa while loop na separated sila. Unya, in here we already know na the loop would run n times, so definite iteration na.Code:for(int i = 0; i < n; i++) { System.out.println(n); }
I think mao sad ning pasabot ni eax.
Last edited by doomsweek; 11-22-2009 at 09:01 PM.
Can you give an example of a for loop executing like a do-while loop?
If a for loop can be a while loop then the reverse is also true.
Code:int factorial = 1; for(int i = 1; i < n; i++) { factorial *= n; }But, I don't know what's the equivalent code for a do-while loop to act like a for loop.Code:int factorial = 1; int i = 1; while(i < n) { factorial *=n ; i++ }
Last edited by doomsweek; 11-22-2009 at 09:00 PM.
after all have been said here, all i can say is use your imagination and common sense lol.
first things first,
the if-else statement is a conditional statement. you use it if you want to check for a condition.
the for, while, do while loop statements are, as what the name implies, for looping a single statement a number of times.
Similar Threads |
|