Jump to content

Switch and cases in C.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
Okay, hai.

first proper tutorial, be kind. =P

Cases, as far as I know can only go from an integer, so we are going to start by declaring said integer. =D

#include <stdio.h>
main()
{
int age;

We will call the integer age, because it's a number that can vary and you can give relevant different responses too so it makes it ideal.

We now want a printf, and a scanf to ask them to enter their age.
printf("Please enter your age\n");
scanf("%d", age);

Now our code has a main, an int, a printf and a scanf, it's time for our switch! ^_^. I'll put it into the whole code so it is easily understandable.

#include <stdio.h>

main()
{
      int         age;
      printf("Please enter your age\n");
      scanf("%d", age);
      
      switch (age)
      {
      case 1: 
           printf("Congratulations on your first birthday\n");
           break;
      case 16:
           printf("You can now legally have sex\n");
           break;
      case 18:
           printf("You can now vote!\n");
           break;
      case 21:
           printf("You are now twenty-one, enjoy the good years!\n");
           break;
      default:
           printf("We don't care about your age!\n");
           break;
      }
      fflush(stdin);
      getchar();
}

the switch (age) is a fancy if statement, doesn't declare anything. each case is if that number is entered in the age variable.

default statement: The default statement is if it doesn't match any of the above cases, do this. ^^ Like a final else after an if.

We use breaks to make the code know to stop and that it's the end of that segment so it does not keep going through each case.

it then prints out each option, and breaks.

as a side note:

Cases are easier to read than a whole group of if else statements.
Cases are faster, especially if the program you are writing is huge.

Hope this helps somebody. :)

Edit: thanks to Jordan for his suggestion!

Edited by Phoenixz, 17 January 2009 - 09:44 AM.

Posted Image

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Very nice tutorial. There are two things I would add to your tutorial though: an explanation on the "default" statement, and why you use/have "breaks" under each case statement.

+rep.

#3
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
ah, yeah i'll add that now, thanks!
Posted Image

#4
Egz0N

Egz0N

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,034 posts
nice tutorial dude .. +rep :)

#5
MathX

MathX

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,001 posts
Great!!! +rock for u :D

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#6
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
odd, my reputation power is still on 1, graphical bug or some? ;o

edit: also hoped it helped one of you. ^^
edit2: Almost forgot this ;_;, Thanks to Wingedpanther for giving me the idea to do a tutorial, when he said it's a good idea to do them because you learn stuff in the proccess, which I did. :)

Edited by Phoenixz, 17 January 2009 - 01:46 PM.

Posted Image

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Your rep power is different from your reputation. There are several factors that calculate rep power. To see your reputation click on UserCP and scroll down to the bottom.

Posted via CodeCall Mobile

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
+rep'd. I like seeing people help out with the structure of C/C++, and switch statements can be a bit odd.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
Zizooooo

Zizooooo

    Newbie

  • Members
  • PipPip
  • 15 posts
how easy and interesting explanation;
in VB :
Dim Phoenixz As String

Phoenixz = InputBox("Enter ur opinion of Phoenixz")

Select Case Phoenixz

Case "Excellent"

Print "Phoenixz is excellent"

Case "Wonderful"

Print "Phoenixz is wonderful"

Case Else

Print "Phoenixz is The best C programmer"

End Select
:) :) :) :)
greetings Zizooooo

#10
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts

Quote

Cases are faster, especially if the program you are writing is huge.

Not always, and it depends on the compiler. If the compiler determines that the tradeoff is acceptable, it'll construct a jump table based on your switch statement. For very large switches (like a state machine) this is fairly good speedup.

#11
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
Actually this tutorial has a lot of errors.


You probably didn't even run your code, or maybe you have compiled it on GCC, because on VC++ it starts with a run time error:

"Run-Time Check Failure #3 - The variable 'age' is being used without being initialized."


Well, it compiles successfully on GCC, however, if i compile it using -Wall it will display a lot of basic warnings:

t.c:4: warning: return type defaults to `int'
t.c: In function `main':
t.c:7: warning: format argument is not a pointer (arg 2)
t.c:29: warning: control reaches end of non-void function


You didn't specified int main, however the compiler assumes main as int, but in the end you didn't even specified the return value, but the worst problem it's that you didn't passed the reference of the variable in scanf function, which ends in a logic error, you didn't pass the input value to the variable, so in your switch statement will always end in default.


Rep- (just for the next time you pay more attention)

#12
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Avoid fflush(stdin).