To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 22871
22870  |  22872
Subject: 
Re: nqc macros
Newsgroups: 
lugnet.robotics
Date: 
Wed, 22 Sep 2004 13:21:09 GMT
Original-From: 
Steve Baker <sjbaker1@airmail.net[avoidspam]>
Viewed: 
916 times
  
roc wrote:
You could use a lot of nested "if", but the best solution is to use
the "switch" statement


right, but then i get confused on how to tell "switch" about the expression
i mean with "if" i did:

if (Random(1) == 0)
{
blabla
}
else
{
blabla
}

but how do i tell "switch" i want it to check the value of Random(6) and
execute different cases?

    switch ( Random(6) )
    {
      case 0 : do_something () ; break ;
      case 1 : do_something_else () ; break ;
      case 2 : do_something_different () ; break ;
      case 3 : do_something_wild_and_crazy () ; break ;
      case 4 : do_something_unexpected () ; break ;
      case 5 : do_something_deeply_meaningful () ; break ;
    }

What this does is to calculate the value of the expression between
the round brackets after the word "switch" - then, depending on the
value of that expression, it will jump down to whichever 'case' has
a matching value and start executing.

Each chunk of 'case' code will usually end with 'break' which
will cause the computer to skip the remaining cases and jump to
the '}' at the end of the "switch" statement.

So, (in the example above), it would call the Random(6) function
and get a random number in return...suppose it gets '4'.  Now,
the computer will jump down to the line beginning 'case 4 :' and
start running code from there.  Hence your robot will call the
function 'do_something_unexpected();' - and when that's done,
it'll hit the 'break' at the end of the 'case 4' line - and
jump down to the end of the 'switch' clause.

If you leave off the 'break' statement at the end of a case
clause, the computer will continue on into the next case
clause.

If none of the case clauses have the right number on them, then
none of them will be executed - if that matters to you, then
you can use a special 'case' clause:

      default :  do_something_interesting () ; break ;

....which is a kind of 'catch-all' that you can put at the
end of the list of case clauses.  The computer will jump to
the 'default' clause if the result of the 'switch' expression
doesn't match *any* of the numbered case clauses.

You can think of an 'if' statement as a special case of the
'switch' statement:

    switch ( ... )
    {
      case 0 :
        {
          ...    /* Do this if the expression is FALSE */
        }
        break ;

      default :
        {
          ...   /* Do this if the expression is *NOT* FALSE */
        }
        break ;
    }

...does exactly the same thing as:

    if ( ... )
    {
       ...   /* Do this if the expression is *NOT* FALSE */
    }
    else
    {
       ...   /* Do this if the expression is FALSE */
    }

...although it's a lot less convenient!

---------------------------- Steve Baker -------------------------
HomeEmail: <sjbaker1@airmail.net>    WorkEmail: <sjbaker@link.com>
HomePage : http://www.sjbaker.org
Projects : http://plib.sf.net    http://tuxaqfh.sf.net
            http://tuxkart.sf.net http://prettypoly.sf.net
-----BEGIN GEEK CODE BLOCK-----
GCS d-- s:+ a+ C++++$ UL+++$ P--- L++++$ E--- W+++ N o+ K? w--- !O M-
V-- PS++ PE- Y-- PGP-- t+ 5 X R+++ tv b++ DI++ D G+ e++ h--(-) r+++ y++++
-----END GEEK CODE BLOCK-----



1 Message in This Thread:

Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR