How to use nested ternary operator in C++

2 Answers

0 votes
#include <iostream>

int main() {
    int n = 7 > 4 ? 9 : 3 > 4 ? 20 : 6; 
    
    std::cout << n << '\n';
    
    n = 3 > 4 ? 9 : 5 > 7 ? 20 : 18; 
    
    std::cout << n << '\n';
}



/*
run:

9
18

*/

 



answered Apr 25, 2020 by avibootz
0 votes
#include <iostream>

int main() {
    int a = 10;
    
    int b = a > 10 ?  24 : a > 5 ? 7 : 1;
     
    std::cout << b;
}
  
  
  
  
/*
run:
  
7
  
*/

 



answered Feb 2, 2023 by avibootz

Related questions

...