Questions tagged [conditions]
128 questions
85
votes
11 answers
Why do we have to use break in switch?
Who decided (and based on what concepts) that switch construction (in many languages) has to use break in each statement?
Why do we have to write something like this:
switch(a)
{
case 1:
result = 'one';
break;
case 2:
…

trejder
- 2,386
35
votes
7 answers
How can I reformat my condition to make it better?
I have a condition
if(exists && !isDirectory || !exists)
{}
how can I modify it, so that it may be more understandable.

Spynet
- 459
31
votes
3 answers
Approaches for checking multiple conditions?
What is the best practice for checking multiple conditions, in no particular order?
The example in question needs to check four distinct conditions, in any order, and fail showing the correct error message.
The examples below use a C-like…

Redandwhite
- 419
4
votes
3 answers
Ifology - how to write this statement better?
I'm wondering how to write the if statement in the following block in a better way. It's supposed to operate when $a is 14, 22, 30 and for all following values at intervals of 8, up to some limit. The current way is obviously not good since the…

N Kristijan
- 51
4
votes
4 answers
Should I repeat condition checking code or put it in a function?
I have a bunch of calls to a method that may or may not need to be made depending on whether certain features are enabled or not. As such, I've wrapped these calls in if blocks checking the enabled statuses.
The arguments to the method in each of…

paul
- 2,084
1
vote
3 answers
Enforcing order for two consecutive statements
I have the following code:
subroutine foo(int index)
{
// Check A.
// Critical: Check A must precede Check B below.
if (index == 1)
{
return true;
}
// Check B.
if (index - 2 < 0)
{
return false;
…
user88438
0
votes
3 answers
When not to use early return?
Nesting is unavoidable, however in most cases returning early is a more viable option.
Consider the following snippet:
MyRoutine(argument)
{
if (0 == argument) {
SubRoutine(argument);
} else if (1 == argument) {
…

Misiur
- 109
0
votes
2 answers
ternary operator usage within IF brackets
could you please help me understand this ternary operator
public static void replace(List list, E val, E newVal) {
for (ListIterator it = list.listIterator(); it.hasNext(); )
if (val == null ? it.next() == null :…
-2
votes
3 answers
Nested if statements or multiple if statements
Which way is better?
-- Option 1-------------
if ( condition1 )
{
statement1
}
else
{
exit program
}
if ( condition2 )
{
statement2
}
else
{
exit program
}
----Option 2------
if ( condition1 )
{
statement 1
…