toupper(); or ||

This is a discussion on toupper(); or || within the C Programming forums, part of the General Programming Boards category; I was helping my brother with a program and he ended up having the program terminate by a do while ...

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    toupper(); or ||

    I was helping my brother with a program and he ended up having the program terminate by a do while loop. the while was
    while(YesNo=='Y' || YesNo=='y');
    I was thinking he would do what I do:
    while(toupper(YesNo)=='Y');
    I think my way is preferred, right?Also which one is better or faster

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Using the || should be faster, since you save on a function call for such a simple comparison.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I don't know what's "preferred" (& does it really matter what's preferred?) but his way will result in a smaller program (your way, you have to #include<ctype.h> and all the code that comes with it gets compiled into your program).

    Also, his way must be faster. If the value of YesNo is 'Y', his way finishes with a single test -- it doesn't even look past the ||. If YesNo is not 'Y', he finishes after the second test.

    Your way, in the best case, toupper() has to test to see if YesNo is an uppercase letter (I guess that takes 2 tests), then return to the main program to test if it's a 'Y'. So, it looks like there's at least 2 extra steps if it already contains 'Y' to begin with. If it's not an uppercase letter, it has to test again to see if it's a lowercase letter, then return whatever the uppercase equivalent is to the main program to be tested against 'Y'. Also, unless toupper() is implemented as a macro (I don't know whether it is or not), there is presumably some more time wasted calling the function.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    Go for what is most readable for you.

    The performance / space tradeoff's of such a simple piece of code are not worth worrying about.

    Find something really worth worrying about, like replacing a crummy bubblesort with a quicksort - that's where you'll get your real performance boosts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  2. process killer on a loop
    By Anddos in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2006, 01:50 AM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21