Ugrás a tartalomhoz

Szerkesztő:LinguisticMystic/cpp/IfElse

A Wikiszótárból, a nyitott szótárból

🔹 Basic Idea of Conditional Statements

[szerkesztés]

Conditional statements allow the program to make decisions based on Boolean expressions — expressions that are either true or false.

  • Example: a > b, x == 0, etc.
  • In C++, true is treated as 1, and false as 0.



🔹 The if Statement

[szerkesztés]
#include <iostream>

int main(){
    if(true){
        std::cout << "Useless check, this text will always be displayed" << std::endl;
    }

    bool isDark = 1; // same as true
    if(isDark){
        std::cout << "Yep, it's dark now!" << std::endl;
    }

    isDark = false;
    if(isDark){
        std::cout << "Nope, it's not dark now (light)!" << std::endl;
        std::cout << "But this text is NEVER printed!!" << std::endl;
    }

    return 0;
}

🧠 Code Explanation:

[szerkesztés]
  • The first if(true) always runs — it will always print.
  • isDark is initially true (1), so the second block runs.
  • Then isDark is set to false (0), so the last block is skipped.



🔹 Using if(divider) Instead of if(divider != 0)

[szerkesztés]

This:

if(divider){
    // code here
}

Is equivalent to:

if(divider != 0){
    // code here
}

✅ Why it’s useful:

[szerkesztés]

It’s shorter and leverages the C++ rule that 0 == false, and anything else is true.



🔹 if-else Statement

[szerkesztés]
#include <iostream>

int main(){
    int numerator, denominator;
    std::cin >> numerator >> denominator;

    if(denominator){
        std::cout << "The result is: ";
        std::cout << numerator / denominator << std::endl;
    }
    else{
        std::cout << "Unfortunately the divisor is 0" << std::endl;
    }

    return 0;
}

🧠 Code Explanation:

[szerkesztés]
  • The program reads two integers.
  • If the second number (denominator) is not zero, it performs division.
  • Otherwise, it safely handles division by zero and prints an error message.



🔹 else if Chains

[szerkesztés]
#include <iostream>

int main() {
    int number;
    std::cout << "Enter a number: ";
    std::cin >> number;

    if(number == 1) {
        std::cout << "Number is 1" << std::endl;
    } else if(number == 2) {
        std::cout << "Number is 2" << std::endl;
    } else if(number == 3) {
        std::cout << "Number is 3" << std::endl;
    } else if(number == 4) {
        std::cout << "Number is 4" << std::endl;
    } else {
        std::cout << "Number is not 1, 2, 3, or 4" << std::endl;
    }

    return 0;
}

🧠 Code Explanation:

[szerkesztés]
  • User inputs a number.
  • The program checks for 1–4 and prints a message.
  • If none match, it hits the final else.



🔹 Ternary Operator ?:

[szerkesztés]

Classic if-else form:

if (a > b) {
    max = a;
} else {
    max = b;
}

Simplified using ternary:

max = a > b ? a : b;

🧠 Explanation:

[szerkesztés]
  • condition ? trueValue : falseValue
  • If a > b is true, assign a to max; otherwise, assign b.



🔹 Example: Even or Odd

[szerkesztés]
#include <iostream>

int main() {
    int number;
    std::cout << "Enter a number: ";
    std::cin >> number;

    std::string result = (number % 2 == 0) ? "even" : "odd";
    std::cout << "The number is " << result << std::endl;

    return 0;
}

🧠 Code Explanation:

[szerkesztés]
  • number % 2 == 0 checks if the number is even.
  • Based on that, "even" or "odd" is assigned to result.



🔹 Example: Nested Ternary Operator

[szerkesztés]
#include <iostream>

int main() {
    int a, b;
    std::cout << "Enter a numbers: ";
    std::cin >> a >> b;

    std::string result = a == b ? "equal" :
                         a > b ? "more" : "less";

    std::cout << result << std::endl;

    return 0;
}

🧠 Code Explanation:

[szerkesztés]
  • If a == b, prints "equal".
  • If not, checks if a > b → prints "more" or "less" accordingly.
  • Demonstrates how ternary operators can be nested for compact multi-branch conditions.



Conclusion

[szerkesztés]
  • if, else if, else provide structured decision making.
  • Ternary operator ?: gives a concise way to assign values based on a condition.
  • Prefer readability: use ternary for simple conditions, and if-else for more complex logic.