💻

Understanding C++ Conditionals

Nov 5, 2024

Lecture on C++ Conditionals

Introduction to Conditionals

  • Actions often depend on certain conditions (e.g., if it rains, carry an umbrella).
  • In coding, operations may depend on some conditions, which are handled using conditionals.
  • C++ Conditionals: primarily use if, else, and else if statements.

If Statement

  • Syntax: if (condition) { /* code to execute if true */ }
  • Example: If it's rainy, execute the code within the curly brackets. If not, it won’t execute. if (rainy) { // carry umbrella }
  • Checking user input for marks: if (marks > 33) { cout << "Pass"; }

If-Else Statement

  • Use else for a different operation if the if condition is false.
  • Example: Print "Fail" if marks are less than 33. if (marks > 33) { cout << "Pass"; } else { cout << "Fail"; }

Indentation in C++

  • Indentation is critical for readability.
  • Use tabs or spaces for statements inside curly brackets.

If-Else If Statement

  • Use else if for multiple conditions.
  • Example: Assign grades based on marks. if (marks > 80) { cout << "A"; } else if (marks > 60) { cout << "B"; } else if (marks > 40) { cout << "C"; } else { cout << "Fail"; }

Nested If-Else Statement

  • Allows for multiple levels of conditions.
  • Example: Check if marks are a pass, then check the level of passing. if (marks > 33) { if (marks > 80) { cout << "Gracefully Passed"; } else { cout << "Just Passed"; } } else { cout << "Fail"; }

Logical Operators

  • Logical AND (&&): Both conditions must be true.
  • Logical OR (||): At least one condition must be true.
  • Example: if (condition1 && condition2) { // execute if both true } if (condition1 || condition2) { // execute if either is true }

Ternary Operator

  • Short form of if-else.
  • Syntax: condition ? expr1 : expr2
  • Example: string result = (marks > 33) ? "Pass" : "Fail"; cout << result;

Bitwise vs Logical Operators

  • Bitwise AND (&) and OR (|) work on bits.
  • Logical AND (&&) and OR (||) evaluate entire expressions.
  • Use logical operators to avoid unnecessary computations.

Switch Case

  • Used to compare a variable against multiple values.
  • Syntax: switch (expression) { case value1: /* code */; break; case value2: /* code */; break; default: /* code */; }
  • Example: Print day based on number. switch (day) { case 1: cout << "Monday"; break; case 2: cout << "Tuesday"; break; // other cases... default: cout << "Invalid"; }

Practical Examples

  • Program to determine if a number is odd or even using if-else.
  • Program using logical operators to evaluate multiple conditions.
  • Examples of switch case for more practical uses.

Common Errors and Tips

  • Ensure proper use of break statements in switch cases to avoid fall-through.
  • Distinguish between logical and bitwise operators to prevent logic errors.

Conclusion

  • Understanding conditionals is crucial for decision-making in code.
  • Practice using various conditional statements to become proficient.