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
1
vote
2answers
204 views

How to effectively handle 404/500 http errors in server-side rendering web application which uses store for state?

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 ...
0
votes
0answers
58 views

Parsing custom if statement input in PHP

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)...
0
votes
1answer
272 views

Refactor multiple “if” in C#

for (int i = lstReportCount - 1; i >= 0; i--){ if ( (input.ServiceTypes == "1" && lstReport[i].A == 0) || (input.ServiceTypes == "2" && lstReport[i].B == 0) || (input....
0
votes
1answer
108 views

What are the pro or cons between a if statement and two different functions [duplicate]

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 ...
0
votes
1answer
188 views

If statement best practice [duplicate]

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 ...
3
votes
3answers
3k views

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
votes
3answers
487 views

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&...
2
votes
4answers
589 views

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 ...
2
votes
2answers
273 views

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: ...
-1
votes
1answer
684 views

coding practices: return vs else [closed]

Which coding style is considered better practice? if-return: function fn { if (condition) { /* if true: insert code here */ return; } /* ...
1
vote
3answers
1k views

Avoiding two if statements for same condition with common code in between [closed]

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....
1
vote
3answers
189 views

If-statement, organize by conditional or effect

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 ...
3
votes
3answers
378 views

Representing a status as single letter strings [duplicate]

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 ...
2
votes
2answers
1k views

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 "[...
2
votes
3answers
344 views

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....
2
votes
2answers
183 views

how to program a lookup table of input and output variables

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 ...
1
vote
2answers
8k views

IF statement with OR logical operator

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 ...
1
vote
1answer
85 views

Using expressions instead of statements

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 ...
0
votes
3answers
2k views

In C, why is NULL and 0 triggering an if statement

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 ...
4
votes
6answers
391 views

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 ...
0
votes
3answers
166 views

Eliminating the bad cases in if to get a nicer code

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 ...
3
votes
1answer
516 views

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 ...
3
votes
2answers
4k views

Do nested conditionals have a significant performance impact? [duplicate]

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 ...
15
votes
5answers
4k views

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 ...
2
votes
4answers
2k views

Overwriting and returning the value of the argument used as conditional of an if statement, inside the same if statement

A simplified example: function logTheColor (color){ if(color == "red"){ color = "The color is red " } else if (color == "yellow") { color = "The color is yellow " } else { ...
0
votes
4answers
3k views

What if(event) statement means in JavaScript? [closed]

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 ...
3
votes
2answers
1k views

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 ...
13
votes
3answers
22k views

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?