Why use ++i instead of i++ in cases where the value is not used anywhere else in the statement?
If we ignore force of habit, '++i' is a simpler operation conceptually: It simply adds one to the value of i, and then uses it.
i++
on the other hand, is "take the original value of i
, store it as a temporary, add one to i
, and then return the temporary". It requires us to keep the old value around even after i
has been updated.
And as Konrad Rudolph showed, there can be performance costs to using i++
with user-defined types.
So the question is, why not always just default to ++i
?
If you have no reason to use `i++´, why do it? Why would you default to the operation which is more complicated to reason about, and may be slower to execute?
Why is else rarely used after if x then return?
In my experience, it depends on the code. If I'm 'guarding' against something, I'll do:
if (inputVar.isBad()) {
return;
}
doThings();
The point is clear: If that statement is false, I don't want the function to continue.
On the other hand, there are some functions with multiple options, and in that case I would write it like this:
if (inputVar == thingOne) {
doFirstThing();
} else if (inputVar == secondThing) {
doSecondThing();
} else {
doThirdThing();
}
Even though it could be written as:
if (inputVar == thingOne) {
doFirstThing();
return;
}
if (inputVar == thingTwo) {
doSecondThing();
return;
}
doThingThree();
return;
It really comes down to which way most clearly shows what the code is doing (not necessarily which bit of code is shortest or has the least indentation).
Using two values for one switch case statement
You can use have both CASE
statements as follows.
case text1:
case text4:{
//blah
break;
}
SEE THIS EXAMPLE:The code example calculates the number of days in a particular month:
class SwitchDemo {
public static void main(String[] args) {
int month = 2;
int year = 2000;
int numDays = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0) &&
!(year % 100 == 0))
|| (year % 400 == 0))
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("Invalid month.");
break;
}
System.out.println("Number of Days = "
+ numDays);
}
}
This is the output from the code:
Number of Days = 29
FALLTHROUGH:
Another point of interest is the break statement. Each break statement
terminates the enclosing switch statement. Control flow continues with
the first statement following the switch block. The break statements
are necessary because without them, statements in switch blocksfall
: All statements after the matching case label are executed in
through
sequence, regardless of the expression of subsequent case labels,
until a break statement is encountered.
EXAMPLE CODE:
public class SwitchFallThrough {
public static void main(String[] args) {
java.util.ArrayList<String> futureMonths =
new java.util.ArrayList<String>();
int month = 8;
switch (month) {
case 1: futureMonths.add("January");
case 2: futureMonths.add("February");
case 3: futureMonths.add("March");
case 4: futureMonths.add("April");
case 5: futureMonths.add("May");
case 6: futureMonths.add("June");
case 7: futureMonths.add("July");
case 8: futureMonths.add("August");
case 9: futureMonths.add("September");
case 10: futureMonths.add("October");
case 11: futureMonths.add("November");
case 12: futureMonths.add("December");
default: break;
}
if (futureMonths.isEmpty()) {
System.out.println("Invalid month number");
} else {
for (String monthName : futureMonths) {
System.out.println(monthName);
}
}
}
}
This is the output from the code:
August
September
October
November
December
Using Strings in switch Statements
In Java SE 7 and later, you can use a String object in the switch
statement's expression. The following code example, ,
displays the number of the month based on the value of the String
named month:
public class StringSwitchDemo {
public static int getMonthNumber(String month) {
int monthNumber = 0;
if (month == null) {
return monthNumber;
}
switch (month.toLowerCase()) {
case "january":
monthNumber = 1;
break;
case "february":
monthNumber = 2;
break;
case "march":
monthNumber = 3;
break;
case "april":
monthNumber = 4;
break;
case "may":
monthNumber = 5;
break;
case "june":
monthNumber = 6;
break;
case "july":
monthNumber = 7;
break;
case "august":
monthNumber = 8;
break;
case "september":
monthNumber = 9;
break;
case "october":
monthNumber = 10;
break;
case "november":
monthNumber = 11;
break;
case "december":
monthNumber = 12;
break;
default:
monthNumber = 0;
break;
}
return monthNumber;
}
public static void main(String[] args) {
String month = "August";
int returnedMonthNumber =
StringSwitchDemo.getMonthNumber(month);
if (returnedMonthNumber == 0) {
System.out.println("Invalid month");
} else {
System.out.println(returnedMonthNumber);
}
}
}
The output from this code is 8.
FROM Java Docs
When is post-decrement/increment vs. pre-decrement/increment used in real-life examples?
The obvious is when you want the old value returned, you use post-increment.
The more subtle things are that pre-increment should really never be slower and could be faster due to the lack of creating a temporary and returning the old value when using post-increment.
A real scenario for using post-increment in C++ is when erasing from standard containers. For example:
set<int> ctr;
ctr.insert(1);
ctr.insert(10);
ctr.insert(12);
ctr.insert(15);
set<int>::iterator it = set.begin();
// Post-increment so the value returned to erase is that of the previous
// iteration (i.e. begin()), yet the iterator stays valid due to the local
// iterator being incremented prior to the erase call
ctr.erase(it++);
// Still valid iterator can be used.
cout << "Value: " << *it << "\n";
In response to the compiler optimization, it is true yet I think it's always important to convey as precisely as possible what you're trying to accomplish. If you don't need the returned value from x++, then don't ask for it. Also, I'm not sure you would always get the same optimization if the type your incrementing is not a simple type. Think iterators that are not just plain pointers. In cases such as this, your mileage may vary with regard to optimization. In short, always pre-increment unless you need the returned (i.e. old value) of the post-increment operator.
Why Switch/Case and not If/Else If?
Summarising my initial post and comments - there are several advantages of switch
statement over if
/else
statement:
Cleaner code. Code with multiple chained
if
/else if ...
looks messy and is difficult to maintain -switch
gives cleaner structure.Performance. For dense
case
values compiler generates jump table, for sparse - binary search or series ofif
/else
, so in worst caseswitch
is as fast asif
/else
, but typically faster. Although some compilers can similarly optimiseif
/else
.Test order doesn't matter. To speed up series of
if
/else
tests one needs to put more likely cases first. Withswitch
/case
programmer doesn't need to think about this.Default can be anywhere. With
if
/else
default case must be at the very end - after lastelse
. Inswitch
-default
can be anywhere, wherever programmer finds it more appropriate.Common code. If you need to execute common code for several cases, you may omit
break
and the execution will "fall through" - something you cannot achieve withif
/else
. (There is a good practice to place a special comment/* FALLTHROUGH */
for such cases - lint recognises it and doesn't complain, without this comment it does complain as it is common error to forgotbreak
).
Thanks to all commenters.
Can we write case statement without having else statement
A case
expression can only manipulate the value of an expression, not remove rows from the result. If you want to omit the null
s from the result, you'll have to add a where
clause:
SELECT CASE WHEN id = 1 THEN 'A'
WHEN id = 2 THEN 'B'
END
FROM test
WHERE id IN (1, 2) -- HERE
SQL: IF clause within WHERE clause
Use a CASE statement
UPDATE: The previous syntax (as pointed out by a few people) doesn't work. You can use CASE as follows:
WHERE OrderNumber LIKE
CASE WHEN IsNumeric(@OrderNumber) = 1 THEN
@OrderNumber
ELSE
'%' + @OrderNumber
END
Or you can use an IF statement like @N. J. Reed points out.
IF' in 'SELECT' statement - choose output value based on column values
SELECT id,
IF(type = 'P', amount, amount * -1) as amount
FROM report
See http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html.
Additionally, you could handle when the condition is null. In the case of a null amount:
SELECT id,
IF(type = 'P', IFNULL(amount,0), IFNULL(amount,0) * -1) as amount
FROM report
The part IFNULL(amount,0)
means when amount is not null return amount else return 0.
Why does python use 'else' after for and while loops?
It's a strange construct even to seasoned Python coders. When used in conjunction with for-loops it basically means "find some item in the iterable, else if none was found do ...". As in:
found_obj = None
for obj in objects:
if obj.key == search_key:
found_obj = obj
break
else:
print('No object found.')
But anytime you see this construct, a better alternative is to either encapsulate the search in a function:
def find_obj(search_key):
for obj in objects:
if obj.key == search_key:
return obj
Or use a list comprehension:
matching_objs = [o for o in objects if o.key == search_key]
if matching_objs:
print('Found {}'.format(matching_objs[0]))
else:
print('No object found.')
It is not semantically equivalent to the other two versions, but works good enough in non-performance critical code where it doesn't matter whether you iterate the whole list or not. Others may disagree, but I personally would avoid ever using the for-else or while-else blocks in production code.
See also [Python-ideas] Summary of for...else threads
Can dplyr package be used for conditional mutating?
Use ifelse
df %>%
mutate(g = ifelse(a == 2 | a == 5 | a == 7 | (a == 1 & b == 4), 2,
ifelse(a == 0 | a == 1 | a == 4 | a == 3 | c == 4, 3, NA)))
Added - if_else: Note that in dplyr 0.5 there is an if_else
function defined so an alternative would be to replace ifelse
with if_else
; however, note that since if_else
is stricter than ifelse
(both legs of the condition must have the same type) so the NA
in that case would have to be replaced with NA_real_
.
df %>%
mutate(g = if_else(a == 2 | a == 5 | a == 7 | (a == 1 & b == 4), 2,
if_else(a == 0 | a == 1 | a == 4 | a == 3 | c == 4, 3, NA_real_)))
Added - case_when Since this question was posted dplyr has added case_when
so another alternative would be:
df %>% mutate(g = case_when(a == 2 | a == 5 | a == 7 | (a == 1 & b == 4) ~ 2,
a == 0 | a == 1 | a == 4 | a == 3 | c == 4 ~ 3,
TRUE ~ NA_real_))
Added - arithmetic/na_if If the values are numeric and the conditions (except for the default value of NA at the end) are mutually exclusive, as is the case in the question, then we can use an arithmetic expression such that each term is multiplied by the desired result using na_if
at the end to replace 0 with NA.
df %>%
mutate(g = 2 * (a == 2 | a == 5 | a == 7 | (a == 1 & b == 4)) +
3 * (a == 0 | a == 1 | a == 4 | a == 3 | c == 4),
g = na_if(g, 0))
Related Topics
C++ Object Created with New, Destroyed with Free(); How Bad Is This
How to Safely Average Two Unsigned Ints in C++
Inheriting Private Members in C++
C++: Wrapping Vector<Char> with Istream
How to Tame the Windows Headers (Useful Defines)
Default VS. Implicit Constructor in C++
Window C/C++ Crypto API Examples and Tips
C++ Can Compilers Inline a Function Pointer
Portable Text Based Console Manipulator
What Would a Std::Map Extended Initializer List Look Like
Which Greedy Initializer-List Examples Are Lurking in the Standard Library