Thread: Switch Case help(Using OR)

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    11

    Question Switch Case help(Using OR)

    I have made a program to check whether alphabet is vowel or consonant using following code:-

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    char ch;
    clrscr();
    printf("Enter any alphabet.\n");
    scanf("%c",&ch);
    switch(ch)
    {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
                printf("Vowel.");
                break;
    default:
                printf("Consonant.);
                break;
    }
    getch();
    }
    It is working fine as it is supposed to but I want to ask is there a way to use OR and put all those vowels in one line rather than 5 different? I tried a bit but failed.
    Can someone help out?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Orcus
    is there a way to use OR and put all those vowels in one line rather than 5 different?
    Yes, use an if statement instead of a switch, with || for "or". Of course, you might still break it up into multiple physical lines of code for formatting purposes, but it'll be one logical line of code.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You mean like one line like so?
    case 'a': case 'e': case 'i': case 'o': case 'u':

    Also, main returns int, not void.

    Also, conio.h has been obsolete for the last 25+ years.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    11

    Smile

    Quote Originally Posted by Salem View Post
    You mean like one line like so?
    case 'a': case 'e': case 'i': case 'o': case 'u':

    Also, main returns int, not void.

    Also, conio.h has been obsolete for the last 25+ years.
    Well, I am still learning C and my teacher has basically started with stdio and conio so I wouldn't know how outdated it might be. Also, The second point of yours makes no sense to me for now. Maybe because I am sort of a starter, might helping with that?

    And that is an interesting way to write the switch statement, Thanks.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    stdio.h is part of standard C.
    conio.h is as obsolete as DOS (before your time - it was microsoft's 16-bit command line OS from the 1980's).

    At least you know now that your teacher isn't as up to date as you previously thought.
    Watch out for these other tell tale signs that they really don't know what they're talking about.
    - using fflush(stdin) to clean up the input buffer.
    - using gets(buffer) to read a line of input.


    > And that is an interesting way to write the switch statement
    C for the most part doesn't care about white space or newlines, so long as the result is unambiguous.
    So
    Code:
    switch(ch)
    {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
                printf("Vowel.");
                break;
    default:
                printf("Consonant.");
                break;
    }
    and
    Code:
    switch(ch) {
    case 'a': case 'e': case 'i': case 'o': case 'u': printf("Vowel."); break;
    default: printf("Consonant."); break;
    }
    are equivalent.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    11
    Thanks, mate. You provided some good valuable information. I appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-09-2016, 03:58 PM
  2. Switch/case vs if/else
    By ulillillia in forum C Programming
    Replies: 18
    Last Post: 09-19-2010, 05:21 AM
  3. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  4. Switch/Case using && != and ||?
    By Kespoosh in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2003, 11:24 PM
  5. switch case....
    By tetraflare in forum C++ Programming
    Replies: 1
    Last Post: 02-10-2003, 03:52 PM

Tags for this Thread