0% found this document useful (0 votes)
6 views17 pages

Expression and Statements

The document discusses expressions and statements in programming languages. It defines expressions and statements, describes the main types of expressions like boolean and mixed-mode expressions, and covers statement types including assignment, compound, and other statements like if/else and loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views17 pages

Expression and Statements

The document discusses expressions and statements in programming languages. It defines expressions and statements, describes the main types of expressions like boolean and mixed-mode expressions, and covers statement types including assignment, compound, and other statements like if/else and loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter Goals

Expressions
Statements
Exercises

Programming Language

Expressions and Statements

MESSI NGUELE Thomas (PhD)

Yaoundé, 02nd 2024

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 1 / 17
Chapter Goals
Expressions
Statements
Exercises

Chapter Goals

1 Define an expression, a statement.


2 Give a difference between an expression and a statement.
3 Give the main types of expressions encountered in programming languages.
4 Give the main types of statements encountered in programming languages

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 2 / 17
Chapter Goals
Expressions
Statements
Exercises

Agenda

1 Chapter Goals

2 Expressions

3 Statements

4 Exercises

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 3 / 17
Chapter Goals Generalities on Expressions
Expressions Tree structure
Statements Boolean expressions
Exercises Mixed - Mode expressions

Agenda

1 Chapter Goals

2 Expressions
Generalities on Expressions
Tree structure
Boolean expressions
Mixed - Mode expressions

3 Statements

4 Exercises

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 4 / 17
Chapter Goals Generalities on Expressions
Expressions Tree structure
Statements Boolean expressions
Exercises Mixed - Mode expressions

Generalities on Expressions
1 An expression is a combinaison of one or more constants, variables,
operators and functions intended to be computed in order to produce a value.
2 In others words, an expression is composed of one or more operands whose
values may be combined by operators.
3 Example is "4.0 + a ∗ b" which evaluation is as follow :
First substitute values for variables a and b.
Multiply these values.
Add 4.0 to the previous result.
4 There are precedence rules in order to avoid non determinism. For example,
in java, the evaluation is as follow :
a - Evaluate any expression enclosed within brackets first, starting with the
innermost brackets.
b - Apply the following operator precedence :
! * / % + - > < >= <= == != && || =
c - If the opperators in (b-) have the same precedence, apply them in order from
left to right.

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 5 / 17
Chapter Goals Generalities on Expressions
Expressions Tree structure
Statements Boolean expressions
Exercises Mixed - Mode expressions

Evaluation of "a + b ∗ c/d"

1 The values of b and c are first multiplied together, the result is then divided
by d and finaly added to a value.
2 The evaluation of an expression should produce a value and nothing more ; it
should not change the state of a program.
3 Operators in expressions can be binary (dyadic) or unary (monadic).

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 6 / 17
Chapter Goals Generalities on Expressions
Expressions Tree structure
Statements Boolean expressions
Exercises Mixed - Mode expressions

Boolean expressions

1 The value returned must be either true or false


2 Often involve relational operators (>,<=,==)
3 Can also contain variables of types boolean and boolean operators (not, and,
or in Pascal ; !, && and || in Java, C C++)
4 In Pascal * (respec. +) has the same precedence as and (respec. or)
5 Are often used in if and while statements.
Example : if(i<0) or (a[i]>a[i+1]) then ...
This lead to what called short-circuit evaluation.
Given (a and b) where a is false, b is not evaluated.
Given (a or b) where a is true, b is not evaluated.

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 7 / 17
Chapter Goals Generalities on Expressions
Expressions Tree structure
Statements Boolean expressions
Exercises Mixed - Mode expressions

Mixed - Mode expressions

1 An expression in which the two operands do not have the same type is called
a mixed-mode expression.
2 Three categories of languages can be identified :
Languages that forbit mixed-mode expressions (Ada).
Languages like Pascal and Java that allow ’sensible’ combinations such as
adding an intenger to floating-point value. The result of such an expression is,
as would be expected, a floating-point value.
Languages like C and C++ that allow ’unusual’ combinations such as the
ability to add an integer to a character. This can lead to unexpected results.

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 8 / 17
Chapter Goals Generalities
Expressions Assignment Statements
Statements Compound Statements
Exercises Other statements

Agenda

1 Chapter Goals

2 Expressions

3 Statements
Generalities
Assignment Statements
Compound Statements
Other statements

4 Exercises

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 9 / 17
Chapter Goals Generalities
Expressions Assignment Statements
Statements Compound Statements
Exercises Other statements

Generalities

1 A statement is a syntactic unit (of an imperative programming language)


that expresses some action to be carried out.
2 Statements are the commands in a language which perform actions and
change the state.
3 Typical statements are :
Assignment variables which change the values of variables.
Conditional statements which have alternative courses of actions.
Iterative statements which loop through a series of statements until some
condition is satisfied.
Procedure and method calls.

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 10 / 17
Chapter Goals Generalities
Expressions Assignment Statements
Statements Compound Statements
Exercises Other statements

Assignment Statements
1 The general form of the assignment is "el :=er".
el is the expression on the left-hand side of an assignement that gives a
reference as its results.
er is the expression on the right-hand side of an assignment that gives a value
as its result.
2 The value of the right-hand expression is assigned to the reference given by
the left-hand expression.
3 Assignment is an operation on reference-value pairs.
4 There are restriction on el :
It should be variable name, field of a record.
Compatibility between the type of el and the type of value given by er.

Assignment Operators
=, +=, -=, *=, /=, ++, –
*b++ ?, a = 1, Is there any difference between b=a++ and b=++a ?
Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 11 / 17
Chapter Goals Generalities
Expressions Assignment Statements
Statements Compound Statements
Exercises Other statements

Compound Statements

1 Many statement in one (or block statement).


2 Example in Pascal
begin
S1 ;
S2 ;
S3 ;
end
3 In C, C++, Java {S1 ; S2 ; S3}
4 There are differences between :
if (a<b) c=d ;
if(a<b) c=d ; e=f ;
if(a<b){c=d ; e=f}

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 12 / 17
Chapter Goals Generalities
Expressions Assignment Statements
Statements Compound Statements
Exercises Other statements

Other statements
1 Selection with if and switch case or case of "selector".
2 Iterative statements. Loops are terminated in one of two ways :
either by a condition being fulfilled,
or by completing a fixe number of iterations.
Example : for loop, while loop, repeat loop.
A mechanism for iterating over the elements of a data structure is known as
an iterator.
3 goto statement. Jumps
4 Exception handling.
During programming excution, "exceptional" evens may occur.
Such exceptional occurrences can arise as a result of errors,
such as attempted division by zero, erroneous user input,
an array subscript going out of bounds,
unexpectedly encoutering the end of the file.
Java : try { }catch(){ ... }

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 13 / 17
Chapter Goals
Expressions
Statements
Exercises

Agenda

1 Chapter Goals

2 Expressions

3 Statements

4 Exercises

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 14 / 17
Chapter Goals
Expressions
Statements
Exercises

Exercises

13 - Compare Pascal precedence rules with C precedence rules.


14 - Compare Pascal case of with C switch case.
15 - According to you, how does C compiler handle the ternary operator ?
16 - Explain why exception handling is important in embedded systems.

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 15 / 17
Chapter Goals
Expressions
Statements
Exercises

Bibliography

WILSON, Leslie B. et CLARK, Robert George. Comparative programming


languages. Pearson Education, 2001.
MACLENNAN, Bruce J. Principles of programming languages : design,
evaluation, and implementation. Holt, Rinehart & Winston, 1986.

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 16 / 17
Chapter Goals
Expressions
Statements
Exercises

Mai 02nd 2024, Dr.Thomas MESSI NGUELE Programming Languages – Expressions and Statem. 17 / 17

You might also like