CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Is "else" always necessary?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

Is "else" always necessary?
PostPosted: Fri Oct 09, 2020 6:34 am     Reply with quote

Hi.
When I use if in the program, do I have to use else after it?
While checking any button or condition checking
Code:

if(input(pin_b0))
output_high(pin_D0);
//else ????

if(hsEnable>3)
hsOut = 0;
//else ???
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Oct 09, 2020 6:42 am     Reply with quote

Usually yes...
Consider your code example:
Quote:

if(input(pin_b0))
output_high(pin_D0);

The first time this test is made, and if true, the pin goes high
it will stay high forever unless your code 'elsewhere' resets the pin.

If you want the output pin to 'follow' or 'mimic' the input pin, you'll need the 'else' half of the IF statement.

Others can probably explain it better...

Jay
ressas



Joined: 15 Nov 2019
Posts: 135

View user's profile Send private message

PostPosted: Fri Oct 09, 2020 10:29 am     Reply with quote

temtronic wrote:
usually yes...
consider your code example

...
if(input(pin_b0))
output_high(pin_D0);
...

the first time this test is made, and if true, the pin goes high
it will stay high forever unless your code 'elsewhere' resets the pin.

if you want the output pin to 'follow' or 'mimic' the input pin, you'll need the 'else' half of the IF statement.

Others can probably explain it better...

Jay

Thanks temtronic.
What you said in the button example makes sense. However, is it also necessary for the variable? When the HSENABLE variable in the example is less than 3, I don't want to do anything. Then?
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Oct 09, 2020 10:41 am     Reply with quote

re:
Quote:

if(hsEnable>3)
hsOut = 0;

In this code,
the FIRST time hsEnable = 4, 5,6,....
then hsOut will be set ot 0.
AND stay there, forever.
It will not be changed to say 255 or 3 or 1, whenever hsEnable <4.

Jay
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Fri Oct 09, 2020 2:39 pm     Reply with quote

It's not strictly necessary. For example, if you want to do some kind of action where your else statement basically says "do nothing", then you can just not put anything after the "if" statement.

However, for my own sake, I usually prefer to do this in order to remind myself that I purposely want no action if my condition is not satisfied:
Code:
if (stmt)
{
   // do stuff   
}
else {}
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Oct 09, 2020 3:17 pm     Reply with quote

The problem with not specifying an 'else', action is that once the if (condition) is met and 'something is set or done, it will NOT be reset or undone.

In his first test, when the input pin B0 is high, the D0 pin gets sets high and stays high forever, even when the input pin goes low.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Fri Oct 09, 2020 3:51 pm     Reply with quote

temtronic wrote:
the problem with not specifying an 'else', action is that once the if (condition) is met and 'something is set or done, it will NOT be reset or undone.

In his first test, when the input pin B0 is high, the D0 pin gets sets high and stays high forever, even when the input pin goes low.


Yes in his case it's probably a good idea. But that's why I said it's not strictly necessary. In my example maybe you'd just increment a counter or something.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Fri Oct 09, 2020 11:56 pm     Reply with quote

Exactly.

It is perfectly reasonable to have a set of operations that you want to
occur 'if' a set of conditions are met, with no need for an 'else'.
However in probably 90% of situations, there will be 'other things' that
have to happen if the conditions are not met, and in this case an 'else'
is the way to go.

Draw out the situations:
Condition met, needs to do xxxxx.
Condition not met needs to do yyyyy.
If there is nothing in the 'yyyyy' section, then no 'else' is wanted or needed.
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Oct 12, 2020 1:33 am     Reply with quote

Ttelmah wrote:
Exactly.

It is perfectly reasonable to have a set of operations that you want to
occur 'if' a set of conditions are met, with no need for an 'else'.
However in probably 90% of situations, there will be 'other things' that
have to happen if the conditions are not met, and in this case an 'else'
is the way to go.

Draw out the situations:
Condition met, needs to do xxxxx.
Condition not met needs to do yyyyy.
If there is nothing in the 'yyyyy' section, then no 'else' is wanted or needed.


It's a personal programming style thing. In my own case, the 90% is the other way around. Most of the time there are no else statements.

For example:
Code:
 
// msg is a pointer to some prebuilt string.
if (RadioActive)
         SendMessage(msg, Radio);
if (ConsoleActive)
        SendMessage(msg, Console);
if (LoggingActive)
        LogData(msg);

There is no "else" execution required in this scenario
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Mon Oct 12, 2020 3:38 am     Reply with quote

I must admit for my customers, there would have to be handling to
ensure that if none of the outputs were 'active', some form of emergency
reporting was done.
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Oct 12, 2020 4:57 am     Reply with quote

re:
Quote:
if (RadioActive)
SendMessage(msg, Radio);

Question. Do you reset 'RadioActive' within the called function 'SendMessage(msg, Radio); ' ?

I'm assuming you do, otherwise once 'RadioActive' was set, you'd always call the 'sendmsg' function. I've been caught in that 'trap' a few times...

Sad
Now I always reset the 'flag' on the FIRST line of the called function, well mostly....

Jay
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Oct 12, 2020 6:04 am     Reply with quote

No. These values are set in the initialisation logic during the hardware and configuration phase. Is there are Radio plugged in? If so use it etc
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
jeremiah



Joined: 20 Jul 2010
Posts: 1355

View user's profile Send private message

PostPosted: Mon Oct 12, 2020 6:32 am     Reply with quote

asmallri wrote:


It's a personal programming style thing. In my own case, the 90% is the other way around. Most of the time there are no else statements.

For example:
Code:
 
// msg is a pointer to some prebuilt string.
if (RadioActive)
         SendMessage(msg, Radio);
if (ConsoleActive)
        SendMessage(msg, Console);
if (LoggingActive)
        LogData(msg);

There is no "else" execution required in this scenario


I am in a similar boat (though not 90/10...more like 70/30). Most of my code ends up being validation, and using if's without else's is a much cleaner method for me as it sets up a type invariant pattern to work with. I'm often times working through a list of invariants at the beginning of a function so that when I get to the bottom of my function and start doing math or whatever logic, I know that the data types I am using are safe to use within that context:

Code:

if(error_condition1){
   return error_code1;
}
if(error_condition2){
   return error_code2;
}
if(error_condition3){
   return error_code3;
}
if(error_condition4){
   return error_code4;
}

// now do logic knowing values won't overflow or be in an invalid state



It's really useful for message validation before acting on a command for example. If/else chaining quickly becomes unreadable and a huge maintenance hazard due to how many levels deep it starts to get. Some of it is also personal preference for me as well.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group