Conditional (computer programming)
Adapted from Wikipedia · Adventurer experience
In computer programming, a conditional statement helps a program decide what to do next. It checks if something is true or false, like asking a question and choosing what to do based on the answer.
For example, a program might check if a number is bigger than ten. If it is, the program does one thing. If it isn’t, it does something else.
Conditional statements are important because they let programs make choices. They help solve problems that have different paths. Without them, every program would just do the same steps one after another, which would not be very useful for many tasks.
Many programming languages, such as C, have special ways to write these conditional statements. In some types of programming, called functional programming, conditional expressions can give a result without changing the program’s normal path.
Conditional statement
Conditional statements help computers make choices. They check if something is true or false and then act based on that.
For example, think about a shop checking if it has items in stock. If there is no stock, it might say "order new stock". If there is stock, it might say "there is stock". This is done using an "if-then-else" statement.
Many programming languages use these statements, but they may look different depending on the language.
Conditional expression
See also: Ternary conditional operator
Many programming languages let you pick a value depending on a condition. This is called a conditional expression. It gives you a result without changing how the program runs.
The idea of conditional expressions started with a researcher named John McCarthy in the late 1950s when he worked on a computer language called LISP.
Main article: Haskell (programming language)
In the language Haskell, you can only use if as an expression, not a statement. Every expression must give a value, so the else part is required.
Main article: Visual Basic
In Visual Basic, there is a function called IIf that acts like a conditional expression. It works differently because it checks both possible results before choosing one.
Main article: Tcl (programming language)
In Tcl, the if command is actually a function. It takes the condition and what to do if it's true as inputs.
Main article: Rust (programming language)
In Rust, if is always an expression. It gives a value based on which part runs. If a part doesn’t give a value, it uses a special empty value. Each part must give the same type of value, so an else part is usually needed.
Pattern matching
Pattern matching is a way to make choices in code, instead of using if–then–else or switch statements. Many languages that use functions, like the Wolfram Language and ML, have pattern matching.
For example, in the OCaml language, you can write:
match fruit with
Pattern matching lets you match actions and values to different shapes of data. In Haskell, here’s an example:
map _ [] = [] map f (h : t) = f h : map f t
This code creates a function called map. It takes a rule and a list, and then uses the rule on each item in the list. The two lines show what to do when the list is empty, and what to do when the list has items.
Map-based conditionals
In some programming languages like Python, Perl, PHP, or Objective-C, you can use special types of data to decide what happens in a program. For example, you can make a list of pet names and then choose the right name based on the type of pet someone picks.
Programmers can use these data collections to organize actions. By storing small pieces of code in a list, they can quickly choose which action to perform when needed.
Branch predication
An alternative to conditional branch instructions is branch predication. Predication is an architectural feature. It lets instructions run only when certain conditions are met. This happens instead of changing how the program moves forward.
Choice system cross reference
This table shows information about the latest official version of each programming language. For languages without an official specification, it uses the most recent version that was released.
| Programming language | Structured if | switch–select–case | Conditional expressions | Arithmetic if | Pattern matching[A] | ||
|---|---|---|---|---|---|---|---|
| then | else | else–if | |||||
| Ada | Yes | Yes | Yes | Yes | Yes | No | No |
| ALGOL-60 | Yes | Yes | Unneeded[C] | No | Yes | No | No |
| ALGOL 68 | Yes | Yes | Yes | Yes | Yes | Yes - case clause | Yes - Conformity clause |
| APL | No | Yes | Yes | Yes | Yes | No | No |
| Bash shell | Yes | Yes | Yes | Yes | Yes | No | Yes |
| C, C++ | No | Yes | Unneeded[B][C] | Fall-through | Yes | No | No |
| C# | No | Yes | Unneeded[B][C] | Yes | Yes | No | No |
| COBOL | Unneeded | Yes | Unneeded[C] | Yes | No | No | No |
| Eiffel | Yes | Yes | Yes | Yes | Yes | No | No |
| F# | Yes | Yes | Yes | Yes[D] | Yes | No | Yes |
| Fortran | Yes | Yes | Yes | Yes | Yes[G] | Yes[I] | Yes - select type |
| Go | No | Yes | Unneeded[C] | Yes | No | No | No |
| Haskell | Yes | Needed | Unneeded[C] | Yes[D] | Yes | No | Yes |
| Java | No | Yes | Unneeded[C] | Fall-through | Yes | No | No |
| ECMAScript (JavaScript) | No | Yes | Unneeded[C] | Fall-through | Yes | No | No |
| Mathematica | No | Yes | Yes | Yes | Yes | No | Yes |
| Oberon | Yes | Yes | Yes | Yes | No | No | No |
| Perl | No | Yes | Yes | Yes | Yes | No | No |
| PHP | No | Yes | Yes | Fall-through | Yes | No | Yes |
| Pascal, Object Pascal (Delphi) | Yes | Yes | Unneeded | Yes | No | No | No |
| Python | No | Yes | Yes | Yes | Yes | No | Yes |
| QuickBASIC | Yes | Yes | Yes | Yes | No | No | No |
| Ruby | Yes | Yes | Yes | Yes | Yes | No | Yes[H] |
| Rust | No | Yes | Yes | Yes[D] | Yes | No | Yes |
| Scala | No | Yes | Unneeded[C] | Fall-through | Yes | No | Yes |
| SQL | Yes[F] | Yes | Yes | Yes[F] | Yes | No | No |
| Swift | No | Yes | Yes | Yes | Yes | No | Yes |
| Tcl | No | Yes | Yes | Yes | Yes | No | Yes |
| Visual Basic, classic | Yes | Yes | Yes | Yes | Yes | No | No |
| Visual Basic .NET | Yes | Yes | Yes | Yes | Yes | No | No |
| Windows PowerShell | No | Yes | Yes | Fall-through | Yes | No | No |
Related articles
This article is a child-friendly adaptation of the Wikipedia article on Conditional (computer programming), available under CC BY-SA 4.0.
Safekipedia