Subscribe to Nakor's Blog        RSS Feed
-----

Programming Fundamentals Refresher

Icon 1 Comments
As it has been a while since I really put much thought into some of the basics of programming I thought I would make a post both as a refresher for myself and maybe as a tutorial/guide for people just getting started. I want to just go over a few simple loops and conditionals such as While, Do While, For, If, and maybe a Switch/Case if I don't run out of time. These will be written in C# but works pretty much the same in quite a few languages.

Loops are a way to test a conditional statement and while that statement is true keep executing the code inside its block.

While: only executes if the condition is true and will continue to execute while the condition remains true
bool condition = true;
int i = 0;

while (condition == true)
{
   Console.WriteLine("This will keep printing while the condition is true");
   i++;
   if (i == 10)
   {
      condition = false; // after the loop runs 10 times we set the condition to false to break out of the loop
   }
}



Do While: This loop will always execute at least once since it doesn't check the value of the condition until after the first run through its code block. From that point on it will keep executing as long as the condition is true.
do
{
   Console.WriteLine("This will keep printing while the condition is true");
   condition = false; 
} while (condition == true); // This loop only executes once since the condition is immediately set to false



For: the for loop is good to use when you only want to loop a given number of times. For instance, if you want to loop through all the elements in an array.
for(int i=0; i<10; i++)
{
   Console.WriteLine("This loop will execute 10 times, no more, no less");
}



Conditional statements are used to determine if a specific condition has been met before continuing down a specific code path, or to determine which code path to continue down.

If/Else If/ Else:
int condition = 1;

if (condition == 1)
{
   Console.WriteLine("The condition equals 1");
}
else if (condition == 2)
{
   Console.WriteLine("The condition equals 2");
}
else
{
   Console.WriteLine("The condition equals anything but 1 or 2");
}



Switch Statement: This is very similar to the if/else in function if not in appearance. It is good to use when you have a bunch of stacked else if's that all depend on the same variables value.
int condition = 1;

switch(condition)
{
   case 1:
          Console.WriteLine("The condition equals 1");
          break; // you use a break to get out of the switch case since our condition has been met
   case 2:
          Console.WriteLine("The condition equals 2");
          break;
   default:
          Console.WriteLine("Executes for any other value that condition equals"); // This is similar to the else in the if/else if/else
          break;
}



Well that's a pretty quick overview but I hope it gives at least a general idea of how these work.

1 Comments On This Entry

Page 1 of 1

alias120 Icon

26 April 2010 - 06:05 PM
Nice little overview, I have been doing something similar in my Blog. I believe it's helpful to new programmers to have this information available. I know that when i first began learning this material it was great to be able to see examples like these. Keep up the good work!

-alias
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

August 2014

S M T W T F S
     12
3456789
10111213141516
1718192021 22 23
24252627282930
31      

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)

    Categories