15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Fixing a Program with a Debugger 101<br />

For example, suppose a program asks the user to type in his age. If you run<br />

your program and type in a valid age, such as 15, you can see how your program<br />

handles the number 15. But what happens if the user types in an<br />

invalid number, such as 0 or -17?<br />

A good programmer always makes sure the program doesn’t accept invalid<br />

data, but suppose part of your program changes the age data by mistake.<br />

Depending on the age, sometimes the program changes the age to 0, sometimes<br />

it changes it to a negative number (-12), sometimes it changes it to an<br />

outrageously large number (486).<br />

Book I<br />

Chapter 4<br />

Programming Tools<br />

To find out how and why your program seems to randomly change the age,<br />

you can step through your program and watch the number stored as the age.<br />

By changing your variable while the program is running, you can type in different<br />

age values to see how your program responds.<br />

By doing this, you can suddenly see that any time the age is less than 5, your<br />

program turns the age into 0; any time the age is greater than 5 but less than<br />

45, the program turns the age into a negative number; and any time the age<br />

is greater than 45, the program turns the age into a huge number, like 486.<br />

Without the ability to change the value of the age variable while the program is<br />

running, debugging a program is much slower and more tedious. By changing<br />

the value of the age variable while the program is running, you can test different<br />

values without having to trace through the program multiple times using<br />

different values. Just run the program once and change the value of the age<br />

variable as many times as you want, as shown in Figure 4-8.<br />

Figure 4-8:<br />

Watching<br />

and<br />

changing<br />

variables<br />

can show<br />

you how a<br />

program<br />

reacts to<br />

different<br />

data.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!