Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
1answer
104 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
179 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
1k 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 ...
3
votes
3answers
482 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
2answers
424 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
272 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
487 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
183 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
375 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 ...
3
votes
2answers
979 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
338 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
140 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
7k 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
80 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
1k 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
383 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
165 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
498 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
3k 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
2k 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
20k 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?