if-else-statement

Is it acceptable to only use the 'else' portion of an 'if-else' statement?

Sometimes, I feel like it is easier to check if all of the conditions are true, but then only handle the "other" situation. I guess I sometimes feel that it is easier to know that something is valid, and assume all other cases are not valid. For example, let's say that we only really care about when there is something wrong: object va...

What is the most terse syntax check for checking preconditions and then calling a switch statement?

What is the most terse syntax to combine a check for some preconditions with a switch statement? Can I combine an if/else and switch statement? if (!IsValid(text)) { DoSomeLogging(); } else { switch (text) { case "1": DoSomething(); break; case "2" DoSomethingElse(); break; default...

Alternative conditional syntax (if-else) failing on PHP 5.3.0 (xampp)

Hello, I recently upgraded to xampp v1.7.2 which dumped PHP 5.3 on me. Along with that all my httpd.confs and php.ini's got wiped out (I had taken it for granted that this release will be an upgrade like all the earlier xampp releases). Anyway, it took me a while to reconfigure all the services - but now I've run into a funny problem....

If else or case statement help!

Hi I could really use some help with the best way to accomplish the following: I have the below in my controller (and I know this should not be here and needs to move to the model) This is an email messaging system so according to what position you hold you are able to email out to set groups of people. So if you are Battalion Commande...

PHP, create_function or evalute it at runtime?

Hi all, i have a class with some method that depend by one parameter. What is the best way to write this method? Example: First way class Test{ var $code; function Test($type){ if($type=="A"){ $this->code=create_function(/*some args and some code*/); } else if($type=="B"){ $this->code=create_functi...

Double If Else Problem in C#

I constantly find myself writing similar code like the example below: if (object["Object Name"] != null) { if (object["Object Name"] == "Some Value") { // Do Statement A } else { // Do Statement B } } else { // Do Statement B } The problem here is that I much check if an object is null or not and then I...

AS3 Compiler error 1083: problem with else syntax

Hey there, I'm creating a simple click and scroll for a future menu for my personal site. I have a box, I called it thing_mc, and I have 3 positions for it. I have a next and prev. button that will control thing_mc position. I'm using TweenLite to animate thing_mc, if that makes a difference. I get a 1083 error (...else is unexpected) a...

Is there a reason to prefer a switch over an if statement with only one condition?

I found the following code in my team's project: Public Shared Function isRemoteDisconnectMessage(ByRef m As Message) isRemoteDisconnectMessage = False Select Case (m.Msg) Case WM_WTSSESSION_CHANGE Select Case (m.WParam.ToInt32) Case WTS_REMOTE_DISCONNECT isRemoteDisconnect...

Comparison of conditional statements.

Are following ways are same.(Considering the evaluation time) if(Condition1) { //code1 } else { //code2 } condition1 ? code1 : code2 Are they just syntactically different? It may seem a stupid question.But I want to clear my doubt. ...

Ifstatement used to make a functionality button appear?

I am on my 2nd day(16th hour) of trying to get my delete button to do what I want with PHP. I have a site that is a social network that has user profiles. Users can leave comments on another users profile. I am trying to put a delete link in a designated area that only shows up if you are viewing your own profile and goes away when you a...

Which is faster - if..else or Select..case?

I have three condition to compare. Which one is more faster between the following two? Please point me out. Thanks all! If var = 1 then Command for updating database ElseIf var = 2 then Command for updating database ElseIf var = 3 then Command for updating database EndIf and Select Case var Case 1 Command for up...

PHP If/Else - More efficent way to do this?

I have a if function that works out how much of a users profile is completed however the way I include below was the best I could think of, however it seems really inefficient. What is the better way to do this? if($user['first_name']!==""&&$user['last_name']!==""&&$user['pemail']!==""&&$user['dob']!==""&&$user['ambitions']!==""&&$use...

Odd Perl conditional operator behavior

I'm doing some work in Perl and I ran across an odd result using the conditional operator. The code in question: ($foo eq "blah") ? @x = @somearray : @y = ("another","array"); Trying to compile this code results in the error "Assignment to both a list and a scalar at XXX line YY, near ');'". In trying to pinpoint the source of the er...

Search for current website title and host bookmarklet

I have a bookmarklet which gets current website TITLE and search for it on my website: javascript:q=(document.location.host);void(open('http://example.com/search.php?search='+document.title,'_self','resizable,location,menubar,toolbar,scrollbars,status')); But now I would like to change this bookmarklet so it will search for current web...

C++ String manipulation - if stament

I have the following code that works correctly. However after I add an else statement anything always evaluates to else wgetstr(inputWin, ch); //get line and store in ch variable str = ch; //make input from char* to string if(str=="m" || str=="M"){ showFeedback("Data Memory Updated"); } if(str=="p" || ...

If / else order sequence issue

I have the following set up, a ddl (ddlProd, radBuyer) and autocomplete text box (txtProdAC, radProd) that when populated and their respective radio buttons are selected, a grid view of the data is produced...lovely stuff. protected void btSearch_Click(object sender, EventArgs e) { lqPackWeights.WhereParameters.Clear(); ...

Declaring a variable in an if-else block in C++

I'm trying to declare a variable in an if-else block as follows: int main(int argc, char *argv[]) { if (argv[3] == string("simple")) { Player & player = *get_Simple(); } else if (argv[3] == string("counting")) { Player & player = *get_Counting(); } else if (argv[3] == string("competitor")) { Player &...

How to reduce the number of if-else statements in PHP?

I found that there are many if-else statements, especially nested if else statements, these statements make my code less readable. How to reduce the number of if else statements in PHP? My tips are as follows: 1.Use a switch statement when it is suitable; 2.use exit() statement when it is feasible; 3. Use ternary statement when it is fe...

Using return instead of else in Javascript

I am working on a project which requires some pretty intricate Javascript processing. This includes a lot of nested if-elses in quite a few places. I have generally taken care to optimise Javascript as much as possible by reading the other tips in SO, but I am wondering if the following two constructs would make any difference in terms o...

If input name found in SQL then do else do… with an SQL query

Im trying to write an SQL query that will check in the table ‘persons’ in the column ‘rName’ for say a name “jack” and if it exists then UPDATE that row else INSERT a new row with the new rName. I’v been trying out IF/ELSE statements but haven’t really seen how they work. Or is there a better way to do what I want without If/ELSE? (I ha...