Most voted "if" questions
The "if" statement is a control structure in many programming languages that alters the execution flow depending on a binary condition - also known as Boolean logic. This tag should be used when the question is related to the use and/or performance of the "if".
Learn more…467 questions
Sort by count of
-
117
votes7
answers78990
viewsHow does this if/Else work with "?" and ":"?
I am studying Javascript and I have doubts about the different ways to make a if/ else. For example, this: foo ? foo : foo How exactly does it work?…
-
74
votes8
answers5453
viewsWhy in some if’s situations are considered bad?
I read in some places that it is not recommended the exaggerated use of if, because it makes your code difficult to read and maintain, so it’s not a good practice. What would be the overuse of if in…
-
46
votes2
answers13820
viewsDifferences between If and ternary operator ?:
There is a difference in performance between using an if and a ternary operator in C#? Utilise Resharper (Productivity tool for VS), he insists that the ternary operator is better but doesn’t…
-
36
votes4
answers58476
viewsIF, ELSE IF, ELSE or IF IF IF. When to use, what’s the difference?
It’s a very basic question, but it intrigues me a lot. See the code below: var bola = "redonda"; if(bola == "comestivel") return comer(); if(bola == "liquida") return beber(); if(bola == "redonda")…
-
27
votes4
answers3196
viewsWhat is good practice when casting an exception within if?
What is the best practice in this case, use the Else even knowing that the if will or will not make an exception? option 1 (with Else): if (condicao){ throw new RuntimeException("Mensagem"); }else{…
-
24
votes2
answers24948
viewsWhat is the difference between Switch, Case and If, Else?
I’d like to know the difference between switch .. case and if .. else. Which offers a better "performance"?
-
16
votes2
answers713
viewsHow to simplify the following IF in PHP?
I had to do a check to concatenate content to a variable, but I think the service got a bit "pig" and wanted to see if someone could help me simplify this check if($IdUserOnline2){ $IdsInteracoes .=…
-
15
votes3
answers2003
viewsIn PHP the correct is Else if or elseif?
In PHP, the correct is else if or elseif? What’s the difference between them? The language allows writing everything together and separately, and apparently the results are identical…
-
14
votes1
answer276
viewsWhy is using String in a switch block more efficient than in an if-Else block?
According to the java documentation: The Java Compiler generates generally more Efficient bytecode from switch statements that use String Objects than from chained if-then-Else statements. Not to…
-
12
votes1
answer162
viewsIs "Then" really necessary at the end of the If block?
The Visual Basic . NET compiler seems to ignore the reserved word Then, the one at the end of the block If. If (1 + 1 = 2) Then Console.WriteLine("Passou no teste.") End If And now, without the…
-
11
votes1
answer1494
viewsJavascript ternary condition with only one value
A very basic Javascript question, in a ternary condition how can I have action only in an if without the need of Else? Example (test) ? test1() : test2(); If you don’t want anything to happen to…
-
11
votes1
answer284
viewsDoubt about the IF
Does the IF function only work with Integers or does it also work with String? I was trying to create a program that would ask if you are sure you want to create the password. You can check the code…
-
11
votes4
answers870
viewsWhy can an if be redundant?
Using a method that returns a boolean the system would determine whether a number is positive or negative. So I did it this way: public boolean isPositive(float num) { boolean positive; if (num…
-
10
votes2
answers210
viewsHow does an "if" work internally?
The if is widely used in programming, and he plays several important roles in a programmer’s everyday life. The code seems to magically run if an expression is passed in the if is true, otherwise…
-
10
votes2
answers1147
viewsShould I declare a variable within the "if" or separate condition?
I need to call a function that can return false or a value, and in case it returns the precise value manipulate it. So I made a code similar to the one below: //A função que é chamada é muito mais…
-
10
votes2
answers217
viewsWhy does this if check "if it’s ! false" instead of "if it’s true"?
I came across this piece of code: function showPrimes(n) { for (let i = 2; i < n; i++) { if (!isPrime(i)) continue; alert(i); // a prime } } function isPrime(n) { for (let i = 2; i < n; i++) {…
-
9
votes1
answer2214
viewsJavascript performance: switch or if nested?
Which of the two alternatives provides the best performance in Javascript: nested switch or if? if { ... } else { if { ... } else { if { ... } else { ... } } } or switch(expression) { case n: code…
-
9
votes1
answer197
viewsif with a Matches or multiple comparisons?
Whereas I have a variable of the type String tipoResidencia that will never be null, and that I need to run a test, the default solution is this: if (tipoResidencia.equals("CASA") ||…
-
9
votes3
answers2573
viewsIs it possible to avoid using ifs in these cases?
I have read many materials on the internet about OOP, the overwhelming majority says to avoid if/else at all costs. For an OOP beginner like myself, many doubts arise. For example: if…
-
9
votes2
answers521
viewsPerformance difference between multiple conditions in one IF or multiple IF’s separately?
During a change in a source code, I came across the following situation, developed by another developer: if ( booleano1 || booleano2 || booleano3 ) { if( booleano1 ) { //faz algo } if( booleano2 ) {…
-
9
votes2
answers426
viewsIs there a way to break in If?
I was wondering if there’s some kind of break on IF. My doubt is based on the example below. When the function b() returns false all following comparisons are not performed. I thought the condition…
-
8
votes1
answer1772
views -
8
votes3
answers374
views"== true" is useful for something? Is it better "!" or "== false"?
Place == true is useful in checking, or is completely useless? You better use ! or == false. Example: if (pessoa.estaMorta() == false) .... if (!pessoa.estaMorta()) .... if (pessoa.estaViva() ==…
-
8
votes4
answers121
viewsShould I use two IF’s or an operator?
Best practice for checking two conditions? In the example I’m going to give, are not very long checks but what is the best practice and what is the difference? (It happened to me that I found myself…
-
8
votes2
answers178
viewsWhat is this if trying to say?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta…
-
7
votes3
answers6440
viewsHow to check if an item is contained in an array?
What am I doing wrong in this if? if (angular.uppercase(nome[id]) in ['A', 'E','I', 'O', 'U']) { .... } If I only do so it works. But I will have to repeat to others. if (angular.uppercase(nome[id])…
-
7
votes2
answers586
views -
7
votes1
answer275
viewsWhat is the purpose of while(*variable) and if(!*variable) in the statements "while" and "if"?
In Code Review there is a implementation of a simply chained list that I am using. However, in the reply of one of the users, he made some modifications to the list, and had a particular…
-
6
votes2
answers982
viewsIs it possible to perform an assignment and comparison in if clauses in Java?
It is possible to assign and at the same time perform a comparison in an if clause in Java? For example: String linha = leitor.readLine(); String saida = ""; if (linha = leitor.readLine()) { saida…
-
6
votes2
answers256
viewsOptimization of custom search tool
Guys, I’m creating a search page that gets, via POST, the information on a form containing several checkbox. See below that there are several sequences of results and is marked in orange one of them…
-
6
votes2
answers963
viewsThe if command in java is not working within a method
I’m a beginner and I’m not getting my code to work as expected with regard to "if/Else". I am using this command within a method and doing as follows: private boolean AutentificaSenha(String s) {…
-
6
votes2
answers1839
viewsSwitch Alternative Inside Switch
I’m making a decision structure that depends on more than one variable, but readability and maintenance using if or switch are terrible. What alternatives do I have for this case? Example of switch…
-
6
votes1
answer7278
viewsHow to use the function toupper() in char in C?
I’m creating a variable: char nome[20]; After this, I am asking the user to enter a name: printf("Digite um nome : "); scanf("%20s",&nome); And I’m checking to see if the name is correct:…
-
5
votes9
answers2644
viewsBest practice for creating if
Which of the two code options has better performance? I’m going to show you a very simple example, usually there’s more code inside the if. To) string mensagem = "OI"; if(exibirAdeus) mensagem =…
-
5
votes5
answers7269
viewsUse if inside foreach
I’m trying to change the order in which the results appear within a loop using foreach. What I need is for the numbers larger than five to appear first, followed by the rest of the numbers. So I…
-
5
votes2
answers3228
viewsQuestion and answer with if and Else in Java
No matter what I say, both "yes" and "yes" just show the ELSE. Where am I going wrong? package saudacao; import java.util.*; public class Saudacao { public static void main(String[] args) {…
-
5
votes1
answer175
viewsHow to run only one if without entering the other in PHP
I have two if parallel and I need to enter one OR the other, not both. But because I am inside one while with the first if, I couldn’t put just one else, having to do another if outside the while.…
-
5
votes2
answers301
views -
5
votes1
answer3045
viewsDifference between If and Iif
In VB.NET, what is the difference of the use of If and of IIf under these conditions? If Condicao Then x() Else y() End If IIf(Condicao, x(), y())…
-
5
votes1
answer302
viewshow to increase the limit of ifelse in R?
I need to calculate the years of study from the Pnad questionnaire. I did a programming similar to this one below, but I have many ifelse within the other (121 to be exact), because I have a larger…
-
5
votes1
answer582
viewsHow to combine two vectors and get all the results of a given account with them
I’m trying to make a calculation, and I need your help. First I make a sequence with normal distribution with 420 numbers, then I make a sequence of numbers I call mci with 42 numbers, then the next…
-
5
votes3
answers24766
viewsIs it possible to use if Else in Mysql queries?
I’ve been searching the internet and hearing rumors that it is possible to use if else in Mysql queries but saw nothing concrete about it. Is it even possible? If so, how? I want to make a query in…
-
5
votes1
answer110
viewsAllow new conditions to be added without modifying code
I am manipulating a .txt and I have to exchange values like M31 for T90 for example. My code is like this at the moment: //Change machine tools for Sodick if (_strLinesFinal.Contains("M50")) { _OS =…
-
5
votes1
answer137
viewsDoubt about Neural Networks
I had finished the course of Neural Networks with python, and simply after having trained my entire neural network, I had a question about the use of it. For example, after my network goes through…
-
5
votes3
answers387
viewsIs there more than one way to use "if"?
const sequence = { _id: 1, get id() { return this._id++ } } const produtos = {} function salvarProduto(produto) { if (!produto.id) produto.id = sequence.id produtos[produto.id] = produto return…
-
5
votes0
answers48
viewsUse of Else outside an if
I was writing a program that would give me all prime numbers between 1 and a certain number that should be informed by the user. After several adjustments to the code, I came to the following final…
-
5
votes2
answers1055
viewsIf, Elseif and Else with Java 8
I would like to build a method using Java 8, replacing the conditional IF, ELSE-IF and ELSE. For this, I built a sequence of code, I do not know if it is ideal and would like to hear better opinions…
-
5
votes2
answers1797
viewsIs there any way to decrease the amount of Else and if?
I should create the conditions for enrolling students based on their age, I was wondering if there is any way to decrease the amount of if in that part of the code: int idade =…
-
5
votes3
answers203
viewsShould I always use Try catch to fix mistakes?
Sometimes when I know something may fail in my system I do a validation using if, equal is below: lista = [1,2,3] #um número que esteja entre os indices da lista numero = int(input(f'Digite um…
-
5
votes1
answer35
viewsComparing captured keyboard strings to Rust
I’m trying to learn Rust and naturally I’m starting with "The Book". At the end of chapter 3 has 3 simple logic exercises, and one of them is the classic converter from Fahrenheit to Celcius. I…