Hard Light Productions Forums

Off-Topic Discussion => Programming => Topic started by: Dysko on June 15, 2008, 09:18:55 am

Title: Switches in C
Post by: Dysko on June 15, 2008, 09:18:55 am
Can I insert a switch into another switch cycle in C?
Example:
Code: [Select]
switch(variable1)
   {
   case variable1==1:
      switch(variable2)
      {
      case variable2==2:
         do this;
      default:
         do another thing;
      }
   default:
      do that;
   }
Would switch(variable2) work?
Title: Re: Switches in C
Post by: Ghostavo on June 15, 2008, 09:33:12 am
Can I insert a switch into another switch cycle in C?
Example:
Code: [Select]
switch(variable1)
   {
   case variable1==1:
      switch(variable2)
      {
      case variable2==2:
         do this;
      default:
         do another thing;
      }
   default:
      do that;
   }
Would switch(variable2) work?

Yes, you can put switchs inside switchs.

But more important, why do you use
Code: [Select]
case variable# == #: when you are comparing the expression variable# == # to the value of variable# instead of just
Code: [Select]
case #:
P.S.
Also, nitpicking, a switch is not a cycle, it's a flow control statement. :p.
Title: Re: Switches in C
Post by: Dysko on June 15, 2008, 09:54:59 am
Yes, you can put switchs inside switchs.
Thank you! :yes:

But more important, why do you use
Code: [Select]
case variable# == #: when you are comparing the expression variable# == # to the value of variable# instead of just
Code: [Select]
case #:
Because our programming professor never told us how to use switches, and they are the most effective way to do a part of my programming homework.
I searched on the internet how to use them, and looks like I misunderstood something :nervous:

P.S.
Also, nitpicking, a switch is not a cycle, it's a flow control statement. :P.
Ops. :nervous:
Title: Re: Switches in C
Post by: Wanderer on June 16, 2008, 01:49:37 pm
Kinda old but still referenced at times.... http://publications.gbdirect.co.uk/c_book/chapter3/flow_control.html
Title: Re: Switches in C
Post by: WMCoolmon on June 16, 2008, 08:44:42 pm
The only unusual thing about switches is that you have to use {} if you initialize variables. Otherwise they're just like any other conditional statement.