INF1021- Algorithmique: Operators and Calculations

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is an operator in the context of programming?

A symbol that performs an operation on one or more values (operands).

What is an operand?

A value or variable on which an operator acts.

Give an example of an expression, labeling the operators and operands.

In the expression 5 + 3, 5 and 3 are operands, and + is the operator.

What is a unary operator?

<p>An operator that acts on a single operand.</p> Signup and view all the answers

What is a negative number?

<p><code>-x</code></p> Signup and view all the answers

What expression adds two numbers using a binary operator?

<p><code>x + y</code></p> Signup and view all the answers

What types of data are operators linked to?

<p>Operators are linked to a type of data and work with certain types of data such as integers and booleans.</p> Signup and view all the answers

Which operator functions with numeric types?

<p><code>%</code></p> Signup and view all the answers

Which operator functions with booleans?

<p><code>&amp;&amp;</code></p> Signup and view all the answers

Match the operator with its description

<ul> <li>= Addition</li> </ul> <ul> <li>= Subtraction</li> </ul> <ul> <li>= Multiplication / = Division</li> </ul> Signup and view all the answers

Flashcards

What is an operator?

A symbol that performs an operation on one or more values (operands).

What is an operand?

A value or variable on which an operator acts.

What is a unary operator?

Acts on a single operand.

What is a binary operator?

Acts on two operands.

Signup and view all the flashcards

What are arithmetic operators?

Perform calculations like addition, subtraction, multiplication, etc.

Signup and view all the flashcards

What are assignment operators?

Assigns a value to a variable.

Signup and view all the flashcards

Increment/decrement operators

Increase or decrease the value of a variable, usually by 1.

Signup and view all the flashcards

Comparison operators

Compare values and return a Boolean (true or false) result.

Signup and view all the flashcards

Logical operators

Combine logical conditions.

Signup and view all the flashcards

What are bitwise operators?

Manipulate individual bits within a number.

Signup and view all the flashcards

String operators

Join or repeat strings and symbols.

Signup and view all the flashcards

Operator Precedence

A mechanism to solve expressions with multiple operators.

Signup and view all the flashcards

Study Notes

  • INF1021- Algorithmique discusses operators and calculations

What You Will Learn

  • Arithmetic, boolean, and comparison operators
  • String manipulation
  • Operator precedence and evaluation order

Operators

  • Operators are symbol(s) performing operations on one or more values (operands)
  • Operands: values or variables on which operators perform operations
  • Example: In "5 + 3", 5 and 3 are operands, and "+" is the operator

Operator Types

  • Operators can be unary, acting on one operand such as "-x" for negation
  • Operators can be binary, acting on two operands like "x + y" for addition
  • Operators are linked to data types, functioning with integers, booleans, etc.
  • "%" works with numeric types
  • "&&" works with booleans

Arithmetic Operators

  • Perform calculations
  • Apply to two numeric variables, a and b
  • Addition: a + b
  • Subtraction: a - b
  • Multiplication: a * b
  • Division: a / b
  • Integer Division: a // b
  • Exponentiation: a ** b
  • Modulo: a % b or a mod b
  • Integer Division: a div b
  • Absolute Value: abs(a)
  • Opposite Value: -a
  • Example: -c = b % a (5 % 2) results in c = 1, with 5 divided by 2 leaving a remainder of 1
  • d = b div a (5 div 2) results in d = 2, as 5 divided by 2 gives the integer 2
  • f = abs(-3) results in f = 3, as the absolute value of -3 is 3
  • n = a ** 3 (2 cubed) results in n = 8

Assignment Operators

  • Give value to a variable Addition Assignment: a += b
  • Subtraction Assignment: a -= b
  • Multiplication Assignment: a *= b
  • Division Assignment: a /= b
  • Modulo Assignment: a %= b
  • Equal: a b
  • "a += b," where a is 2 and b is 5, results in a being assigned the value of 7
  • "b *= 3," where b is 5, results in b being assigned the value of 15

Increment and Decrement Operators

  • Increase or decrease variable value (typically by 1)
  • a is a numeric variable
  • Pre-increment (++variable): ++a
  • Post-increment (variable++): a++
  • Pre-decrement (--variable): --a
  • Post-decrement (variable--): a--
  • Example:
  • If a = 2, then b = ++a results in a = 3 and b = 3 (pre-increment)
  • If b = 3, then c = b++ results in c = 3 and b = 4 (post-increment)

Comparison Operators

  • Compare values
  • Apply to two variables (a and b) of the same type
  • Greater Than: a > b
  • Greater Than or Equal To: a >= b
  • Less Than: a < b
  • Less Than or Equal To: a <= b
  • Equal To: a == b
  • Not Equal To: a != b or a b
  • Example (with a = 2 and b = 5): -"a > b" results in FAUX (false) -"a != b" results in VRAI (true)

Logical Operators

  • Combine logical conditions
  • Apply to two boolean variables, a and b
  • Result of comparison is either VRAI (true) or FAUX (false)
  • AND (Conjunction): a ET b or a && b
  • OR (Disjunction): a OU b or a || b
  • NOT (Negation): NON a or !a

Truth Table

  • When variable A is true and variable B is true, the non A and non B column are false
  • When variable A is true and variable B is false, the non A column is false and non B Is true
  • When variable A is false and variable B is true, the non A column is true and Non B is false
  • When variable A is false and variable B is false, the non A and non B column are true

Truth Table

  • When variable A is true and variable B is true, the A and B column is true
  • When variable A is true and variable B is false, the A and B column is false
  • When variable A is false and variable B is true, the A and B column is false
  • When variable A is false and variable B is false, the A and B column is false

Truth Table

  • When variable A is true and variable B is true, the A or B column is true
  • When variable A is true and variable B is false, the A or B column is true
  • When variable A is false and variable B is true, the A or B column is true
  • When variable A is false and variable B is false, the A or B column is false

Binary Operators

  • Binary operators manipulate individual bits
  • Left Shift: "a << 1"
  • Right Shift: "a >> 3"
  • Example:
  • a = 4 (00000100 in binary)
  • b = 48 (00110000 in binary) -"a << 1" (Shift left by 1) results in a = 8 (00001000 in binary) -"b >> 2" (Shift right by 2) results in b = 12 (00001100 in binary)

String Operators

  • Used for alphanumeric strings
  • String Concatenation: a + b or a & b
  • String Duplication: a * 2
  • Example:
  • If a = "deux" and b = "variables"
  • "c = a + b" results in c = "deuxvariables" -"d = a * 3" results in d = "deuxdeuxdeux"

Operator Overview by Type

  • Integer: +, -, *, /, div, mod, **, <, <=, >, >=, ==, !=
  • Real: +, -, *, /, **, <, <=, >, >=, ==, !=
  • Character: <, <=, >, >=, ==, !=, +, *
  • String: <, <=, >, >=, ==, !=, +, *
  • Boolean: <, <=, >, >=, ==, !=, ET (AND), OU (OR), NON (NOT)

Operator Precedence (Highest to Lowest)

  • Parentheses: ()
  • Increment/Decrement: ++, --
  • Exponents: **
  • Multiplication, Division, Modulo: *, /, %
  • Addition, Subtraction: +, -
  • Bitwise Shift: <<, >>
  • Comparison: ==, !=, <, <=, >, >=
  • Logical: ET (AND), OU (OR)
  • Assignment: =, +=, -=, *=
  • String Operations: +, *

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Arithmetic Operators Precedence Quiz
16 questions
Python Arithmetic Operators Quiz
10 questions
C# Arithmetic and Relational Operators
18 questions
C# Operators: Arithmetic & Relational
8 questions
Use Quizgecko on...
Browser
Browser