dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [DotGNU]The C# Book - The plan


From: Peter Minten
Subject: Re: [DotGNU]The C# Book - The plan
Date: Mon, 08 Jul 2002 19:07:17 +0200

Peter Minten wrote:
 
> I'm going to do a little writing (about control structures and
> iteration), I'll post my work here so that it can be discussed.

I've completed the first draft of the control structures chapter. 
I've attached the text to this message.

I also have a proposal for the title that incorporates the 3 most
important elements: DotGNU, Portable.NET and C#: "Learning DotGNU -- C#
programming with Portable.NET".

Greetings,

Peter
TITLE: Learning DotGNU -- C# programming with Portable.NET

{{{{ CHAPTER: Control structures }}}}

Now that we can put some simple text on the screen, let's move on to
an important subject: control structures. The control structures allow
us to chose what code is executed. There are only 2 types of control
structures in C#: the if..then..else type and the switch type.

>>>> SECTION: If..Then..Else <<<<

-- BASIC --

Let's start with the if..then..else. This control structure uses an
expression and executes some code if true and some other code if
false.

The syntax of it is:
if(EXPRESSION)
  STATEMENT (or BLOCK OF STATEMENTS)
else
  STATEMENT (or BLOCK OF STATEMENTS)

If the expression evaluates to true the first block (the one under if)
is executed otherwise the second block (under else) is executed.

STORY
=====
The sensei explained the if..then..else control structure.
The student said "I understand, it's simply a switch, if the switch is
set on true then the true-code is used if the switch is set on false
then the false-code is used."
"I don't get it." said the doofuses.
"Allright. Here is an example: when you buy me a pizza I will be
pleased, else you will get headaches." said the sensei.
"No way luser!" the doofuses said.
The sensei picked up his walking stick and hit the doofuses on the
head with it.
"You didn't buy me a pizza so the expression 'the doofuses bought a
pizza for the sensei' evaluated to false, thus the else thing
happened" headaches."
=====

-- ADVANCED --

It is also possible to test for more than one expression. This is done
using 'else if', example:

if (EXPRESSION 1)
  STATEMENT
else if (EXPRESSION 2)
  STATEMENT
else
  STATEMENT

The else statement is executed when non of the expressions are true.

-- EXPERT --

There is an operator that uses the if..then..else system: the '?:' operator
or conditional operator. This operator is used this way:
CONDITINAL-OR-EXPRESSION ? EXPRESSION_TRUE : EXPRESSION_FALSE

The conditional-or-expression is something you but between the parentheses
of an 'if'. Expression_true is the expression that should be evaluated when the
conditional-or-expression is true, expression_false should be evaluated when
false.

Here is an example:

bool male = true;
String foo = male ? "sir" : "madam";
Console.WriteLine("Hello {0}", foo);

This produces "Hello sir" since male is true and thus evaluates to true (logical
isn't it?). The "sir" and "madam" parts in the string assignments may seem 
weird,
but a simple string also counts as an expression, that expression evaluates to
the expression (just think about it :-).

>>>> SECTION: Switch <<<<

-- BASIC --

Besides the if..then..else control structure that can only test for true or 
false
there is also a switch control structure that can test for more than 2 values.

A switch structure looks like this:

switch(EXPRESSION)
{
     case VALUE:
         STATEMENTS
         CASE_BLOCK_END_STATEMENT

     case VALUE:
         STATEMENTS
         CASE_BLOCK_END_STATEMENT
}

The expression returns a value, when that value is found in a 'case VALUE:'
part the code of that part is executed. The case block end statement is mostly
'break;' which simply marks the end of the case block and makes the program
exit the switch structure. If you omit the case block end statement a runtime
error occurs.

SIDE NOTE
=====
This is a real improvement over the old C language in which when a break;
statement was omitted the code of the next case block was executed, this
created big problems and is known as fall-through.
=====

Here is an example of a switch statement:

int i = 2;
switch(i)
{
    case 1:
        console.WriteLine("i = 1");
        break;

    case 2:
        console.WriteLine("i = 2");
        break;

    default:
        console.WriteLine("i != 1 and i != 2");
        break;
}

Notice the 'default' part. This is a special case block that is executed only
when no other case block matches. Default is always the last case block
in a switch structure. It's good practice always to include a default block
even if it can not be reached (in that case make it print an error and exit
the program because reaching an theoretically unreachable block is to
be considered a bug).

-- ADVANCED --

Besides the 'break;' statement one other statement is a typical block end
statement: 'goto LABEL'. Goto makes the program jump to the case block
with label (the 'case VALUE:' or 'default:' part is the label) LABEL. This
can be used to deliberately create fall-through (there are some situations
when fall-through is good). For example if we would replace the break
statement in the first case block of the example by 'goto default' (no : )
then if i was 1 this would be printed:
i = 1
i != 1 and i != 2

-- EXPERT --

Besides 'break' and 'goto' any other statement or block of statements that
prevent the program from reaching the end of a case block is valid
(like for(;;) (see iteration)).

All case blocks most have atleast one label. But more labels are permitted,
example:
case "Good":
case "Right":

reply via email to

[Prev in Thread] Current Thread [Next in Thread]