LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 11-13-2004, 01:33 PM   #1
ripwheels8
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.
ripwheels
 
Old 11-13-2004, 02:05 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,065

Rep: Reputation: 295Reputation: 295Reputation: 295
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, 02:31 PM   #3
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

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

Code:
for(;;)
 switch{
  case '0':
    ...
  case '1':

}
 
Old 11-13-2004, 03:10 PM   #4
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

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

What ripwheels8 refers to:
Code:
for(;;) /* Endless Loop */
 switch(x){
  default:
    ...
   case 0:
    ...
   case 1:
    ...
/* And so forth */
 }
I think btmiller refers to:

Code:
for(x = VALUE; x != SET; ...){
  ...
}
 
Old 11-13-2004, 04:47 PM   #5
karlan
Member
 
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
 
  


Reply


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 05:53 PM
switch statements and input protection (in c) veritas Programming 2 09-27-2004 07:43 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM
if statements thesnaggle Linux - Software 1 02-16-2004 09:52 AM
PHP: egrep For Loops and If Statements jhrbek Programming 14 12-31-2003 12:26 PM


All times are GMT -5. The time now is 06:00 AM.

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