Be a Supporter!

C while switch loop

  • 470 Views
  • 1 Reply
New Topic Respond to this Topic
Hyperion
Hyperion
  • Member since: Oct. 10, 2003
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
C while switch loop 2005-05-04 09:37:03 Reply

In C i've been having a lot of trouble making while switch loops. Can anyone help?

Ravens-Grin
Ravens-Grin
  • Member since: Jun. 3, 2003
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to C while switch loop 2005-05-04 21:00:44 Reply

Ok let's do a simple breakdown of the while loop....

In C, in an expression, any number other then 0 is considered true. For example...

while(Value)
{
printf("I'm True if Value is not equal to one and I keep looping!");
}

or you can do it the other way

while(!Value)
{
printf("I'm true if value is equal to zero because the ! means not in C!");
}

Now the good ol' switch case.....
the basic template is this

switch ( expression )
{
case value_1 : statement
............................................
default : statement
}

The expression can be anything that gives (or returns which means you can even put a function in here!) an integer value; such as a long, int, double, or a char. Let me give an example..
(Note I didn't want to make a great UI at all)

int menu_choice=0;
printf("1 for this, 2 for that, 3 for what?");
scanf( "%d", &menu_choice);
switch(menu_choice)
{
case 1: printf("This!");
case 2: printf("That!");
case 3: printf("What?");
default: printf("Wrong value!!!");
}

My C might be a little broken, haven't used it in awhile.... but hopefully that clears it up for you.