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.

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-else

The 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-if

By adding the else if statement, it is possible to check several conditions in a row, and do actions when the condition it checks for is true.

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.

? equals if and : equals else. Instead of if A then B else C, it would be A ? B : C.

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 statements

Switch 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.

Pascal: C: V (Vlang):
case someChar of
  'a': actionA;
  'x': actionX;
  'y','z':actionYandZ;
  else actionNoMatch;
end;
switch (someChar) {
  case 'a': actionA; break;
  case 'x': actionX; break;
  case 'y':
  case 'z': actionYandZ; break;
  default: actionNoMatch;
}
match some_char {
  'a' {action_a}
  'x' {action_x}
  'y','z' {action_y_z}
  else {action_no_match}
}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.