ITclub

العودة إلى C++
محاضرة 3

Operators (Increment/Decrement, Precedence, Logical)

معاملات الزيادة والنقصان، الأسبقية، والمعاملات المنطقية

ملخص المحاضرة

📜 Lecture 3: Operators (Increment, Logical, Priority)

This lecture details operator precedence, increment/decrement operators, and logical operators for comparisons.

Key Concepts

  • Increment/Decrement Operators:
    • Postfix (e.g., Z++ or Z--): "Use-then-change". The variable's original value is used in the expression, and then it is incremented/decremented by 1.
    • Prefix (e.g., ++Z or --Z): "Change-then-use". The variable is incremented/decremented by 1 first, and then its new value is used in the expression.
    • Example (Postfix): int Z=5; X = Z++; -> X becomes 5, then Z becomes 6.
    • Example (Prefix): int Z=5; X = ++Z; -> Z becomes 6, then X becomes 6.
  • Arithmetic Priorities: The order in which C++ performs operations.
    1. () (Brackets)
    2. ++, -- (Increment/Decrement)
    3. *, /, % (Multiplication, Division, Modulus)
    4. +, - (Addition, Subtraction)
    5. = (Equality/Assignment)
  • Logical (Comparison) Operators: Used to compare values, resulting in true (1) or false (0).
    • > (Greater than), < (Less than), >= (Greater or equal), <= (Less or equal), == (Equal to), != (Not equal).
  • Logical (Boolean) Operators: Used to combine multiple conditions.
    • && (AND): Returns true (1) only if all conditions are true.
      • (10 > 6 && 10 > 7) is true (1).
      • (10 > 6 && 10 > 11) is false (0).
    • || (OR): Returns true (1) if at least one condition is true.
      • (10 > 6 || 10 > 11) is true (1).
    • ! (NOT): Reverses the result.