Off-Topic Discussion > Programming

Switches in C

(1/1)

Dysko:
Can I insert a switch into another switch cycle in C?
Example:

--- Code: ---switch(variable1)
   {
   case variable1==1:
      switch(variable2)
      {
      case variable2==2:
         do this;
      default:
         do another thing;
      }
   default:
      do that;
   }
--- End code ---
Would switch(variable2) work?

Ghostavo:

--- Quote from: Darth DySkO on June 15, 2008, 09:18:55 am ---Can I insert a switch into another switch cycle in C?
Example:

--- Code: ---switch(variable1)
   {
   case variable1==1:
      switch(variable2)
      {
      case variable2==2:
         do this;
      default:
         do another thing;
      }
   default:
      do that;
   }
--- End code ---
Would switch(variable2) work?

--- End quote ---

Yes, you can put switchs inside switchs.

But more important, why do you use
--- Code: ---case variable# == #:
--- End code ---
when you are comparing the expression variable# == # to the value of variable# instead of just
--- Code: ---case #:
--- End code ---

P.S.
Also, nitpicking, a switch is not a cycle, it's a flow control statement. :p.

Dysko:

--- Quote from: Ghostavo on June 15, 2008, 09:33:12 am ---Yes, you can put switchs inside switchs.

--- End quote ---
Thank you! :yes:


--- Quote from: Ghostavo on June 15, 2008, 09:33:12 am ---But more important, why do you use
--- Code: ---case variable# == #:
--- End code ---
when you are comparing the expression variable# == # to the value of variable# instead of just
--- Code: ---case #:
--- End code ---

--- End quote ---
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:


--- Quote from: Ghostavo on June 15, 2008, 09:33:12 am ---P.S.
Also, nitpicking, a switch is not a cycle, it's a flow control statement. :P.

--- End quote ---
Ops. :nervous:

Wanderer:
Kinda old but still referenced at times.... http://publications.gbdirect.co.uk/c_book/chapter3/flow_control.html

WMCoolmon:
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.

Navigation

[0] Message Index

Go to full version