An if statement is a form of conditional statement that enables or disables the execution of a group of statements based on the evaluation of a condition. More elaborate forms include the if-then-else statement and the if-then-elsif statement.
Questions tagged [if-statement]
33 questions
24
votes
6 answers
Best practice for redundant conditions in if-elif-else statements
What is considered better practice?
Case 1:
if n == 0:
doThis()
elif n < 0:
doThat()
elif n > 0:
doSomethingElse()
Case 2:
if n == 0:
doThis()
elif n < 0:
doThat()
else:
doSomethingElse()
The first case is more explicit and…
Nikhil Kumar
- 351
20
votes
3 answers
What is "short-circuiting" in C like languages?
I have heard of the term "short-circuiting" being used in C, C++, C#, Java, and many others. What does this mean and in what scenario would it be used?
fasil
- 301
15
votes
5 answers
If Else - Repeated Code Logic
My boss gave me a project with a particular logic. I have to develop a web page which has to lead the navigator through many cases until he/she arrives at the product.
This is the path scheme of the navigation in the site:
IMPORTANT!
In the…
Kevin Cittadini
- 261
6
votes
6 answers
Should an Else statement be used just for a comment?
I came across the following in some sample code:
if (urlStr)
{
NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile:urlStr];
node.nodeIcon = iconImage;
}
else
{
// it's a separator, don't bother with the icon
}
Is this style…
Steve Moser
- 297
6
votes
2 answers
Do nested conditionals have a significant performance impact?
This is a question that lives in my mind from a long time.
Does the use of multiple nested conditional statements impact the performance of a taken code? So far I know that programmers have created a precise term to describe this situation, the…
Aluminum
- 201
5
votes
3 answers
Changing large number of if-elif-else statements to use underlying structure
I have a function that looks something like this:
function_name(step, ... , typ):
if typ == 'some type of calc method':
if step == 1:
do_me_at_step_1(...)
elif step == 2:
do_me_at_step_2(...)
…
auden
- 1,657
4
votes
2 answers
How to structure many complex conditionals on a class
I have a class (as a protobuf) OrderChange, that represents when an order (imagine Amazon.com) changes:
message OrderChange {
Order old_order = 1;
Order new_order = 2;
}
message Order {
OrderType order_type = 1;
OrderCategory order_category…
onepiece
- 169
4
votes
1 answer
Is it always possible to separate multiple conditions in an IF statement into individual statements?
I'm trying to find the simplest way to model user-defined conditional statements without resorting to text parsing. This is fairly easy when there is only one condition in the statement because you can split that into an operand, a comparison…
leylandski
- 417
4
votes
2 answers
In an if statement, what are an "if clause" and a "then clause"?
I am a bit confused about the nomenclature for the parts of an if statement. Consider the following example:
1: if condition then
2: statement_1;
3: else
4: statement_2;
5: end if;
What is the "if clause" in this statement? Here are a…
rick
- 2,005
3
votes
3 answers
Representing a status as single letter strings
I worry that I'm too concerned with code smells. I've spent the last two days procrastinating over implementation details and how I would actively refuse using the approach suggested.
We have a scenario where we "pull" orders and each order can be…
Tez Wingfield
- 253
2
votes
3 answers
Is it good practice to eliminate zero in a statement if possible (e.g.:rewrite a-b>0 into a>b)?
Sometimes, I would write if-statements with conditions like it:
if(a-b>0){
}
but in fact, I can move 'b' from left hand side to right hand side:
if(a>b){
}
Similar cases like 'a-b!=0','a-b<=0' (not the case like 'a>0') may also applies it. My…
ocomfd
- 5,750
2
votes
4 answers
Avoiding if statements in Nested FOR loops
Please pardon me if this is a duplicate question.
I have two nested for loops which will iteration for around mn times (the complexity is around 3k).
Inside these for loops, I have 3 If conditions based on what I do certain operations. I am trying…
2
votes
2 answers
Highlighting importance of order when using short-circuited conditions
I was working on a piece of code when I noticed that an if statement could work or crash depending on the order used for the parts connected with and.
You can replicate the problem like this:
boolean func(String x) {
assert x
return true
}
v…
AK_is_curious
- 296
2
votes
3 answers
Avoiding two if statements for same condition with common code in between
This is a problem I run into often, and am looking for the best solution. I will have code like this (python):
def func(var, opt):
if opt:
var = var.set_opt(opt)
result = var.get_result()
if opt:
return [r[0] for r in…
shane
- 137
2
votes
2 answers
Long chained method calls contained within an if statement shown on a sequence diagram
Just wondering if there is a good way to do this? Currently i'm performing the method calls as if they were happening prior to the conditional block, then comparing what would be the result in "[isDoable == true]" with isDoable being the result of…
ldmccartin
- 23
- 1
- 3