Questions tagged [if-statement]

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.

31 questions
19
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
  • 291
  • 1
  • 2
  • 4
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…
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 Young
  • 1,637
  • 3
  • 11
  • 21
5
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
  • 287
  • 1
  • 6
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
  • 407
  • 1
  • 3
  • 14
4
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…
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
  • 1,945
  • 2
  • 19
  • 16
3
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…
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
  • 12
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,652
  • 8
  • 29
  • 37
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…
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
  • 1
  • 3
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…
2
votes
3 answers

Why doesn't the compiler assume the if statement condition is correct inside it?

First off all: sorry for the title, but I didn't now how to better formulate the meaning of my following question in a single phrase. While I was writing the following Swift code: if errorData.isMemberOfClass(UIAlertController) { …
Aluminum
  • 181
  • 1
  • 1
  • 6
1
2 3