I can't seem to understand any of it. I don't understand the ? or the : or what to write at the top of my head. I took a break fr a few days, went over the lessons again... nothing. I'm so confused.
Nothing is sticking in the last 3 lessons
Under discussion
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Andrea Joó
25 November 2021, 16:22
For me its also complicated. But there is a simpler solution. I asked a professional programmer, who showed me a very simple solution.
int min = a;
if (b<min)
min=b;
if (c<min)
min=c;
if(d<min)
min=d
So for example, if a =8, b=6
the min variable will change to 6. but if the next variable is 12, the variable will remain the same. I think it is the simplest solution so far.
0
Naughtless
10 May 2021, 05:26
Ternary operators like the ? and : are really just conveniences for more experienced programmers. It's just a shortcut if you don't want to write down an entire if else statement just to assign values to one or two variables. So just ignore those for now, I'm confident you'll come across those again in the future when you've completely understood if and else.
To be completely honest though, if this is your first time EVER touching programming, it's going to be hard. When I entered this course, I already learned the basics from a failed attempt at self-teaching myself C++ and a tiny bit of Java knowledge. But the early phases of learning C++ was quite frustrating.
So just hang in there! It only gets easier after this.
+2
Daniel Ritter Full Stack Developer at cituro GmbH
8 May 2021, 16:04
you don't have to use the ternary operator to solve the tasks.
ternary operator: is a short way to write if-else-constructs (conditional assignments).
would be the same like this:
+1
Guadalupe Gagnon
8 May 2021, 16:48
just fyi, the ternary operator, while similar, is not a short hand way to write if-else statements. The only thing a ternary operator can be used for is setting a value based on the condition. The correct way to describe a ternary operator is:
<a boolean expression> ? <result if true> : <result if false>
if-else statement, on the other hand, are a way to create branching code logic. You can't use the ternary operator in that sense. It completely fails if it is not used to set a value:
+1
Daniel Ritter Full Stack Developer at cituro GmbH
8 May 2021, 20:18
Right. I expressed myself incorrectly. My English is not so good :)
Therefore the example ;)
+1
Guadalupe Gagnon
9 May 2021, 04:19
No worries, and your help to the OP was spot on.
When I first learned the ternary operator I thought it was a shorthand for if-else. That led to some buggy code. I just try to make sure people understand the differences so they don't make the same mistakes that I have made.
+2
John Solomon Legara
11 May 2021, 06:54
I learned this the hard way. I, too, thought it was a shorthand for if-else. I realized it is close to python's way < result_a if condition else result_b >. Basically it needs to return something.
0