Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
Assignment Statements
Part 1
Expressions
• Expressions are the fundamental means of specifying computations in a
programming language.
• A formal mechanism (BNF) for describing the syntax of expressions was
introduced in lecture 3.
• In this lecture, the semantics of expressions are discussed.
Expressions
• The essence of the imperative programming languages is the dominant role of
assignment statements. The purpose of these statements is to cause the side
effect of changing the values of variables, or the state, of the program. So an
integral part of all imperative languages is the concept of variables whose
values change during program execution.
• Functional languages use variables of a different sort, such as the parameters of
functions. These languages also have declaration statements that bind values to
names. These declarations are similar to assignment statements, but do not have
side effects.
Arithmetic Expressions
• evaluation of arithmetic expressions similar to those found in mathematics, science, and
engineering was one of the primary goals of the first high-level programming languages.
• In programming languages, arithmetic expressions consist of operators, operands,
parentheses, and function calls.
• An operator can be unary, meaning it has a single operand, binary, meaning it has two
operands, or ternary, meaning it has three operands.
• An implementation of such a computation must cause two actions: fetching the operands,
usually from memory, and executing arithmetic operations on those operands.
Operator Evaluation Order
• The operator precedence and associativity rules of a language dictate the order of evaluation of its
operators.
• Precedence
The value of an expression depends at least in part on the order of evaluation of the operators in the
expression. Consider the following expression:
a+b*c
mathematicians long ago developed the concept of placing operators in a hierarchy of evaluation
priorities and basing the evaluation order of expressions partly on this hierarchy. For example, in
mathematics, multiplication is considered to be of higher priority than addition, perhaps due to its
higher level of complexity.
The operator precedence rules
• The operator precedence rules for expression evaluation partially define the order in which the
operators of different precedence levels are evaluated.
• In imperative languages, exponentiation has the highest precedence (when it is provided by the
language), followed by multiplication and division on the same level, followed by binary addition and
subtraction.
• Many languages also include unary versions of addition and subtraction. Unary addition is called the
identity operator because it usually has no associated operation and thus has no effect on its operand.
action on the same level. Unary minus, of course, changes the sign of its operand.
a + (- b) * c
is legal, but
a+-b*c
usually is not.
The operator precedence rules
Next, consider the following expressions:
-a/b
-a*b
- a ** b
In the first two cases, the relative precedence of the unary minus operator and the binary operator is irrelevant—
the order of evaluation of the two operators has no effect on the value of the expression. In the last case, however,
it does matter.
The common programming languages, only Fortran, Ruby, Visual Basic, and Ada have the exponentiation
operator. In all four, exponentiation has higher precedence than unary minus, so
- A ** B
is equivalent to
-(A ** B)
Associativity
• Associativity
Consider the following expression:
a-b+c–d
When an expression contains two adjacent(operators) occurrences of operators with the same level of
precedence, the question of which operator is evaluated first is answered by the associativity rules of the
language.
• Associativity in common languages is left to right, except that the exponentiation operator (when
provided) sometimes associates right to left. In the Java expression
a-b+c
the left operator is evaluated first.
Associativity
• Exponentiation in Fortran and Ruby is right associative, so in the expression
A ** B ** C
the right operator is evaluated first.
• In Visual Basic, the exponentiation operator, ^, is left associative.
• The associativity rules for a few common languages are given here:
• Language Associativity Rule
Ruby Left: *, /, +, - Right: **
C-based languages Left: *, /, %, binary +, binary - Right: ++, --, unary -, unary +
Relational and Boolean Expressions
• A relational operator is an operator that compares the values of its two
operands.
• Boolean expressions consist of Boolean variables, Boolean constants,
relational expressions, and Boolean operators. The operators usually
include those for the AND, OR, and NOT operations.
• In the mathematics of Boolean algebras, the OR and AND operators must
have equal precedence. However, the C-based languages assign a higher
precedence to AND than OR.
Relational and Boolean Expressions
• because arithmetic expressions can be the operands of relational expressions, and relational expressions can be the
operands of Boolean expressions, the three categories of operators must be placed in different precedence levels,
relative to each other.
• The precedence of the arithmetic, relational, and Boolean operators in the C-based languages is as follows:
Highest postfix ++, --
unary +, unary -, prefix ++, --, !
*, /, %
binary +, binary -
<, >, <=, >=
=, !=
&&
Lowest ||
Assignment Statement
• The assignment statement is one of the central constructs in imperative languages. It
provides the mechanism by which the user can dynamically change the bindings of
values to variables.
• Simple Assignments
Nearly all programming languages currently being used the equal sign for the
assignment operator. All of these must use something different from an equal sign for
the equality relational operator to avoid confusion with their assignment operator. For
example,
a=b + c
Compound Assignment Operators
• A control structure is a control statement and the collection of statements whose execution it
controls.
Selection Statement
• A selection statement provides the means of choosing between two or
more execution paths in a program.
• Selection statements fall into two general categories: two-way and n-way,
or multiple selection.
Two-Way Selection Statements
/* C++ Selection Statements - C++ if Statement */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if(num/2==0)
{
cout<<"You entered an even number";
}
getch();
}
<!DOCTYPE HTML>
<html>
<head>
<title>JavaScript if-else Statement</title>
</head>
<body>
</body>
</html>
Nesting Selectors
• <if_stmt> → if <logic_expr> then <stmt>
| if <logic_expr> then <stmt> else <stmt>
• the nested if, can be written in Ruby as follows:
• if sum == 0 then
if count == 0 then
result = 0
else
result = 1
end
end
public class JavaProgram
{ Multiple-Selection Statements
public static void main(String args[])
{
int month = 4;
String season;
switch(month)
{
case 12:
case 1:
case 2: season = "Winter";
break;
case 3:
case 4:
case 5: season = "Spring";
break;
case 6:
case 7:
case 8: season = "Summer";
break;
case 9:
case 10:
case 11: season = "Autumn";
break;
default: season = "Bogus Month";
}
System.out.println("April is in the " + season);
}
}
Iterative Statement
• An iterative statement is one that causes a statement or collection of statements to be executed zero, one, or
more times. An iterative statement is often called a loop.
• The primary possibilities for iteration control are logical, counting, or a combination of the two.
• The body of an iterative statement is the collection of statements whose execution is controlled by the
iteration statement.
• In some situations, it is convenient for a programmer to choose a location for loop control other than the
top or bottom of the loop body.
The for Statement
<!DOCTYPE html>
<html>
<head>
<title>for Loop Example in PHP</title>
</head>
<body>
<?php
echo "<h2>Printing number from 1 to 10</h2>";
for($x = 0; $x <= 10; $x++)
{
echo $x;
echo " ";
}
?>
</body>
</html>
Python for Loop - Count Backward
# Python for loop - counting backward example
print("Welcome to counting backward example code using Python for
loop.");
print("Counting in backward started...");
for num in range(10, 0, -1):
print(num);
Logically Controlled Loops
/* C# Loops - The while Loop - Example Program */
using System;
namespace WhileLoop
class WhileClass
int i=1;
while(i<10)
i++;
Console.ReadLine();
}
Iteration Based on Data Structures
For example, consider the following C# code:
List<String> names = new List<String>();
names.Add("Bob");
names.Add("Carol");
names.Add("Alice");
...
foreach (String name in names)
Console.WriteLine(name);