Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
27
Добавлен:
25.03.2016
Размер:
14 Мб
Скачать
☆

66

Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008

Tip You can also press F5 to continue execution in the debugger.

The application completes and finishes running.

Congratulations! You’ve successfully written and called methods and used the Visual Studio 2008 debugger to step in and out of methods as they run.

If you want to continue to the next chapter

Keep Visual Studio 2008 running, and turn to Chapter 4, “Using Decision Statements .”

If you want to exit Visual Studio 2008 now

On the File menu, click Exit. If you see a Save dialog box, click Yes (if you are using Visual Studio 2008) or Save (if you are using Visual C# 2008 Express Edition) and save the project.

Chapter 3 Quick Reference

To

Do this

Declare a method

Write the method inside a class. For example:

 

int addValues(int leftHandSide, int rightHandSide)

 

{

 

...

 

}

Return a value from inside a

Write a return statement inside the method. For example:

method

return leftHandSide + rightHandSide;

 

Return from a method before

Write a return statement inside the method. For example:

the end of the method

return;

 

Call a method

Write the name of the method, together with any arguments

 

between parentheses. For example:

 

addValues(39, 3);

Use the Generate Method Stub

Right-click a call to the method, and then click Generate Method Stub

Wizard

on the shortcut menu.

 

 

Display the Debug toolbar

On the View menu, point to Toolbars, and then click Debug.

 

 

Step into a method

On the Debug toolbar, click Step Into.

 

or

 

On the Debug menu, click Step Into.

 

 

Step out of a method

On the Debug toolbar, click Step Out.

 

or

 

On the Debug menu, click Step Out.

 

 

Chapter 4

Using Decision Statements

After completing this chapter, you will be able to:

Declare Boolean variables.

Use Boolean operators to create expressions whose outcome is either true or false.

Write if statements to make decisions based on the result of a Boolean expression.

Write switch statements to make more complex decisions.

In Chapter 3, “Writing Methods and Applying Scope,” you learned how to group related

statements into methods. You also learned how to use parameters to pass information to a method and how to use return statements to pass information out of a method. Dividing a

program into a set of discrete methods, each designed to perform a specific task or calculation, is a necessary design strategy. Many programs need to solve large and complex problems. Breaking up a program into methods helps you understand these problems and focus on how to solve them one piece at a time. You also need to be able to write methods that selectively perform different actions depending on the circumstances. In this chapter, you’ll see how to accomplish this task.

Declaring Boolean Variables

In the world of programming (unlike in the real world), everything is black or white, right or wrong, true or false. For example, if you create an integer variable called x, assign the value

99 to x, and then ask, “Does x contain the value 99?” the answer is definitely true. If you ask, “Is x less than 10?” the answer is definitely false. These are examples of Boolean expressions.

A Boolean expression always evaluates to true or false.

Note The answers to these questions are not definitive for all programming languages. An unassigned variable has an undefined value, and you cannot, for example, say that it is definitely less than 10. Issues such as this one are a common source of errors in C and C++ programs. The Microsoft Visual C# compiler solves this problem by ensuring that you always assign a value to a variable before examining it. If you try to examine the contents of an unassigned variable, your program will not compile.

67

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]