vendredi 7 septembre 2018

if-else statement only works in a specific order and I don't know why

I recently started learning how to program and one of the tasks I was assigned was to write an algorithm in c++ that would ask for three numbers, then pick out the smallest one. A segment of the code i wrote is as follows.

if (a<=b) {a = d;} else {b = d;}

This is supposed to pick out the smallest of the first two numbers and then "link it" to d, but after a while of getting the wrong results I realized that regardless of the values of a and b it gives d a value of 0. Turns out this only works if I write the same thing with the characters flipped, with the input variables coming last:

if (a<=b) {d = a;} else {d = b;}

Why is that? Is there something about the syntax of c++ that I should know?

Seems like a strangly specific rule to have...

Aucun commentaire:

Enregistrer un commentaire