Podcast
Questions and Answers
Given int x = 20;
and int y = 3;
, what is the result of the expression x % y
?
Given int x = 20;
and int y = 3;
, what is the result of the expression x % y
?
- 23
- 5
- 6
- 2 (correct)
If int a = 8;
and int b = 12;
, which of the following expressions will return false
?
If int a = 8;
and int b = 12;
, which of the following expressions will return false
?
- a > b (correct)
- a <= b
- a < b
- b >= a
What is the purpose of relational operators in C#?
What is the purpose of relational operators in C#?
- To perform mathematical calculations on numerical values.
- To assign values to variables.
- To determine the relationship between operands and return a Boolean value. (correct)
- To combine multiple conditions into a single expression.
If int p = 7;
and int q = 4;
, what is the value of (p * 2) - q
?
If int p = 7;
and int q = 4;
, what is the value of (p * 2) - q
?
Which operator returns the remainder of a division operation?
Which operator returns the remainder of a division operation?
Given int m = 15;
and int n = 5;
, what would m / n
evaluate to?
Given int m = 15;
and int n = 5;
, what would m / n
evaluate to?
What will be the output of the following C# code snippet?
int x = 3;
int y = 7;
Console.WriteLine(x >= 5 && y < 10);
What will be the output of the following C# code snippet?
int x = 3;
int y = 7;
Console.WriteLine(x >= 5 && y < 10);
If int c = 4;
and int d = 6;
, what is the result of c * (d + c)
?
If int c = 4;
and int d = 6;
, what is the result of c * (d + c)
?
Flashcards
Operators
Operators
Symbols that perform specific mathematical or logical processes in programming.
Arithmetic Operators
Arithmetic Operators
Operators that perform math calculations on numeric values.
Addition Operator (+)
Addition Operator (+)
Adds two operands together.
Subtraction Operator (-)
Subtraction Operator (-)
Signup and view all the flashcards
Multiplication Operator (*)
Multiplication Operator (*)
Signup and view all the flashcards
Division Operator (/)
Division Operator (/)
Signup and view all the flashcards
Modulus Operator (%)
Modulus Operator (%)
Signup and view all the flashcards
Relational Operators
Relational Operators
Signup and view all the flashcards
Study Notes
- Operators are symbols for mathematical or logical processing in programming.
- Operators specify the operations performed on operands.
- C# operators include: arithmetic, relational, logical, and assignment operators.
Arithmetic Operators
- Arithmetic operators perform mathematical operations on numerical values.
Basic Arithmetic Operators
- Addition (+): Adds two operands.
- Example: a + b (if a = 11, b = 5) returns 16.
- Subtraction (-): Subtracts second operand from the first.
- Example: a - b returns 6.
- Multiplication (*): Multiplies two operands.
- Example: a * b returns 55.
- Division (/): Divides the first operand by the second.
- Example: a / b returns 2.
- Modulus (%): Returns the remainder of dividing the first operand by the second.
- Example: a % b returns 1.
Relational Operators
- Relational operators determine relationships between numeric operands.
- They return Boolean values: true or false.
- Greater Than (>): Checks if the first operand is greater than the second.
- Example: a > b (if a = 11, b = 5) returns true.
- Less Than (<): Checks if the first operand is less than the second.
- Example: a < b returns false.
- Greater Than or Equal To (>=): Checks if the first operand is greater than or equal to the second.
- Example: a >= b returns true.
- Less Than or Equal To (<=): Checks if the first operand is less than or equal to the second.
- Example: a <= b returns false.
- Equal To (==): Checks if two operands are equal.
- Example: a == b returns false.
- Not Equal To (!=): Checks if two operands are not equal.
- Example: a != b returns true.
Logical Operators
- Logical operators perform logical operations using logical expressions.
- These operators return a Boolean value.
Logical Operators
- AND (&&): Returns true if both operands are true, else false.
- Example: A && B (if A = true, B = false) returns false.
- OR (||): Returns true if at least one operand is true, else false.
- Example: A || B returns true.
- XOR (^): Returns true if only one operand is true, else false.
- Example: A ^ B returns true.
- NOT (!): Reverses a Boolean variable's value.
- Example: !A returns false.
Assignment Operators
- Assignment operators assign a value or expression result to a variable.
Assignment Operators
- Assign (=): Assigns a value or expression to the left variable.
- Example: b = a (if a = 2, b = 5) assigns the value of a to b, so b now equals 2.
- Add and Assign (+=): Adds the left operand to the first operand and assigns the result to the first operand.
- Example: b += a means b = b + a, so b now equals 7.
- Subtract and Assign (-=): Subtracts the second operand from the first and assigns the result to the first operand.
- Example: b -= a; If b is 5 and a is 2, b becomes 3.
- Multiply and Assign (*=): Multiplies both operands and assigns the result to the first operand.
- Example: b *= a; If b is 5 and a is 2, b becomes 10.
- Divide and Assign (/=): Divides the first operand by the second and assigns the result to the first operand.
- Example: b /= a; If b is 5 and a is 2, b becomes 2.
- Modulus and Assign (%=): Assigns the remainder of the first operand divided by the second to the first operand.
- Example: b %= a; If b is 5 and a is 2, b becomes 1.
- Increment (++) Adds 1 to the first operand and assigns the result to the first operand.
- Example: b++ means b = b + 1, so b now equals 6.
- Decrement (--) Subtracts 1 from the first operand and assigns the result to the first operand.
- Example: b-- means b = b - 1, so b now equals 4.
Expressions
- An expression is a combination of operands and operators evaluating to a single value.
- Integer Expressions: Evaluates to an integer value when all operands are integers.
- Example:
int x = 10 + 5 * 2;
evaluates to20
.
- Example:
- Floating-Point Expressions: Evaluates to a floating-point value if the operands are floating-point values.
- Example:
double y = 10 + 5 * 2.0;
evaluates to20.0
.
- Example:
Precedence and Associativity
- Operator precedence dictates the order of operator evaluation in an expression.
- Higher precedence operators are executed before lower precedence ones.
- Associativity determines evaluation order when operators share the same precedence.
- When operators of the same precedence are used in a single expression, they are evaluated from left to right.
- Multiplication has higher precedence than addition; assignment has the lowest.
- Example:
int a = 2 + 3 * 4;
evaluates multiplication first (3 * 4
), then adds2
.
- Example:
Math Class
- The
System.Math
class offers methods for various calculations. Pow()
raises a number to a specified power.- Example:
Math.Pow(2, 3)
returns 8.
- Example:
Exp()
raises the constant e to the given power.Log()
returns the natural and 10-based logarithm, respectively.Sqrt()
returns the square root of the given number.Sign()
returns the sign of the specified number.Abs()
returns the absolute value of a given number.Ceiling()
returns the smallest integral value larger than or equal to a fractional number.Floor()
returns the largest integral value smaller than or equal to the given fractional number.Round()
returns the number after rounding it to the nearest integral value.Truncate()
returns the number after removing its fractional part.Sin()
returns the sine of the specified angle.Cos()
returns the cosine of the specified angle.Tan()
returns the tangent of the specified angle.- The methods of Math class are static, and are used with the class name without instantiating an object.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.