Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF Statement multiple conditions, same statement

Tags:

Hey all, looking to reduce the code on my c# if statements as there are several repeating factors and was wondering if a trimmer solution is possible.

I currently have 2 if statements that need to carry out an identical statement, however the only variable is an extra condition on an if statement when a checkbox is not checked. Im just wondering if there is a way to make it one statement or make the condition string variable, heres the compressed version of the code:

if (checkbox.checked)   {     if (columnname != a && columnname != b && columnname != c)     {       "statement 1"     }   } else   {     if (columnname != a && columnname != b && columnname != c          && columnname != A2)     {       "statement 1"     }   } 

Its like I need to run an if statement within the conditions of an if statement if that makes sense, like this psuedo form:

if (columnname != a      && columnname != b      && columnname != c      && if(checkbox.checked{columnname != A2}) 
like image 314
markdigi Avatar asked Aug 27 '09 14:08

markdigi


People also ask

Can an if statement have multiple conditions?

The multiple IF conditions in Excel are IF statements contained within another IF statement. They are used to test multiple conditions simultaneously and return distinct values. The additional IF statements can be included in the “value if true” and “value if false” arguments of a standard IF formula.

Can you have 3 conditions in an if statement?

If you have to write an IF statement with 3 outcomes, then you only need to use one nested IF function. The first IF statement will handle the first outcome, while the second one will return the second and the third possible outcomes. Note: If you have Office 365 installed, then you can also use the new IFS function.

Can we enter multiple if conditions in an IF formula?

It is possible to nest multiple IF functions within one Excel formula. You can nest up to 7 IF functions to create a complex IF THEN ELSE statement. TIP: If you have Excel 2016, try the new IFS function instead of nesting multiple IF functions.


2 Answers

if (columnname != a    && columnname != b    && columnname != c   && (checkbox.checked || columnname != A2)) {    "statement 1" } 

Should do the trick.

like image 106
Daniel Elliott Avatar answered Sep 27 '22 21:09

Daniel Elliott


if (columnname != a && columnname != b && columnname != c          && (columnname != A2 || checkbox.checked))     {       "statement 1"     } 
like image 38
mikefrey Avatar answered Sep 27 '22 20:09

mikefrey