Page 1 of 3 123 LastLast
Results 1 to 10 of 21
  1. #1

    Smile when to use if else,for loop, switch and do while condition? and where to put this co


    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

  2. #2
    unsa pasabot nimo na mo ungot ka? mo ungot imo program?

  3. #3
    ang for ug while kay the same raman tingali ilang buhaton, ang difference lang kay ilang syntax.

    Code:
    for(int i = 0; i < n; i++) {
        // Do something with n
    }
    is equivalent to:

    Code:
    int i = 0;
    
    while(i < n) {
        // Do something with n
        i++;
    }
    Just use whatever you feel like using.

    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.

  4. #4
    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.

  5. #5
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    Quote Originally Posted by eax View Post
    for loop - kung imong loop is fixed ang length sa looping niya.
    how about
    Code:
    for(;;){ }
    Quote Originally Posted by eax View Post
    while loop - kung ang loop is undefined ang length sa loop.
    how about
    while(*mem < 2048){}
    its a finite limitation.

    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.

  6. #6
    careful sad paggamit ana kay tingali muungot sad imong program kay dako ra ug running time.

    avoid nested loops.

  7. #7
    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 Sahami
    Code:
    while(rs.next()) {
        // Do something with ResultSet
    }
    VS
    Code:
    for(;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:
    int i = 0;
    while(i < n) {
        System.out.println(n);
        i++;
    }
    VS
    Code:
    for(int i = 0; i < n; i++) {
        System.out.println(n);
    }
    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.

    I think mao sad ning pasabot ni eax.
    Last edited by doomsweek; 11-22-2009 at 09:01 PM.

  8. #8
    Quote Originally Posted by MarkCuering View Post
    FORLOOP can be while, and dowhile.
    Can you give an example of a for loop executing like a do-while loop?

    Quote Originally Posted by MarkCuering View Post
    WHILE and DO-WHILE cannot be FORLOOP, you need to add additional codes for initialization and increment.
    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;
    }
    Code:
    int factorial = 1;
    int i = 1;
    
    while(i < n) {
        factorial *=n ;
        i++
    }
    But, I don't know what's the equivalent code for a do-while loop to act like a for loop.
    Last edited by doomsweek; 11-22-2009 at 09:00 PM.

  9. #9
    after all have been said here, all i can say is use your imagination and common sense lol.

  10. #10
    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.

  11.    Advertisement

Page 1 of 3 123 LastLast

Similar Threads

 
  1. Ten Reasons to use SAP 2500 for your Water Station Business
    By ingenmart in forum Business, Finance & Economics Discussions
    Replies: 1
    Last Post: 02-23-2012, 09:50 PM
  2. For Sale: im selling my CPU unit good as set ready to use...see inside for details...
    By dookie3283 in forum Computers & Accessories
    Replies: 17
    Last Post: 09-02-2011, 04:19 AM
  3. For Sale: rush for to night if mada for only 25k
    By sexy_back in forum Computers & Accessories
    Replies: 1
    Last Post: 05-04-2011, 09:01 PM
  4. how to use a methods in a switch case? I need it so badly
    By istoryansucks in forum Programming
    Replies: 11
    Last Post: 07-19-2010, 04:41 PM
  5. Replies: 14
    Last Post: 10-07-2005, 04:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top