Lesson 3: Conditional Execution and Debugging

If all else fails
English Idiom

Practical Programming

So far all our programs had only one execution path: the program statements were always executed one after another exactly in the order they were written. Today we are going to learn how to enable computer to make decisions and execute different code paths depending on user input, results of calculations, etc.

The simple C/C++ language if() construct allows a code to be executed only when the condition is me. For example, the following code will print a message on the screen only when user enters zero:

int x;
in >> x;
if(x == 0) {
    // this will be executed only when user enters zero
    cout << “Good job!” << endl;
    cout << “You’ve entered zero!” << endl;
}

Note the curly brackets { } around the block of code that will be executed when the condition is met. They are required because you might have multiple statements in this block (as we do in the example above). Without the brackets, the compiler will not know where this block of code ends.

The if() statements can be nested. Good programmers use indentation for each if() “level”  to make the code more readable. Both tabs and spaces can be used for indentation:

if(x == 0) {
    if(y == 0) {
        cout << “Both x and y are zero” << endl;
    }
    if(z == 0) {
        cout << “Both x and z are zero” << endl;
    }
}

The if() condition can be very complex and include logical operators. For example, the code above can be re-written as follows:

if((x == 0) && (y == 0)) {
    cout << “Both x and y are zero” << endl;
}
if((x == 0) && (z == 0)) {
    cout << “Both x and z are zero” << endl;
}

Often, your program needs to execute one piece of code if a condition is true and another piece of code if it is false. You can write two if() statements to achieve this:

int x;
cin >> x;
if(x % 2 == 0) {
    cout << “You’ve entered an even number” << endl;
}
if(x %2 == 1) {
    cout << “You’ve entered an odd number” << endl;
}

Another (simpler and more readable) option is to use if/else language construct:

int x;
cin >> x;
if(x % 2 == 0) {
    cout << “You’ve entered an even number” << endl;
} else {
    cout << “You’ve entered an odd number” << endl;
}

If you have more than two options, you can “chain” if/else together so the “else” part of an if() will be the next if():

int x;
cin >> x;
if(x > 0) {
    cout << “You’ve entered a positive number” << endl;
} else if(x < 0) {
    cout << “You’ve entered a negative number” << endl;
} else {
    cout << “You’ve entered zero” << endl;
}

When you are writing complex programs, it might be hard to understand what exactly the program is doing by reading the code. With a debugger, you can look into the actual program behavior (e.g. will it go into “if” or “else”?), view variables, and even modify variables.

To start the debugger, move cursor to the line within the “main” function and press F9 to set a breakpoint: the debugger stops execution of the program when it gets to the breakpoint. Now you can start debugging by pressing F5. When program stops, you can view or modify values for all variables using “Debug” / “Windows” / “Locals” menu. To continue program execution until next breakpoint press F5; to execute a single line press F10. You can also add “Debug” toolbar to your Visual Studio toolbar (right click on empty space in the toolbar) to have quick access to all debugging commands.

Exercises

1)    Write a program that reads 2 numbers from user and prints the smaller number. For example, if user enters 8 and 4 then the program should print 4.
2)    Write a program that reads 3 integers a,b,c from user and prints the median (the integer in the middle) of the three integers (a,b,c). For example, if user enters 8,10, and 3 then the program should print the median 8.
3)    Write a game of 21: user enters two integers and wins only if the sum of the two integers is equal to 21.

This entry was posted in Practical Programming. Bookmark the permalink.

One Response to Lesson 3: Conditional Execution and Debugging

  1. Pingback: Practical programming « Random Notes

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>