Thread: If, Else and switch statements

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    If, Else and switch statements

    The program should input the speed from the keyboard and display the appropriate message on the screen dependent on the speed the car is travelling.

    under 31mph-No action
    under 41mph-warning
    under 56mph-summons
    over 56-automatic ban

    this is how far i got

    Code:
    #include <stdio.h>
    void main (void)
    
    {
      int x;
      printf ("Enter teh car speed in mph: ");
      scanf ("%d", &x);
      if (x<31)
               printf ("No action");
    
    
    }
    I don't know what to do next. I know I have to use else/switch statement....but don't know how to use it. can any one help me?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Learning how to edit posts and use code tags properly would be a start

    Since there is no such thing as an else/switch statement, you'd better read the book again


    Try
    else if ( x >= 31 && x < 41 )
    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.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Using my novice logic ( )...I can see a simple way of doing that with just 3 IF statements and 1 ELSE statement.

    Try using a combination of AND or OR operators (like Salem's example) in each of those statements.

    CJ

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Using my novice logic ( ) ..I can see a simple way of doing that without AND or OR operators.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by Monster
    Using my novice logic ( ) ..I can see a simple way of doing that without AND or OR operators.
    Yeah...I can think of one as well. However, seeing as I am myself learning the general stuff of C right now..."I think" the AND and OR operators are an easy option.

    At least with what little knowledge I have I am trying to help a fellow programmer. Unlike some people I see who only post questions!

    CJ

Popular pages Recent additions subscribe to a feed