if-then-else

Should I assume two-elements enums will remain this way?

It happens quite a lot that I've a two member enum : enum E{ A, B } Let's say I want to assign a different value to i according to my enum value. should I write this : int i= (e== E.A)?0:1; or this : int i; if (e==E.A) i=0; else if (e==E.B) ...

Where to put comments in an IF THEN ELSE construct

I never decided on what the best way is to comment IF-THEN-ELSE constructs, so I never standardized on a consistent way to comment them. I appreciate any insights. Some options: a) IF (blabla) { // this comment explains what happens in the IF case dothis(); } else { // this comment explains what happens in the ELSE case d...

Best Practice on IF/ELSE Statement Order

Which is a better practice? (I'm coding in .Net if that makes a difference) IF condition = true THEN ...true action--even if rare... ELSE ...action END IF or IF condition = [most common condition] THEN ...most common action.... ELSE ...least common action END IF ...

MGrammar for parsing IF statement

I am building up a MGrammar spec to parse some pseudo code looking for particular bits of information. I have most of the spec working except for 1 cruical element. The pseudo code supports an if-then-else syntax and I have been unable to find a satisfactory way of parsing it. The exact construct is... IF expression operator expres...

C++ "if then else" template substitution

I would like to declare a template as follows: template <typename T> { if objects of class T have method foo(), then const int k=1 else if class has a static const int L then const int k=L else const int k=0; } How can I do this? In general, I would like a mechanism for setting static consts based on propert...

Applescript testing for file exist

OK, I thought this would be a simple one, but apparently I'm missing something obvious. My code is as follows: set fileTarget to ((path to desktop folder) & "file$") as string if file fileTarget exists then display dialog "it exists" else display dialog "it does not exist" end if Easy right? Unfortunately, when I run the scri...

Should 'else' be kept or dropped in cases where it's not needed?

This is a sort of trivial question but something I've been wondering about. In terms of style (I assume the performance is identical), is it better to keep an 'else' in an if statement where it's not necessary? For example, which of the following is better: if (x < 10) doSomething(); else if (x > 20) doSomethingElse(); or if (...

something about JOptionPane.showInputDialog in java

Hi, i'm new in java and have a phase of code like this: import javax.swing.JOptionPane; public class test { public static void main(String[] args) { String value=JOptionPane.showInputDialog("please input your value"); if (value== "1"){ System.out.println("1"); }else{ System.out.println("not 1"); } } } Question : why ...

C++ design pattern to get rid of if-then-else

I have the following piece of code: if (book.type == A) do_something(); else if (book.type == B) do_something_else(); .... else do so_some_default_thing. This code will need to be modified whenever there is a new book type or when a book type is removed. I know that I can use enums and use a switch statement. Is there a design patte...