Thread: Having a problem with a Switch Statement

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    Quote Originally Posted by quzah View Post
    Code:
    if ... will always run this if check
    if ... will always run this check
    else
        will only run if that was not true
    Compare that with this:
    Code:
    if ... always runs this if check
    else if ... only after the first test was false
    else ... when neither was true...
    .
    So that could cause a conflict in more complicated programs?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ARod609 View Post
    So that could cause a conflict in more complicated programs?
    You have to think about what you want your program to do. Watch:
    Code:
    if( x == 1 )
        printf( "x is one\n" );
    else
    if( x == 2 )
        printf( "x is two\n" );
    else
        printf( "x is not one or two\n" );
    Compared to what you have:
    Code:
    if( x == 1 )
        printf( "x is one\n" );
    if( x == 2 )
        printf( "x is two\n" );
    else
        printf( "x is not one or two\n" );
    What is the output of this if x is 1? Is that what you wanted the program to do? You need to think through what you want to do.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    17
    Output
    Code:
    x is one
    x is not one or two
    I see, so the first "if" function and the "else" perform there functions because the code did not clarify what process it wanted to follow?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-30-2011, 01:59 PM
  2. Problem with a switch statement.
    By Merholtz in forum C Programming
    Replies: 3
    Last Post: 10-19-2008, 04:21 PM
  3. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  4. Replies: 1
    Last Post: 08-31-2004, 04:07 AM

Tags for this Thread