Learnings of the Week(Oct.13-17)
By: Frea Diane T. Bautista
This week, we learned alot about looping and branching. We used it in some of our activities but what do looping and branching really mean.
For me, The for Statement
This is the famous for loop structure that many of you have been using to write your own algorithms. The for loop is used when the number of loops is known (unlike the while loop, as we will see later). The syntax of the for loop is:
for (initialization; conditional expression; stepping)
{
statement 1;
statement 2;
}
As you can see, the initialization, the expression that controls the loop, and the stepping parts are all defined in the top of the statement and in one location. The parts are also separated by semicolons, and you begin the looped statements, or the statements that will be executed in the loop, using a block ( { } ). The first part of the for statement is the initialization part; use this part to initialize the variables that will be used in the algorithm of the for loop. Note that these variables are allocated on the stack, and this part will be executed only one time, because it doesn't make any sense to declare and initialize the same variable with the same value each time in the loop.
The next part is the conditional expression, which determines whether the loop will be executed again or not. If the condition (something like i < myArray.Length) evaluated to false, control passes to the first statement after the for statement block. If the condition evaluated to true, the body of the for block (the controlled statements) will be executed.
The last part is the stepping part. Usually it will be a counter that will increment the variable that the conditional expression uses, because at some point we need the conditional expression to evaluate to false and terminate the execution of the for loop. Note that the stepping part executes after the controlled statements. That is, first the initialization part executes (one time only) and the variables allocate space on the stack; second, the conditional expression is evaluated, and if true, the controlled statements execute, followed by the stepping part execution, and again the conditional expression is evaluated and the process iterates.
Note that any of the three parts that make the for statement can be empty; although it's not common, it can happen. Take a look:
static void Main(string[] args)
{
int x = 10;
for(; x < 100; x += 10)
{
Console.WriteLine(x);
}
Console.ReadLine();
}
Compile the method into a class and run the application; you will get the following result:
As you can see, we omit the initialization part and we just put in the semicolon. Also note that the counter can increment or decrement, and it's up to you to define the algorithm.
The for loop can be used to define very complex algorithms; this happens as a result of nesting the for loops. Let's take a very simple example which extends the above loop example. It will simply write the same numbers, but this time with a little difference.
public class Loops
{
static void Main(string[] args)
{
for(int x = 10; x < 100; x += 10)
{
Console.WriteLine();
Console.WriteLine(x);
for(int y = x - 1, temp = x - 10; y > temp; y--)
{
Console.Write("{0}, ", y);
}
}
Console.ReadLine();
}
}
Compile the class and run it, and you will get the following result:
I have used a nest for loop to print all the numbers between two iterations from the outer for loop. The ability to use for loops actually makes great programmers.
Saturday, October 18, 2008
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment