if-statement

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 (...

Check if variable is different from several values

If I want to take an action if php if a variable isn't 0,1, or 2, how would I do that? my if statement isn't working. Thanks! ...

How to create an if statement in javascript that will run if a variable is undefined?

Possible Duplicate: Detecting an undefined object property in JavaScript Would it be like this? if(variable == undefined) { or would it be like this? if(variable == "undefined") { ...

How to define a variable based on an if/then/else statement

I'm trying to translate some python code to haskell. However I reached a point where I'm not sure how to proceed. if len(prod) % 2 == 0: ss = float(1.5 * count_vowels(cust)) else: ss = float(count_consonants(cust)) # muliplicaton by 1 is implied. if len(cust_factors.intersection(prod_factors)) > 0: ss *= 1.5 return ss I'...

c++ return value

hi, I have the following code in c++: int fff ( int a , int b ) { if (a>b ) return 0; else a+b ; } although I didn't write 'return' after else it does not make error ! < br/> in main() when I wrote: cout<<fff(1,2); it printed 1 ? How did that happened can any one Explain that ? ...

Ignoring if statement

I have a facebook login system that seems to work fine, but I want to redirect to the home page if the user is logged in. SO this is the top of my page <?php session_start(); if(!empty($_SESSION)){ header("Location: home.php"); } But that gets ignored and it runs the login script as if $_SESSION is empty, even tho I print the Sessio...

Using the IF statement to see what intent started the activity - ANDROID

Hi there Hopefully this is very simple. I have a central activity that can be launched from two separate classes. I was hoping that in this central activity I could have an IF statement like if(this.getIntent() == MainMenu.class) { // Do something here } But obviously that isn't legal so how could I structure an expression to chec...

Check number of list items and columnize if condition exists

Example page: http://vincent-massaro.com/columns/columns.html On this page, there are two ordered lists, one with four items, and one with eight. I need to write a condition that checks how many list items are in each ordered list, and if it's more than four, split them into columns; if there are less than four, don't split into columns...

Most Efficient Way To Write This Loop VB.Net

Good afternoon all, I am beginning my first forays into programming and have decided to begin with VB.net as I can get VS2010 professional free through MS Dreamspark program. I have been following some basic tutorials online and am now writing a small program that runs a loop to add all the numbers together between two numbers input by ...

Help refactoring this code

Update: the much better answer has little to do with refactoring, but has to do with setting a default for empty keys. See the first answer- thanks a lot guys! Hi folks, how would you refactor this? if n=network_stats["b"] network_stats["b"] = n +1 else network_stats["b"]=1 end I have a nagging feeling this ...

refactor my if statement code

Hello, I've been messing with this bit of code for over an hour trying to rearrange it different ways. Is there any easier way to write it? if x is not Number ;// if x is string { if y is not Number ;// x, y both strings { Eval(x) Eval(y) return } else ...

ASP.net GridView - Use If-Statement in HeaderText property of BoundField

Can I do something like this: <asp:BoundField DataField="Field1" HeaderText='<% IF(Eval("Field2").ToString().SubString(3,4).Equals("Text3"),"Text1","Text2") %>' SortExpression="Field1" /> With the goal of having the header of Field1 be Text1 when the 4th-7th characters of Field2 = Text3 and Text2 otherwise? I tried it and it jus...

convert from if else to switch statement

I have the following if, else if, else construct and I am just curious how I could convert such as construct into a switch statement. var emailSubject = email.subject.toLowerCase(); if(emailSubject.indexOf("account request") >= 0){ //do acct req }else if(emailSubject.indexOf("accounts pending removal for") >= 0){ //do accou...

JQuery change function based on if the 'select' is more or less than a given value.

Hi All, This seems to be defeating me at the moment but I don't think I can be a million miles away. Basically I have a Age Verification form which just shows the year unless it's the year in which the day month would also affect the user meeting the age restriction, (in this case 1992 for 18) - in which case the day/month selects wo...

How to best convert VB6 "Select Case 1067 To 2938..." to C#?

I'm converting some VB6 logic to C# and have encountered the following SELECT/CASE statement. Select Case ZipCode Case 1067 To 19417, 35075 To 35085, 48455 To 48465, 55583 To 55596, 67480 To 67551, 75392, 85126, _ 93047 To 93059, 21217 To 21739, 35091 To 35096, 48480, 55606 To 55779, 67655 To 67707, 76726 To 76835, _ 85221 To 87...

Placing a foreach loop inside an if condition

I am needing to create a foreach statement that will run through and create conditions for a conditional statement. I wrote this bit of code, not expecting it to work, and of course it didn't... $filename = "whitelist.txt"; $handle = fopen($filename, 'r'); $whitelist = fread($handle, filesize($filename)); fclose($handle); $whitelist = e...

if [ $? -ne 0 ] then syntax error then unexpected

Hi all, I have been trying to execute the following unix shell script which is not working. I am running it by ksh. echo $?; if [ $? -ne 0 ] then failed $LINENO-2 $5 $6 fi failed() { echo "$0 failed at line number $1"; echo "moving $2 to failed folder" } This is giving an error saying "Synatx error:then unexpected". Basicall...

Deal with a lot of if-else , switch

Hi! What is the best way to deal with something like this : if(key==Space) { switch(weapon) { case GUN: p->shotGun(); break; case BOW: p->shotBow(); break; } } else if(key==Enter) { //... } else if(key==Up) { //... } ...

PHP Fatal error: Cannot break/continue

if (isset($errors)) { foreach ($errors as $error) { echo $error; } } else {break 2;} // some more code Outputs Fatal error: Cannot break/continue 2 levels I tried brake 1 not working either. ...

Excel Macro: how do I expand a ROW height to accomodate wrap text?

I'm slowly amending and expanding an If...ElseIf...Else statement (see post) to help me format a long list of categories and sub-categories (thanks marg & Lunatik). I've assigned fixed row heights to 90% of the range/lines. Now I'm stuck on those cells with lots of text that wraps over two lines in a cell. Two lines of text does not fit...