Conditional (computer programming)Conditionals allow blocks of code to give different results based on true or false. The data type that allows for true or false is called a boolean. If-else or If-then-elseThe if statement is the most basic kind of conditional. It checks that a condition is true, then does an action based on a block of code. if (condition) { } // code for if the condition is true
By adding an else statement, it will be done, if the condition is false. if (condition) { } // code for if the condition is true
else { } // code if the condition is false
Else-ifBy adding the if (condition) { } // code for if this condition is true
else if (condition) { } // code for if this condition is true
else if (condition) { } // code for if this condition is true
else { } // code for if none of the above conditions happen (false)
Ternary operator (?:)A ternary operator is a special operator that is easier to type out and represents conditional statements in several programming languages.
int var = (condition) ? X : Y;
Some languages do not use the ternary operator, but do similar, by typing out the if and else statement on the same line. int var = if (condition) X else Y
Case, switch, and match statementsSwitch statement is the most common term, but it might be called case or match, depending on the language. The switch statement can be used to replace if-else if-else statements.
|
Portal di Ensiklopedia Dunia