ArticleslgStudy

computer science

Statement (computer science)

Statement (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 Statement (computer science) rather than just read about it. In short: In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out. A program written in such a language is formed by a sequence of one or more statements.

Key takeaways

  • Statement (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 Statement (computer science) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Statement (computer science) from memory before moving on to harder problems.

Reference excerpt

In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out. A program written in such a language is formed by a sequence of one or more statements. A statement may have internal components (e.g. expressions). Many programming languages (e.g. Ada, Algol 60, C, Java, Pascal) make a distinction between statements and definitions/declarations. A definition or declaration specifies the data on which a program is to operate, while a statement specifies the actions to be taken with that data. Statements which cannot contain other statements are simple; those which can contain other statements are compound. The appearance of a statement (and indeed a program) is determined by its syntax or grammar. The meaning of a statement is determined by its semantics.

Simple statements Simple statements are complete in themselves; these include assignments, subroutine calls, and a few statements which may significantly affect the program flow of control (e.g. goto, return, stop/halt). In some languages, input and output, assertions, and exits are handled by special statements, while other languages use calls to predefined subroutines.

assignment Fortran: variable = expression Pascal, Algol 60, Ada: variable := expression; C, C#, C++, PHP, Java: variable = expression; call Fortran: CALL subroutine name(parameters) C, C++, Java, PHP, Pascal, Ada: subroutine name(parameters); assertion C, C++, PHP: assert(relational expression); Java: assert relational expression; goto Fortran: GOTO numbered-label Algol 60: goto label; C, C++, PHP, Pascal: goto label; return Fortran: RETURN value C, C++, Java, PHP: return value; stop/halt/exit Fortran: STOP number C, C++: exit(expression) PHP: exit number;

Compound statements

Compound statements may contain (sequences of) statements, nestable to any reasonable depth, and generally involve tests to decide whether or not to obey or repeat these contained statements.

Notation for the following examples: <statement> is any single statement (could be simple or compound). <sequence> is any sequence of zero or more <statements> Some programming languages provide a general way of grouping statements together, so that any single <statement> can be replaced by a group: Algol 60: begin <sequence> end Pascal: begin <sequence> end C, PHP, Java: { <sequence> } Other programming languages have a different special terminator on each kind of compound statement, so that one or more statements are automatically treated as a group: Ada: if test then <sequence> end if; Many compound statements are loop commands or choice commands. In theory only one of each of these types of commands is required. In practice there are various special cases which occur quite often; these may make a program easier to understand, may make programming easier, and can often be implemented much more efficiently. There are many subtleties not mentioned here; see the linked articles for details.

count-controlled loop: Algol 60: for index := 1 step 1 until limit do <statement> ; Pascal: for index := 1 to limit do <statement> ; C, Java: for ( index = 1; index <= limit; index += 1) <statement> ; Ada: for index in 1..limit loop <sequence> end loop Fortran 90: condition-controlled loop with test at start of loop: Algol 60: for index := expression while test do <statement> ; Pascal: while test do <statement> ; C, Java: while (test) <statement> ; Ada: while test loop <sequence> end loop Fortran 90: condition-controlled loop with test at end of loop: Pascal: repeat <sequence> until test; { note reversed test } C, Java: do { <sequence> } while (test) ; Ada: loop <sequence> exit when test; end loop; condition-controlled loop with test in the middle of the loop: C: do { <sequence> if (test) break; <sequence> } while (true) ; Ada: loop <sequence> exit when test; <sequence> end loop; if-statement simple situation: Algol 60:if test then <unconditional statement> ; Pascal: if test then <statement> ; C, Java: if (test) <statement> ; Ada: if test then <sequence> end if; Fortran 77+: if-statement two-way choice: Algol 60: if test then <unconditional statement> else <statement> ; Pascal: if test then <statement> else <statement> ; C, Java: if (test) <statement> else <statement> ; Ada: if test then <sequence> else <sequence> end if; Fortran 77+: case/switch statement multi-way choice: Pascal: case c of 'a': alert(); 'q': quit(); end; Ada: case c is when 'a' => alert(); when 'q' => quit(); end case; C, Java: switch (c) { case 'a': alert(); break; case 'q': quit(); break; } Exception handling: Ada: begin protected code except when exception specification => exception handler Java: try { protected code } catch (exception specification) { exception handler } finally { cleanup } Python: try: protected code except exception specification: exception handler else: no exceptions finally: cleanup

Syntax

Apart from assignments and subroutine calls, most languages start each statement with a special word (e.g. goto, if, while, etc.) as shown in the above examples. Various methods have been used to describe the form of statements in different languages; the more formal methods tend to be more precise:

Algol 60 used Backus–Naur form (BNF) which set a new level for language grammar specification. Up until Fortran 77, the language was described in English prose with examples, From Fortran 90 onwards, the language was described using a variant of BNF. Cobol used a two-dimensional metalanguage. Pascal used both syntax diagrams and equivalent BNF. BNF uses recursion to express repetition, so various extensions have been proposed to allow direct indication of repetition.

Statements and keywords Some programming language grammars reserve keywords or mark them specially, and do not allow them to be used as identifiers. This often leads to grammars which are easier to parse, requiring less lookahead.

No distinguished keywords Fortran and PL/1 do not have reserved keywords, allowing statements like:

in PL/1: IF IF = THEN THEN ... (the second IF and the first THEN are variables). in Fortran: IF (A) X = 10... conditional statement (with other variants) IF (A) = 2 assignment to a subscripted variable named IF As spaces were optional up to Fortran 95, a typo could completely change the meaning of a statement: DO 10 I = 1,5 start of a loop with I running from 1 to 5 DO 10 I = 1.5 assignment of the value 1.5 to the variable DO10I

… excerpt ends here. Continue reading the full article.

Worked examples

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

Start with the simplest possible case. Write down what Statement (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 Statement (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 Statement (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 Statement (computer science)

In research
Statement (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 Statement (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
Statement (computer science) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming language concepts, Statements, so understanding it makes those chapters shorter.
In everyday life
Look for Statement (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 “Statement (computer science)” →

How to study Statement (computer science) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Statement (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 Statement (computer science) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Statement (computer science) in simple terms?

In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out. A program written in such a language is formed by a sequence of one or more statements.

Why does Statement (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 Statement (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 Statement (computer science).

Tags

  • Programming language concepts
  • Statements

Keep exploring