Questions tagged [nested-if]

This tag refers to a code structure in which multiple if-statements are placed in a "nested" form (i.e. one if-statement is contained within another).

Use this tag for questions related to constructing, maintaining, or refactoring nested if-statements.

428 questions
24
votes
5 answers

How to perform nested 'if' statements using Java 8/lambda?

I have the following code and would like to implement it using lambda functions just for fun. Can it be done using the basic aggregate operations? List result = new ArrayList<>(); for (int i = 1; i <= 10; i++) { if (10 % i == 0) { …
moon
  • 1,702
  • 3
  • 19
  • 35
24
votes
5 answers

"if" block without curly braces makes subsequent "else if" nested

AFAIK, if an "if" block is not provided the curly braces then only 1 statement is considered inside it. e.g. if(..) statement_1; statement_2; Irrespective of tabs, only statement_1 is considered inside the if block. Following code doesn't get…
iammilind
  • 68,093
  • 33
  • 169
  • 336
16
votes
3 answers

PHP - Nested IF statements

What control structures can one use instead of multiple nested IF statements. eg: function change_password($email, $password, $new_password, $confirm_new_password) { if($email && $password && $new_password && $confirm_new_password) { …
Kausheel
  • 245
  • 1
  • 2
  • 8
14
votes
3 answers

How to write nested if else if in MYSQL

The folowing sntax seams to be correct. While running on mysql gives error Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 27". …
user3239587
  • 213
  • 1
  • 4
  • 14
11
votes
5 answers

Is There Anything Like a Templatized Case-Statement

So I have this really ugly code: template std::conditional_t
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
6
votes
2 answers

What happens with nested branches and speculative execution?

Alright, so I know that if a particular conditional branch has a condition that takes time to compute (memory access, for instance), the CPU assumes a condition result and speculatively executes along that path. However, what would happen if, along…
6
votes
4 answers

Are there any exceptions in removing curly braces for an "if" statement?

I am a computer science student, and some time ago our professor explained us that in C language we can remove curly braces when there's only one statement like: if (a) do b but we can't do something like this: if(a) do b do c because that…
WhiteNoise
  • 71
  • 3
5
votes
4 answers

Nested-If statements in Shell-scripting

This is my script: echo "Name" read name if [ "$name" == "abcd" ]; then echo "Password" read password if [ "$password == "pwd" ]; then echo "Hello" else echo "Wrong password" fi else echo "wrong username" fi And this is the…
hari
  • 1,297
  • 5
  • 17
  • 36
5
votes
10 answers

What's the right way to handle "One, Both, or None" logic?

I have a logic situation that is best described as two "Teams" trying to win a task. The outcome of this task could be a single winner, a tie (draw), or no winner (stalemate). Currently, I'm using a nested if/else statement like so: // using PHP,…
Stephen
  • 18,827
  • 9
  • 60
  • 98
5
votes
2 answers

Using Nested IF ELSE statements in sql

When any one of the following conditions is met, I want the code to go to the next execution step: First Name, Last Name and DOB : all three are not blank ID and DOB are not blank SSN and DOB are not blank ID and Group Number are not blank Below…
user2913493
5
votes
4 answers

Ruby: nested if statements

I was writing some code and it ended up being way too ugly to my liking. Is there anyway that I can refactor it so that I don't use nested if statements? def hours_occupied(date) #assuming date is a valid date object availability =…
rlhh
  • 893
  • 3
  • 17
  • 32
5
votes
4 answers

Replacing Nested if Statement With AND

I am wondering whether nested if is better than AND statement. I have a loop that goes so many times so I am thinking of faster execution available. Below is the code that has same logic with my code. The nested if statement is inside a loop. for…
blenzcoffee
  • 851
  • 1
  • 11
  • 35
4
votes
1 answer

Using IFS with Arrayformula function in Google Sheets

I was wondering if its possible to use an IFS function inside the arrayfunction? Im looking to have a drop down list populate an array depending on what is selected in the data validation. the formula im using is: =ARRAYFORMULA(IFs(H2="1st…
4
votes
1 answer

Appropriate Use of Nested If Statement

Nested if statements are allowable, obviously. So it seems that there might be an appropriate use for them. Everyone hates on them, and I was wondering if there are times when nested if statements are the best course of action. Here's an example…
RoboticRenaissance
  • 1,130
  • 1
  • 10
  • 17
4
votes
3 answers

MySQL CASE for value range doesn't work but nested IF's do?

I'm probably missing something that is really, really simple, but I can't for the life of me figure out what it is that I'm not doing correctly... I have this query which is used to pull out hours people have completed in volunteering and then…
Othyn
  • 829
  • 1
  • 9
  • 21
1
2 3
28 29