Skip to main content

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.

Filter by
Sorted by
Tagged with
0 votes
5 answers
189 views

What is the best way to express this code? (It was difficult to phrase in words) if cond1 or cond2: # shared code if cond1: # cond1 code else: # cond2 code The above ...
hl5619's user avatar
  • 9
24 votes
6 answers
9k views

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: ...
Nikhil Kumar's user avatar
-1 votes
3 answers
1k views

I have a number of non-nested if statements that look like this: if (!bytes[nameof(PropertyVersion.Price)].SequenceEqual(dbBytes[nameof(PropertyVersion.Price)])) changes += $"Price {TextHelper....
Azhari's user avatar
  • 115
4 votes
2 answers
364 views

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 { ...
onepiece's user avatar
  • 169
-1 votes
1 answer
207 views

I have a method where I fetch user input, check if certain values exist, and based on that build my own custom input object that I would use to search in a database. The code for the search method is ...
AnOldSoul's user avatar
  • 173
-1 votes
1 answer
877 views

I'm working a project where I found another developer wrote a method as you see it below. How would I clean those IF statements and refactor this method. Also is it ok ti set a variable to nil ? def ...
egyamado's user avatar
  • 117
1 vote
2 answers
2k views

I'm working on a website using server-side rendering approach. I'm using React and Redux for this. Redux handles the state of the application and provides API to change the state. All asynchronous ...
Yos's user avatar
  • 167
0 votes
0 answers
488 views

I'm working on a feature where users can get data based on the if statement they write. The if statement looks something like the excel's conditionals. Basic syntax: IF ( lhs == rhs, ifTrue, ifFalse)...
Shahlin Ibrahim's user avatar
0 votes
1 answer
469 views

for (int i = lstReportCount - 1; i >= 0; i--){ if ( (input.ServiceTypes == "1" && lstReport[i].A == 0) || (input.ServiceTypes == "2" && lstReport[i].B == 0) || (input....
xCyrusT's user avatar
  • 13
0 votes
1 answer
540 views

I was wondering what are the pro and cons between having one function with a if statement dictating 2 sections of code or two discreet functions that are called on separately. In terms of pro and ...
Drummerboy2543's user avatar
0 votes
1 answer
394 views

I am reviewing best practice with if statements. Below in example A I include the entire code to be run and ensure it remains within the if statement. While for example B the code to be executed ...
safesploit's user avatar
5 votes
3 answers
13k views

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 ...
auden's user avatar
  • 1,656
2 votes
3 answers
559 views

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<=...
ocomfd's user avatar
  • 5,760
2 votes
4 answers
4k views

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 ...
learntogrow-growtolearn's user avatar
2 votes
2 answers
379 views

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: ...
AK_is_curious's user avatar
2 votes
3 answers
3k views

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....
shane's user avatar
  • 137
1 vote
3 answers
546 views

A co-worker submitted code for a PR the other day, which effectively follows this flow: if (a) do something_a else if (b) do something_b else if (c) do something_a where a, b, and c are ...
JRJurman's user avatar
  • 347
3 votes
3 answers
483 views

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 ...
Tez Wingfield's user avatar
2 votes
2 answers
3k views

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 "[...
ldmccartin's user avatar
2 votes
3 answers
427 views

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....
Aluminum's user avatar
  • 201
2 votes
2 answers
1k views

This is a small C++ application (and the table is not huge either) so if possible I would like to avoid include anything other than STL (and boost). So to the problem: I have a table of predefined ...
nass's user avatar
  • 163
2 votes
2 answers
32k views

Just a basic question on IF statements in programming languages, specifically C++. Consider the following basic code example: int i = 2; if(i == 2 || i == 4) { //do something } Because the first ...
Bob's user avatar
  • 21
1 vote
1 answer
144 views

In javascript you can't use statements in expressions. Because of this you are forced to use logic and ternary operators in an expression when you want to use if or switch etc.. Since I didn't want to ...
DifferentPseudonym's user avatar
1 vote
3 answers
8k views

I have a function that is called by myStruct *structName = myFunction(0); The function looks like myStruct *myfunction(int x) { if ( x == NULL) { return NULL; } /*rest of code*...
D.J.'s user avatar
  • 19
7 votes
6 answers
1k views

I came across the following in some sample code: if (urlStr) { NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile:urlStr]; node.nodeIcon = iconImage; } else { // it's a ...
Steve Moser's user avatar
0 votes
3 answers
188 views

I have read this link Should I return from a function early or use an if statement? and it triggered a conflict in my head. I agree that it looks nicer and cleaner and I guess that would be the way I ...
acy's user avatar
  • 3
4 votes
1 answer
2k views

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 ...
leylandski's user avatar
6 votes
2 answers
14k views

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 ...
Aluminum's user avatar
  • 201
15 votes
5 answers
5k views

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 ...
Kevin Cittadini's user avatar
2 votes
4 answers
3k views

A simplified example: function logTheColor (color){ if(color == "red"){ color = "The color is red " } else if (color == "yellow") { color = "The color is yellow " } else { ...
Alejandro Veltri's user avatar
0 votes
4 answers
6k views

I'm rather new to JavaScript and programming in general so I am pretty much only used to seeing if statements that have some kind of comparison operator like, if (x < 10) or if(myBool). I have ...
j flo's user avatar
  • 17
4 votes
2 answers
5k views

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 ...
rick's user avatar
  • 2,005
20 votes
3 answers
44k views

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's user avatar
  • 301