Читать книгу Beginning Programming All-in-One For Dummies - Wallace Wang - Страница 103

Watching variables

Оглавление

When you step or trace through a program, line-by-line, you can see how the program works. For more insight into your program's behavior, you can watch your variables.

You can read more about variables in Book 2, Chapter 2. For now, just think of a variable as a temporary place to store data, such as a number or a word.

Watching a variable lets you see what data your program is storing and using at any given time. That way, if your program is supposed to print a name but actually prints that person’s phone number, you can step through your program line-by-line and watch to see which line stores the wrong data for the program to print.

Not only can you “watch” how your program stores data, but a debugger lets you change data while your program is running. By changing data, you can see how your program responds.

For example, suppose a program converts temperatures between Celsius and Fahrenheit. If you store a valid temperature, such as 15, you can see how your program handles the number 15. But what happens if the user types in an invalid number, such as 500000 or –1700000?

To find out how and why your program seems to randomly change the temperature, you can step through your program and watch the number stored as the age. By changing your variable while the program is running, you can type in different temperature values to see how your program responds.

When testing different values, always start off with values for which you already know how your program should respond. For example, a temperature of 0 in Celsius should be 32 in Fahrenheit. Likewise, a temperature of 100 in Celsius should be 212 in Fahrenheit.

Without the ability to change the value of variables while the program is running, debugging a program is much slower and more tedious. By changing the value of variables while the program is running, you can test different values without having to trace through the program multiple times using different values. Just run the program once, and change the value of the variable as many times as you want, as shown in Figure 4-9.


FIGURE 4-9: Watching and changing variables can show you how a program reacts to different data.

Beginning Programming All-in-One For Dummies

Подняться наверх