Thread: The else is ignored in this IF statement.Its a part of a project that isn't finished

  1. #1
    Banned
    Join Date
    Nov 2019
    Posts
    2

    The else is ignored in this IF statement.Its a part of a project that isn't finished

    Code:
    int j=5,r1=1;
          if(r1>=10 || r1<20){
                switch(r1){
                    case 10:
                    printf(" ten");
                    break;
                case 11:
                   printf(" eleven");
                   break;
                case 12:
                   printf(" twelve");
                   break;
                case 13:
                   printf(" thirteen");
                   break;
                case 14:
                   printf(" fourteen");
                   break;
                case 15:
                   printf(" fifteen ");
                   break;
                case 16:
                   printf(" sixteen ");
                   break;
                case 17:
                   printf(" seventeen ");
                   break;
                case 18:
                   printf(" eighteen ");
                   break;
                case 19:
                   printf(" nineteen ");
                   break;
                default:
                    break;
                        }
                      }
            else{
                switch (j){
                case 2:
                   printf(" twenty");
                   break;
                case 3:
                   printf(" thirty");
                   break;
                case 4:
                   printf(" forty");
                   break;
                case 5:
                   printf(" fifty ");
                   break;
                case 6:
                   printf(" sixty ");
                   break;
                case 7:
                   printf(" seventy ");
                   break;
                case 8:
                   printf(" eighteen ");
                   break;
                case 9:
                   printf(" ninety ");
                   break;
                default:
                    break;
                }
            }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Think carefully about this expression: (r1>=10 || r1<20). For what value of r1 does this expression evaluate to false?

    Let's say we choose r1 < 10, say r1 == 0. Then r1 < 20 must be true, because if r1 < 10, then surely r1 < 20, because any integer that's less than 10 must be less than 20. Therefore the expression evaluates to true.

    Let's say we choose r1 >= 20, say r1 == 30. Then r1 >= 10, because surely any integer that's greater than or equal to 20 is also greater than or equal to 10. Therefore the expression evaluates to true.

    So, the expression is always true, hence the else branch is ignored.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Nov 2019
    Posts
    2
    Thank you soo much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-02-2013, 06:45 PM
  2. I finished my syllogism project
    By jeremy duncan in forum C Programming
    Replies: 3
    Last Post: 12-04-2011, 10:35 AM
  3. help with while statement for college project
    By fsutati in forum C Programming
    Replies: 8
    Last Post: 10-24-2010, 08:47 PM
  4. If statement is been passed over-Simple Opencv project
    By aprop in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2010, 12:05 PM
  5. multi file project error part 2
    By dantestwin in forum C++ Programming
    Replies: 6
    Last Post: 07-21-2004, 11:35 PM

Tags for this Thread