A method is, essentially, a mini-program within your larger program. It is a set of statements that execute one after the other, as in the following:
void MyMethod()
{
int a; // declare an integer
a = 5; // assign it a value
console.WriteLine("a: {0}", a); // display the value
}
Methods are executed from top to bottom. The compiler reads each line of code in turn and executes one line after another. This continues in sequence until the method branches. Branching means that the current method is interrupted temporarily and a new method or routine is executed; when that new method or routine finishes, the original method picks up where it left off. A method can branch in either of two ways: unconditionally or conditionally.
As the name implies, unconditional branching happens every time the program is run. An unconditional branch happens, for example, whenever the compiler encounters a new method call. The compiler stops execution in the current method and branches to the newly called method. When the newly called method returns (i.e., completes its execution), execution picks up in the original method on the line just below the line where the new method was called.
Conditional branching is more complicated. Methods can branch based on the evaluation of certain conditions that occur at runtime. For instance, you might create a branch that will calculate an employee's federal withholding tax only when their earnings are greater than the minimum taxable by law. C# provides a number of statements that support conditional branching, such as if, else, and switch.
A second way that methods break out of their mindless step-by-step processing of instructions is by looping. A loop causes the method to repeat a set of steps until some condition is met (e.g., "Keep asking for input until the user tells you to stop or until you receive ten values"). C# provides many statements for looping, including for, while, and do...while.
Unconditional Branching Statements:
The simplest example of an unconditional branch is a method call. When a method call is reached, there is no test made to evaluate the state of the object; the program execution branches immediately (and unconditionally) to the start of the new method.
You call a method by writing its name, for example:
UpdateSalary() // invokes the method UpdateSalary
As explained in the introduction, when the compiler encounters a method call, it stops execution of the current method and branches to the new method. When that new method completes its execution, the compiler picks up where it left off in the original method.
Execution begins in Main(), but branches to a method named SomeMethod(). The WriteLine() statements in each method assist you in seeing where you are in the code as the program executes.
Branching to a method:
using System;
class Functions
{
static void Main()
{
Console.WriteLine("In Main! Calling SomeMethod()...");
SomeMethod();
Console.WriteLine("Back in Main().");
}
static void SomeMethod()
{
Console.WriteLine("Greetings from SomeMethod!");
}
}
Output:
In Main! Calling SomeMethod()...
Greetings from SomeMethod!
Back in Main().
Program flow begins in Main() and proceeds until SomeMethod() is invoked. (Invoking a method is sometimes referred to as "calling" the method.) At that point, program flow branches to the method. When the method completes, program flow resumes at the next line after the call to that method.
Conditional Branching Statements:
While methods branch unconditionally, often you will want to branch within a method depending on a condition that you evaluate while the program is running. This is known as conditional branching. Conditional branching statements allow you to write logic such as "If you are over 25 years old, then you may rent a car".C# provides a number of constructs that allow you to write conditional branches into your programs.
if Statements :
The simplest branching statement is if. An if statement says, "if a particular condition is true, then execute the statement; otherwise skip it." The condition is a Boolean expression. An expression is a statement that evaluates to a value, and a Boolean expression evaluates to either true or false.
The formal description of an if statement is:
if (expression )
Statement1
This is the kind of description of the if statement you are likely to find in your compiler documentation. It shows you that the if statement takes an expression (a statement that returns a value) in parentheses, and executes Statement1 if the expression evaluates true.
if . . . else Statements:
Often, you will find that you want to take one set of actions when the condition tests true and a different set of actions when the condition tests false. This allows you to write logic such as "If you are over 25 years old, then you may rent a car; otherwise, you must take the train."
The otherwise portion of the logic is executed in the else statement. the test in the if statement fails (valueOne is not larger than valueTwo), the body of the if statement is skipped and the body of the else statement is executed. Had the test succeeded, the if statement body would execute and the else statement would be skipped.
Nested if Statements:
It is possible, and not uncommon, to nest if statements to handle complex conditions. For example, suppose you need to write a program to evaluate the temperature and specifically to return the following types of information:
-
If the temperature is 32 degrees or lower, the program should warn you about ice on the road.
-
If the temperature is exactly 32 degrees, the program should tell you that there may be ice patches.
-
If the temperature is higher than 32 degrees, the program should assure you that there is no ice.
If so, it prints one message; if not, the temp must be less than 32 and the program prints the next message. Notice that this second if statement is nested within the first if, so the logic of the else statement is: "since it has been established that the temp is less than or equal to 32, and it isn't equal to 32, it must be less than 32."
VISUAL STUDIO INTRODUCTION
C SHARP INTRODUCTION
C SHARP OUT LOOK
DOT NET AND C SHARP
C SHARP APPLICATION STRICTURE
OOPS INTRODUCTION
OOPS AND C SHARP
IDE AND C SHARP
INSTANTIATING OBJECTS IN C SHARP
CLASSES AND OBJECTS IN C SHARP
OPERATORS IN C SHARP
SWITCH AND ITERATION IN C SHARP
BRANCHING IN C SHARP
CONSTANTS AND STRING
STATIC AND INSTANCE MEMBERS IN DOT NET
No comments:
Post a Comment