Transcript for:
Lecture on the Ternary Operator

hey sup people the trinary operator also known as the conditional operator it's a shortcut to using an if else statement when assigning or returning a value here's the formula we write a condition followed by a question mark if this condition is true we return some value if true or if that condition is false we return some other value here's an example first we'll use an if-else statement and then later we'll switch to the ternary operator so let's create a function that will find the maximum of two integers and i'm going to assign that to int max and we will invoke a function find max but we'll still need to declare it so let's pass in two integers maybe three and four and then i'm going to display whatever max is okay so let's declare this function so we're returning an integer the name is find max and let's set up some parameters into x and int y so if we're using an if else statement if we need to return x if it's larger we can check to see if x is larger than y if so then return x else return y so this does work so the max between three and four is four so if i switch three to five well then the max is five so a shortcut to writing an if else statement like this if we're returning or assigning a value is that we could instead use the tenary operator so let's return and then we have a condition so this is our condition is x greater than y then we add a question mark then the value we're returning if true so if x is greater than y let's return x colon then our value if this condition is false and here within our else statement we're returning y and we no longer need this if else statement and that's all there is to it so we cut down on all that code and now just have one line of code so it's kind of like a shortcut and this works much the same so the maximum between five and four is five and if i change five back to three well then the max is four so yeah that's the ternary operator it's a shortcut to using an if-else statement when assigning or returning a value you write some condition add a question mark like you're asking a question then list some value to return if this condition is true and then some other value if this condition is false so you just follow this formula so yeah that's the trenary operator if you would like a copy of this code i'll post this to the comment section down below and well yeah that's the canary operator nc