ЁЯУЪ

Gateway Classes: Unit One-Shot Lecture

Jul 7, 2024

Gateway Classes: Unit One-Shot Lecture on Unit Two

Syllabus

  • Arithmetic Expressions
  • Operator Precedence
  • Conditional Branching

Operators

Definition

  • Operator: A symbol that instructs the compiler to perform specific mathematical or logical manipulations.
  • Operand: The item on which the operation is performed.
  • Example: In A + B, A and B are operands, and + is the operator.

Types of Operators

  1. Arithmetic Operators
    • + (addition)
    • - (subtraction)
    • * (multiplication)
    • / (division)
    • % (modulus)
  2. Relational Operators
    • < (less than)
    • <= (less than or equal to)
    • > (greater than)
    • >= (greater than or equal to)
    • == (equal to)
    • != (not equal to)
  3. Logical Operators
    • && (logical AND)
    • || (logical OR)
    • ! (logical NOT)
  4. Bitwise Operators
    • & (bitwise AND)
    • | (bitwise OR)
    • ^ (bitwise XOR)
    • ~ (bitwise NOT)
    • << (left shift)
    • >> (right shift)
  5. Assignment Operators
    • =
    • +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=
  6. Increment and Decrement Operators
    • ++ (increment)
    • -- (decrement)
  7. Conditional (Ternary) Operator
    • ? :
  8. Comma Operator
    • ,
  9. Sizeof Operator
    • sizeof

Unary, Binary, and Ternary Operators

  • Unary Operators
    • Requires one operand.
    • Examples: ++A, --B
  • Binary Operators
    • Requires two operands.
    • Examples: A + B, A - B
  • Ternary Operators
    • Requires three operands.
    • Example: condition ? expr1 : expr2

Precedence and Associativity

  • Precedence determines which operators are evaluated first in an expression.
  • Associativity determines the direction of evaluation (left-to-right or right-to-left) of operators with the same precedence.

Examples

  • * has higher precedence than +, so in A + B * C, B * C is evaluated first.
  • For operators like + and - with the same precedence, associativity is left-to-right, so in A - B + C, A - B is evaluated first.*

Type Conversion

  • Implicit Conversion (Type Promotion)
    • Done by the compiler automatically.
    • Example: mixing int and float in an expression results in the int being converted to float.
  • Explicit Conversion (Type Casting)
    • Done by the programmer.
    • Syntax: (type) value
    • Example: (int) 3.14

Control Statements

  • Branching
    • if
    • if-else
    • if-else-if
    • switch
  • Looping
    • while
    • do-while
    • for
  • Jumping
    • break
    • continue
    • goto

Example: Swapping Two Numbers Without Temporary Variable

#include <stdio.h> int main() { int a = 5, b = 3; a = a + b; // a = 8 b = a - b; // b = 5 a = a - b; // a = 3 printf("a = %d, b = %d", a, b); return 0; }

Example: Checking Even or Odd Number

int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num % 2 == 0) printf("%d is even", num); else printf("%d is odd", num); return 0; }

Quadratic Equation Roots

  • Formula: ax┬▓ + bx + c = 0
  • Discriminant: D = b┬▓ - 4ac
  • Nature of Roots
    • D > 0: real and distinct
    • D = 0: real and equal
    • D < 0: imaginary

Example: Finding Roots of a Quadratic Equation

#include <math.h> #include <stdio.h> int main() { double a, b, c, discriminant, root1, root2; printf("Enter coefficients a, b, and c: "); scanf("%lf %lf %lf", &a, &b, &c); discriminant = b * b - 4 * a * c; if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("Roots are real and distinct: %.2lf, %.2lf", root1, root2); } else if (discriminant == 0) { root1 = root2 = -b / (2 * a); printf("Roots are real and equal: %.2lf, %.2lf", root1, root2); } else { printf("Roots are imaginary"); } return 0; }

Character Type Check

Example: Check for Vowel or Consonant

#include <stdio.h> int main() { char c; printf("Enter a character: "); scanf(" %c", &c); switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': printf("%c is a vowel", c); break; default: printf("%c is a consonant", c); } return 0; }

Difference Between Switch and If-Else Statements

  • Switch
    • Syntax: switch(expression) { case value1: statements; break; ... default: ; }
    • Fast operation: Directly jumps to matching case.
    • Handles only integral types.
    • Requires break statements.
    • Case labels must be unique and constant.
  • If-Else
    • Syntax: if (condition) { statements } else { }
    • Slow operation: Sequential evaluation of conditions.
    • Handles various data types.
    • Does not require break statements.