# AMPL equivalent of case statement

 1 Hi All, I would like to know what is the ampl equivalent of the following: case: i = 1 to N-1 {  XXX; XXX;  } case i = N {  XXX; XXX;  } case i = N+1 to Z {  XXX; XXX;  } Thanks in advance asked 25 Jan '12, 01:28 Pavan 300●2●6●21 accept rate: 0%

 1 I'm not sure about the availability of "case" or "switch" statements in AMPL. Meanwhile, if your conditions for selecting commands are solely based on numbers, you might use multiple "for" or "repeat" statements. answered 25 Jan '12, 03:20 Ehsan ♦ 4.8k●3●11●22 accept rate: 16% Thanks for responding @Ehsan. What I did was, for{i in 1..Z} { if(iN && i<=Z) { XXX; XXX; } } Still I get an error with second 'if' condition saying that one is a syntax error. Not sure what might have gone wrong here? (25 Jan '12, 03:30) Pavan got it! in the second if loop, it should be "==" and not":=" (25 Jan '12, 03:37) Pavan 1 @Pavan: I think you need "then" after each condition in a "if" statement. You should also use "else if" instead of multiple "if"s structure to avoid unnecessary condition checkings. (25 Jan '12, 03:54) Ehsan ♦ @Ehsan: yeah I have then after if condition its just I forgot to type. I will make it "else if" -- thanks for that. (25 Jan '12, 04:19) Pavan
 toggle preview community wiki

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "Title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

Tags:

×127
×29
×28