View unanswered posts | View active topics It is currently Sat Sep 19, 2020 7:19 pm

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
logic within if conditions is not working correctly
Author Message
Rookie

Joined: Mon Mar 02, 2009 7:03 pm
Posts: 6
logic within if conditions is not working correctly
One of my costudents at university found another bug:
 Code:bool a = true;if (a == true) { foo();}else { bar();}

Behaviour: bar();
Expected behaviour: foo()

 Code:bool a = false;if (a == false) { foo();}else { bar();}

Behaviour: bar();
Expected behaviour: foo()

 Code:bool a = false;if (a == true) { foo();}else { bar();}

Behaviour: foo();
Expected behaviour: bar()

 Code:bool a = true;if (a == false) { foo();}else { bar();}

Behaviour: foo();
Expected behaviour: bar()

Here is a solution that "fixes" it partly:
 Code:bool valid (bool stmta, bool stmtb) { if (stmta) {  if (stmtb) {   return false;  }  else {   return true;  } } else {  if (stmtb) {   return true;  }  else {   return false;  } }}

Now you can do this:
 Code:bool a = true; if(valid(a,true)) { foo();}else { bar();}

Now this code will result in the correct statement executed.

Fri Mar 06, 2009 3:58 pm
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Re: logic within if conditions is not working correctly
besides this bug (that has been reported before) there is in ANSI C actually no data type "bool" (like in Pascal).
true and false are values of a data Type char!
In RobotC this is likely.

In ANSI C "false" is equal to zero, and "true" is "not false" (unequal to zero).

But on the other hand the condition
 Code: if (a==true)

is only "true" if a is =1,
but
 Code:if (a)

is also true if a is =2 or 4 or -90.

so: in conditions it is better to write
 Code:if (a)//orif (a!=false)

 Code:if (a==true)

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

Fri Mar 06, 2009 5:22 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 2 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning