Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-13-2004, 02:33 PM   #1
LQ Newbie
Registered: Jul 2004
Posts: 9

Rep: Reputation: 0
C Loops and switch statements

I am trying to write a program using a switch statment in a loop until a certain variable is entered. What command do I use and how do I place the switch inside the loop? Your assistance is greatly appreciated. Thank you.
Old 11-13-2004, 03:05 PM   #2
Senior Member
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,284

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
You could have a variable that is set when the value you want to use to the exit the loop is set, and then check for it at the top of the loop (breaking if it is set). The ugly and unrecommended way to do it, is to use a goto from within the switch statement itself, in the case you want to cause to exit the loop.
Old 11-13-2004, 03:31 PM   #3
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
You could do it of many ways.
The way you refer to:

  case '0':
  case '1':

Old 11-13-2004, 04:10 PM   #4
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
I had a problem posting the last thread.

What ripwheels8 refers to:
for(;;) /* Endless Loop */
   case 0:
   case 1:
/* And so forth */
I think btmiller refers to:

for(x = VALUE; x != SET; ...){
Old 11-13-2004, 05:47 PM   #5
Registered: Aug 2003
Location: San Francisco, California
Distribution: Slackware
Posts: 158

Rep: Reputation: 30
well, think about how you would do this:

assign a variable inside your program.
you would look between the curly braces of main, right? RIGHT!

so if you want to nest something in side a loop, just place it within the curly braces. Don't get confused about what braces match! You can also nest as many things as you wish, NO limit


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
switch statements calling a function tekmorph Programming 2 10-19-2004 06:53 PM
switch statements and input protection (in c) veritas Programming 2 09-27-2004 08:43 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 03:41 AM
if statements thesnaggle Linux - Software 1 02-16-2004 10:52 AM
PHP: egrep For Loops and If Statements jhrbek Programming 14 12-31-2003 01:26 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:26 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration