Podcast
Questions and Answers
What is an operator in the context of programming?
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?
What is an operand?
A value or variable on which an operator acts.
Give an example of an expression, labeling the operators and operands.
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?
What is a unary operator?
What is a negative number?
What is a negative number?
What expression adds two numbers using a binary operator?
What expression adds two numbers using a binary operator?
What types of data are operators linked to?
What types of data are operators linked to?
Which operator functions with numeric types?
Which operator functions with numeric types?
Which operator functions with booleans?
Which operator functions with booleans?
Match the operator with its description
Match the operator with its description
Flashcards
What is an operator?
What is an operator?
A symbol that performs an operation on one or more values (operands).
What is an operand?
What is an operand?
A value or variable on which an operator acts.
What is a unary operator?
What is a unary operator?
Acts on a single operand.
What is a binary operator?
What is a binary operator?
Signup and view all the flashcards
What are arithmetic operators?
What are arithmetic operators?
Signup and view all the flashcards
What are assignment operators?
What are assignment operators?
Signup and view all the flashcards
Increment/decrement operators
Increment/decrement operators
Signup and view all the flashcards
Comparison operators
Comparison operators
Signup and view all the flashcards
Logical operators
Logical operators
Signup and view all the flashcards
What are bitwise operators?
What are bitwise operators?
Signup and view all the flashcards
String operators
String operators
Signup and view all the flashcards
Operator Precedence
Operator Precedence
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.