1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

for loop or while loop?

Discussion in 'Software/Coding' started by Trickmaster, Jul 9, 2015.

  1. Trickmaster

    Trickmaster Member

    Joined:
    May 23, 2014
    Messages:
    1,178
    Likes Received:
    925
    Trophy Points:
    113
    Gender:
    Male
    Location:
    Potato Heaven
    Which function definition body is better/more likely to be used, even though they do the same task?

    def count_to_first_vowel(s):
    ''' (str) -> str
    Return the substring of s up to but not including the first vowel in s. If no vowel
    is present, return s.
    >>> count_to_first_vowel('hello')
    'h'
    >>> count_to_first_vowel('cherry')
    'ch'
    >>> count_to_first_vowel('xyz')
    xyz
    '''
    substring = ''
    for char in s:
    if char in 'aeiouAEIOU':
    return substring
    substring = substring + char
    return substring


    or

    def count_to_first_vowel(s):
    ''' (str) -> str
    Return the substring of s up to but not including the first vowel in s. If no vowel
    is present, return s.
    >>> count_to_first_vowel('hello')
    'h'
    >>> count_to_first_vowel('cherry')
    'ch'
    >>> count_to_first_vowel('xyz')
    xyz
    '''
    substring = ''
    i = 0
    while i < len(s) and not s in 'aeiouAEIOU':
    substring = substring + s
    i = i + 1
    return substring


    @alyphen @repository
     
  2. Trickmaster

    Trickmaster Member

    Joined:
    May 23, 2014
    Messages:
    1,178
    Likes Received:
    925
    Trophy Points:
    113
    Gender:
    Male
    Location:
    Potato Heaven
    Sorry about the spacing, it posted wrong :p
     
  3. Trickmaster

    Trickmaster Member

    Joined:
    May 23, 2014
    Messages:
    1,178
    Likes Received:
    925
    Trophy Points:
    113
    Gender:
    Male
    Location:
    Potato Heaven
    Code:
    def count_to_first_vowel(s):
        '''  (str) -> str
        Return the substring of s up to but not including the first vowel in s. If no vowel
        is present, return s.
        >>> count_to_first_vowel('hello')
        'h'
        >>> count_to_first_vowel('cherry')
        'ch'
        >>> count_to_first_vowel('xyz')
        xyz
        '''
        substring = ''
        for char in s:
            if char in 'aeiouAEIOU':
                return substring
            substring = substring + char
        return substring
    
    or
    
    def count_to_first_vowel(s):
        '''  (str) -> str
        Return the substring of s up to but not including the first vowel in s. If no vowel
        is present, return s.
        >>> count_to_first_vowel('hello')
        'h'
        >>> count_to_first_vowel('cherry')
        'ch'
        >>> count_to_first_vowel('xyz')
        xyz
        '''
        substring = ''
        i = 0
        while i < len(s) and not s[i] in 'aeiouAEIOU':
            substring = substring + s
            i = i + 1
        return substring
    
     
  4. Trickmaster

    Trickmaster Member

    Joined:
    May 23, 2014
    Messages:
    1,178
    Likes Received:
    925
    Trophy Points:
    113
    Gender:
    Male
    Location:
    Potato Heaven
    ^^Forgot about the code option, that helps
     
  5. not2excel

    not2excel Member

    Joined:
    Jun 24, 2015
    Messages:
    35
    Likes Received:
    27
    Trophy Points:
    18
    the main differences between the standard for (fori) loop and while loop are the reason you use them. If you know beforehand how many times you wish to loop, eg. the number of indices in a collection or just say 10 times. While loops are best for condition checking in which the condition could be false at any point in time over the course of the loop dispatchment
     
    Trickmaster likes this.
  6. Trickmaster

    Trickmaster Member

    Joined:
    May 23, 2014
    Messages:
    1,178
    Likes Received:
    925
    Trophy Points:
    113
    Gender:
    Male
    Location:
    Potato Heaven
    So, you would use a for loop in this situation? How could you control it so the function only iterates 10 times in a for loop?
     
  7. alyphen

    alyphen Member

    Joined:
    Jan 4, 2014
    Messages:
    101
    Likes Received:
    96
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Technical Consultant
    Location:
    127.0.0.1
    Home Page:
    In this situation you would use a for loop, since you're running a predetermined number of iterations (based on the string length)
    If you are finding out how many iterations later (based on a condition), that is when you use the while loop.
    This is pretty much what @not2excel outlined, but I hope it makes it clearer.
     
    NanoNet and Trickmaster like this.
  8. NanoNet

    NanoNet Member

    Joined:
    May 23, 2014
    Messages:
    1,044
    Likes Received:
    345
    Trophy Points:
    83
    Gender:
    Male
    Location:
    IKEA
    alyphen gave the perfect answer...! But if your looking for a quicker, and easier way to get your questions answered, go to www.stackoverflow.com. If you post a question there, you will get an answer within minutes. It's helped me a ton in my adventure of learning basic knowledge to some higher level stuff :p
     
    Trickmaster likes this.
  9. Trickmaster

    Trickmaster Member

    Joined:
    May 23, 2014
    Messages:
    1,178
    Likes Received:
    925
    Trophy Points:
    113
    Gender:
    Male
    Location:
    Potato Heaven
    Thanks! That helps a lot, I'll def use it :p