Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: C# ternary expressions
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Ternary expressions are like short one line if statements. So here we have a program that goes around and asks the user
0:07
to enter a number between 1 and 1000. And then it parses it, and then here is a little if statement that's all on one line. A ternary expression.
0:17
First has the test, num < 100 if that's true, it's going to print out the first value or execute this command here, small.
0:27
If for some reason it's bigger or equal to 100 it's going to say huge. And then we just use that to print out The number is small or the number is huge
0:35
things like that. It's kind of a silly example, but you can definitely see what's happening here, right the idea is just use this one-liner
0:42
probably it's better in some kind of if statement but you know maybe not in this case. Let's run it real quick just to see what's happening.
0:51
We can go over here and say 10. The number's small. 99, 98, small. 100 and I'd don't know, 101. It's huge. Okay, if we hit enter, just leave.
1:04
All right looks like our program is working as expected. So here's the ternary expression, C#. Test? true : false
1:14
How do you know that it's true and then false and not false then true? You memorize it. That's how it works. There's not a whole lot of obvious reason
1:21
it's not like if you didn't know about this you would know what this meant, you just have to study it and learn it.
1:26
Not too bad, it's really good if you like little lambda expressions and stuff that you want to have, just a little test and then
1:31
some kind of conditional and as an expression and the value comes back, pretty nice for that.