ArticleslgStudy

computer science

Expression (computer science)

Expression (computer science) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Expression (computer science) rather than just read about it. In short: In computer science, an expression is a syntactic entity in a programming language that may be evaluated to determine its value of a specific semantic type. It is a combination of one or more constants, variables, functions, and operators that the programming language interprets (according to its particular rules of precedence and of association) and computes to produce ("to return", in a stateful environment) anoth…

Key takeaways

  • Expression (computer science) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Expression (computer science) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Expression (computer science) from memory before moving on to harder problems.

Reference excerpt

In computer science, an expression is a syntactic entity in a programming language that may be evaluated to determine its value of a specific semantic type. It is a combination of one or more constants, variables, functions, and operators that the programming language interprets (according to its particular rules of precedence and of association) and computes to produce ("to return", in a stateful environment) another value. In simple settings, the resulting value is usually one of various primitive types, such as string, boolean, or numerical (such as integer, floating-point, or complex). Expressions are often contrasted with statements—syntactic entities that have no value (an instruction).

Definition Like in mathematics, an expression is used to denote a value to be evaluated for a specific value type supported by a programming language. In some cases an expression can't be fully evaluated; in this case, the value is undefined, even though the calculation was effected and finished. The process of evaluating an expression to a well-defined value type is called evaluation; it can occur in different contexts, such as definition and initialization .

Examples 2 + 3 is both an arithmetic and programming expression, which evaluates to 5. A variable is an expression because it denotes a value in memory, so y + 6 is also an expression. An example of a relational expression is 4 ≠ 4, which evaluates to false.

C You can see how the parts of the code can be an expression.

In C (and many other programming languages), the "=" is considered an operator like in mathematics; more specifically, a binary operator.

Void as a result type In C and most C-derived languages, a call to a function with a void return type is a valid expression, of type void. Values of type void cannot be used, so the value of such an expression is always thrown away.

Side effects and elimination In many programming languages, a function, and hence an expression containing a function, may have side effects. An expression with side effects does not normally have the property of referential transparency. In many languages (e.g. C++), expressions may be ended with a semicolon (;) to turn the expression into an expression statement. This asks the implementation to evaluate the expression for its side-effects only and to disregard the result of the expression (e.g. x+1;) unless it is a part of an expression statement that induces side-effects (e.g. y=x+1; or func1(func2());).

Caveats The formal notion of a side effect is a change to the abstract state of the running program. Another class of side effects are changes to the concrete state of the computational system, such as loading data into cache memories. Languages that are often described as "side effect–free" will generally still have concrete side effects that can be exploited, for example, in side-channel attacks. Furthermore, the elapsed time evaluating an expression (even one with no other apparent side effects), is sometimes essential to the correct operation of a system, as behaviour in time is easily visible from outside the evaluation environment by other parts of the system with which it interacts, and might even be regarded as the primary effect such as when performing benchmark testing. It depends on the particular programming language specification whether an expression with no abstract side effects can legally be eliminated from the execution path by the processing environment in which the expression is evaluated.

See also Evaluation strategy Expression (mathematics) Boolean expression

References

External links This article is based on material taken from Expression at the Free On-line Dictionary of Computing prior to 1 November 2008 and incorporated under the "relicensing" terms of the GFDL, version 1.3 or later.

Worked examples

Example 1 — a first encounter with Expression (computer science)

Start with the simplest possible case. Write down what Expression (computer science) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Expression (computer science) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Expression (computer science) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Expression (computer science)

In research
Expression (computer science) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Expression (computer science) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Expression (computer science) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Evaluation strategy, Programming language concepts, so understanding it makes those chapters shorter.
In everyday life
Look for Expression (computer science) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Expression (computer science)” →

How to study Expression (computer science) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Expression (computer science) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Expression (computer science) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Expression (computer science) in simple terms?

In computer science, an expression is a syntactic entity in a programming language that may be evaluated to determine its value of a specific semantic type. It is a combination of one or more constants, variables, functions, and operators that the programming language interprets (according to its p…

Why does Expression (computer science) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Expression (computer science)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Expression (computer science).

Tags

  • Evaluation strategy
  • Programming language concepts

Keep exploring