Teaching Myself C Sharp Lesson 5 Control Statements

After the frustration and failure of my last project and not being able to complete the necessary scripting to get it to run just rams home the importance as well as urgency of getting this stuff down pat.
Remember turning a weakness into a strength.
Well to get any strength that means training.
Training is repeating.
So I need to pull my finger out. 10 weeks to go.
Control Statements – controlling program flow.
A program needs to have a flow as well as a way to control the flow of the program. Control statements allow the program to be controlled as to which block of code runs, re-runs, skips, switches and stops running. The changes of flow in C# are:
º     Selection statements
º     Iterative statements

Selection Statements
Selection statements enable you to run particular blocks of code based on the outcome of a condition. The ‘if’ and the ‘switch’ statements are selection statements.
ifelsestatments.PNG
Nesting and Stacking ‘if’ Statements
Nesting is the inclusion of one statement within another. To nest an ‘if’ statement within another ‘if’ statement  you place the second ‘if’ statement within the first. You can nest within the ‘if’ or ‘else’ statement sections.
In the script above, to make it more effective you could do the following:
nesting
Stacking ‘if’ statements combines the ‘else’ statement with another ‘if’ statement.
stacking
DISCLIAMERObviously this book was written during less inclusive and less enlightened time
Gender and y also obviously exist!!
So no lynch mobs or trial by the masses for me…okay!? Everyone happy…somewhat?

The code in the above example is like the first example of code but the ‘else’ statement on line 13 is immediately followed by an ‘if’ statement. There are no braces or blocks.
The format for stacking is:

if ( condition 1)
{
//do something about condition 1
}
else if (condition 2)
{
//do something about condition 2
}
else if (condition 3)
{
//do something about condition 3
}
else if (condition x)
{
//do something about condition x
else
{
//all previous conditions failed
}

The ‘switch’ Statement
There are more easier ways to change the program flow depending upon multiple values stored in a variable. The format of a switch statement is:
Switch (value)
{
case result_1 :
//do stuff for result 1
break;
case result_2 :
//do stuff for result 2
break;

case result_n :
//do stuff for result _x
break;
default :
// do stuff for default case
}
switch.PNG

 
I didn’t end up finishing this blog on this lesson as I had problems with the code but got it sorted with some help.
I will be continuing the lessons I have learnt but will be using a new source.
Until then.

Peace and Blessings.

Advertisements

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s