Safekipedia

Conditional (computer programming)

Adapted from Wikipedia · Discoverer experience

In computer programming, a conditional statement helps decide what a program does next by checking if something is true or false. This is like asking a question and then choosing what to do based on the answer. For example, a program might check if a number is bigger than ten and, if it is, do one thing; if it isn’t, do something else.

Conditional statements are important because they let programs make choices and 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, although some languages also let them change things in special ways.

Conditional statement

Conditional statements help computers make decisions. They check if something is true or false and then do different things based on that.

For example, imagine a shop checking if it has stock. If there is no stock, it might say "order new stock". But if there is stock, it might say "there is stock". This is done using an "if-then-else" statement.

Many languages have these statements, but they may look a little different depending on the language used.

Conditional expression

See also: Ternary conditional operator

Many programming languages let you choose a value based 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. However, 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 different way to make choices in code instead of using if–then–else or switch statements. Many languages that work with 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 both 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 small rule and a list, then applies the rule to each item in the list and returns the new list. The two lines show what to do when the list is empty (just return an empty list) and when the list has items (apply the rule to the first item and continue with the rest).

While pattern matching can sometimes just bind names to values without making a choice, it is often used to pick different actions based on what the data looks like.

Map-based conditionals

In some programming languages like Python, Perl, PHP, or Objective-C, you can use special types of data collections to decide what happens in a program. For example, you can create a list of pet names and then pick the right name based on the type of pet someone chooses.

Programmers can also use these collections to organize different 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 that lets instructions run only when certain conditions are met, 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 languageStructured ifswitch–select–caseConditional expressionsArithmetic ifPattern matching[A]
thenelseelse–if
AdaYesYesYesYesYesNoNo
ALGOL-60YesYesUnneeded[C]NoYesNoNo
ALGOL 68YesYesYesYesYesYes - case clauseYes - Conformity clause
APLNoYesYesYesYesNoNo
Bash shellYesYesYesYesYesNoYes
C, C++NoYesUnneeded[B][C]Fall-throughYesNoNo
C#NoYesUnneeded[B][C]YesYesNoNo
COBOLUnneededYesUnneeded[C]YesNoNoNo
EiffelYesYesYesYesYesNoNo
F#YesYesYesYes[D]YesNoYes
FortranYesYesYesYesYes[G]Yes[I]Yes - select type
GoNoYesUnneeded[C]YesNoNoNo
HaskellYesNeededUnneeded[C]Yes[D]YesNoYes
JavaNoYesUnneeded[C]Fall-throughYesNoNo
ECMAScript (JavaScript)NoYesUnneeded[C]Fall-throughYesNoNo
MathematicaNoYesYesYesYesNoYes
OberonYesYesYesYesNoNoNo
PerlNoYesYesYesYesNoNo
PHPNoYesYesFall-throughYesNoYes
Pascal, Object Pascal (Delphi)YesYesUnneededYesNoNoNo
PythonNoYesYesYesYesNoYes
QuickBASICYesYesYesYesNoNoNo
RubyYesYesYesYesYesNoYes[H]
RustNoYesYesYes[D]YesNoYes
ScalaNoYesUnneeded[C]Fall-throughYesNoYes
SQLYes[F]YesYesYes[F]YesNoNo
SwiftNoYesYesYesYesNoYes
TclNoYesYesYesYesNoYes
Visual Basic, classicYesYesYesYesYesNoNo
Visual Basic .NETYesYesYesYesYesNoNo
Windows PowerShellNoYesYesFall-throughYesNoNo

Related articles

This article is a child-friendly adaptation of the Wikipedia article on Conditional (computer programming), available under CC BY-SA 4.0.