Answers for "ternary conditional c++"

C++
4

conditional operator in cpp

//(expression 1) ? expression 2 : expression 3
//If expression 1 evaluates to true, then expression 2 is evaluated.
   int x, y = 10;

   x = (y < 10) ? 30 : 40;
   cout << "value of x: " << x << endl; //prints 40
Posted by: Guest on May-04-2020
0

c++ ternary operator vs if else

#include <iostream>

using namespace std;

int main() {

// METHOD 1 USING AN IF STATEMENT
    cout << "*******************************" << endl;
    cout << "Method 1 using the if statement" << endl;
    cout << "*******************************" << endl;
    for (int i {1}; i <= 100; i++){
        cout << i;
        if (i % 10 == 0){
            cout << endl;
        }else {
            cout << " ";
        }
    }
    cout << "n" << endl;
// METHOD 2 USING THE TERNARY STATEMENT
    cout << "***********************************" << endl;
    cout << "Method 2 using conditional operator" << endl;
    cout << "***********************************" << endl;
    for(int b{1}; b <= 100; b++){
    cout << b << ((b % 10 == 0) ? "n" : " ");//condition ? doThisIfTrue : doThisIfFalse
    }    
    return 0;
}
Posted by: Guest on August-23-2021
0

c++ ternary statement

x = condition ? expression1 : expression2

// Example:
double x = 1 > 0 ? 10 : 20; // put any value
Posted by: Guest on December-09-2020

Browse Popular Code Answers by Language