Sam Hendriks
Sam Hendriks

Reputation: 35

Can I shorten this if statement?

This if-statement currently compares a variable with a static number and if they are equal, it gives the settime7 variable a hexidecimal that it uses to send time to a unit.

Is there a way to shorten this if-statement:

if(timeM == 0){
   settime7 = 0x00;
}
else if(timeM == 1){
   settime7 = 0x01;
}
else if(timeM == 2){
   settime7 = 0x02;
}
else if(timeM == 3){
   settime7 = 0x03;
}
// ...and so on to timeM == 60 and settime = 0x3C.

Upvotes: 1

Views: 117

Answers (2)

Matthieu Brucher
Matthieu Brucher

Reputation: 22023

Just assign the variable:

settime7 = timeM;

Whether you write yourself the integer as decimal or hexadecimal doesn't matter for a stored integer.

Upvotes: 7

bruno
bruno

Reputation: 32586

if I well understand you want

...
else if(timeM == 60){
   settime7 = 0x60;
}

then :

settime7 = (timeM / 10) * 16 + (timeM % 10)

Upvotes: 4

Related Questions