Ternary Operators in C++ (Conditional Assignment)

preview_player
Показать описание


Gear I use:
-----------------

Рекомендации по теме
Комментарии
Автор

As a Sr. C++ developer, I am enjoying your series - I mentor new developers, and I have been watching your vids as alternate ways of explaining concepts to newcomers to C++.

You have a great way of explaining a topic in a digestible manner - keep it up TheCherno.

DeanMichaud
Автор

"This whole series is my opinion, really" and it just so happens your opinion is awesome.

LucidStew
Автор

A better way to write this kind of nested conditional at 4:50 is:

s_speed = s_level > 10 ? 15 :
s_level > 5 ? 10 :
5;

An old school C trick that with proper indentation makes the nested ternary assignment very nice to read and more readable than if...else if...else

jonsnow
Автор

You missed my favorite use of the ternary operator: conditional argument passing. You can use the ternary in the list of arguments to a function or method call like this:


SetSpeed(s_Level > 5 ? 10 : 5);


So the ternary is not just a replacement for an if statement. It can be used in places an if statement can not.

AndrewEsh
Автор

I think that the ternary operator when - even when nested - can lead to more readable code

int a{ 30 };
int b{ 20 };

(a == b) ? cout << "a, b equal" :
(a > b) ? cout << "a greatest" : cout << "b greatest";

It's easy to follow the logic and it's easier to see it and take it in when it's formatted like this. The equivalent if-else would be harder to read.

Also, you can init a const with the ternary operator.

const int isEqual = (a == b) ? true : false;

MultiPwolf
Автор

Nesting ternary statements was not a concept I had considered before watching this, thanks.

TheMastercheeff
Автор

# max value for 3 variable
#include<iostream>
using namespace std;

static int a = 10;
static int b = 1000;
static int c = 500;
int main()
{

int value = a>b? a>c? a:c:b>c? b:c; //max value
cout<<value<<endl;

return 0;
}

Buzz-pcep
Автор

For someone coming from C# and modern JavaScript, I appreciate your opinion on these videos. They have helped me a ton as I get back into C/C++. Thanks!

WillSams
Автор

You are seriously spoiling us with the daily videos. <3

systematicloop
Автор

I just noticed the increase in the terminal font sizesince the early videos, very nice upgrade thank you.

mytech
Автор

I like to put extra brackets around the boolean part like this :
s_Speed = (s_Level > 5) ? 10 : 5;
To clear up that this is like the condition block of an if statement. Looks much clearer in my opinion even though it is 2 extra symbols

redcrafterlppa
Автор

You are above s_Level 10 but we do not grant you the rank of "Master"

OnoxOrion
Автор

Simple, understandable, without too much talking. Thanks

CaptainCyGr
Автор

i like how your intro gets all smashed together, esp watching at 2x -- "hewsupguysmynimsaCherno and welcome back to my c++ series."

ehrenmurdick
Автор

Thanks Cherno ! I'm really enjoying your series :-)

neiltsakatsa
Автор

in each of the video of this tutorial series I learned something new even though I am an intermediate level at programming... kudos to you

motionsmoments
Автор

Thanks for this, didn't know you could nest it!

h.hristov
Автор

A nice video that i was able to digest much easier than some of the harder ones. Great series. Will be contributing to patreon 1st April to get whole months worth.

websurfer
Автор

Wow! I didn't even know you could nest ternary operators! That was neat, and complicated for my future self that needs to read that at the same time! lol

ParhamSalamati
Автор

Damn, we are on a 4 day streak! Good job Cherno :) Really love your content

KPkiller