C# Operators: Arithmetic & Relational

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

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?

  • a > b (correct)
  • a <= b
  • a < b
  • b >= a

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?

<p>10 (D)</p> Signup and view all the answers

Which operator returns the remainder of a division operation?

<p>% (C)</p> Signup and view all the answers

Given int m = 15; and int n = 5;, what would m / n evaluate to?

<p>3 (D)</p> Signup and view all the answers

What will be the output of the following C# code snippet?

int x = 3;
int y = 7;
Console.WriteLine(x >= 5 && y < 10);

<p>False (D)</p> Signup and view all the answers

If int c = 4; and int d = 6;, what is the result of c * (d + c)?

<p>40 (B)</p> Signup and view all the answers

Flashcards

Operators

Symbols that perform specific mathematical or logical processes in programming.

Arithmetic Operators

Operators that perform math calculations on numeric values.

Addition Operator (+)

Adds two operands together.

Subtraction Operator (-)

Subtracts the right operand from the left operand.

Signup and view all the flashcards

Multiplication Operator (*)

Multiplies two operands.

Signup and view all the flashcards

Division Operator (/)

Divides the left operand by the right operand.

Signup and view all the flashcards

Modulus Operator (%)

Returns the remainder after dividing the left operand by the right operand.

Signup and view all the flashcards

Relational Operators

Operators used to determine the relationship between two operands.

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 to 20.
  • Floating-Point Expressions: Evaluates to a floating-point value if the operands are floating-point values.
    • Example: double y = 10 + 5 * 2.0; evaluates to 20.0.

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 adds 2.

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.
  • 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.

Quiz Team

Related Documents

More Like This

C Programming Operators
5 questions
C Operator Mastery Quiz
10 questions

C Operator Mastery Quiz

TrustedReasoning avatar
TrustedReasoning
C# Arithmetic and Relational Operators
18 questions
Use Quizgecko on...
Browser
Browser