chapter 02 (part iii) introduction to c++ programming

24
Chapter 02 (Part III) Introduction to C++ Programming

Upload: angela-hoover

Post on 02-Jan-2016

232 views

Category:

Documents


3 download

TRANSCRIPT

Chapter 02 (Part III)

Introduction to C++ Programming

Goal

• To review Part II.

• Example:– How to make decisions.

• If-statement

• Debugging skills.

Exercise

#include <iostream>

int main(){

int number1;int number2;

std::cout << "Enter first number: "; std::cin >> number1

std::cout << "Enter second number: "; std::cint >> number2;

sum = number1 + number2;

std::cout << "Sum is " << sum << std::endl

}

Indicate all the syntax errors in the following C++ codes (Hint: 5 errors).

Review

If-statement

If (number1 == number2)

{

std::cout << number1;

std::cout << " is equal to ";

std::cout << number2;

std::cout << ".";

std::cout << std::endl;

}

Condition (logical calculation):True or False

Body

If number1 is equal to number2?

Body

true

false

2.7 Decision Making: Equality and Relational Operators

• Condition– The resulting value is either true or false.– Can be formed using relational operators.

• if statement– If condition is true, body of the if statement

executes.– If condition is false, body of the if

statement does not execute.

If-statement

• If there is only one statement in the body, the braces can be removed.

• Note:– Indent the statement(s) in the body of an if statement t

o enhance readability.

if (number1 == number2){ std::cout << number1; std::cout << " is equal to "; std::cout << number2; std::cout << "."; std::cout << std::endl; }

if (number1 == number2) std::cout << number1 << " is equal to " << number2 << "." << std::endl;

Common Programming Error• Placing a semicolon immediately after th

e right parenthesis after the condition in an if statement is often a logic error (although not a syntax error).

• Example:int A;cin >> A;If (A < 3);

cout << “A < 3.” << endl;

int A;cin >> A;If (A < 3) ;cout << “A < 3.” << endl;

Good Programming Practice• A lengthy statement may be spread over

several lines. – If a single statement must be split across lines,

choose meaningful breaking points.– Example:

Good Badint A, B, C,

count;

int A, B, C

, count;

cout << “Hello” <<

end;

cout << “Hello”

<< end;

Relational operators. Standard algebraic equality or relational operator

C++ equality or relational operator

Sample C++ condition

Meaning of C++ condition

Relational operators

> x > y x is greater than y

< x < y x is less than y

>= x >= y x is greater than or equal to y

<= x <= y x is less than or equal to y

Equality operators

= == x == y x is equal to y

≠ != x != y x is not equal to y

A syntax error will occur if any of the operators ==, !=, >= and <= appears with spaces between its pair of symbols.

Common Programming Error

• Do not confuse the equality operator == with the assignment operator = (logic errors). – The equality operator should be read “…is

equal to...”– The assignment operator should be read “

…is assigned the value of...”– Example:

int A; A = 3; //If (A == 3) //

std::cout << “A is equal to 3.” << std::endl;

A is assigned of the value of 3

if A is equal to 3

Comparing Two Integers

using

• Place using declarations immediately after the #include to which they refer.– using

• to import a namespace or parts of a namespace into the current scope.

– Example 1 (import an entire namespace):using namespace std;

– Example 2 (import part of a namespace):using std::cout;using std::cin;using std::endl;

• For readability, there should be no more than one statement per line in a program.

using

Successively Obtaining Two Integers

• cin >> number1 >> number2;– The input will be split by space, tab, or newlin

e characters, and separately stored into variables number1 and number2.

Comparing Two Integers

Additional Material: Debugging Skills

• When the structure of your program is getting complicated, you may encounter more semantic errors rather than syntax errors.

• Practice the following debugging skills to reduce development time:– Comment codes.– Output messages.– Set break points and execute program in debug

mode.

Commenting Codes

• Comment / Uncomment selected codes

Commenting Codes

• Condition: syntax error / semantic error

• Steps:– Comment codes until the program can

properly execute.– Uncomment codes until the error is found.

Outputting messages

• Insert std::cout between statements to output messages or variables for finding erroneous statements.– Show numbers or letters to indicate the order

of execution.

Setting Breakpoints and Executing Program in Debug Mode

1. Click the left side of editor to set breakpoints.

Breakpoints are where that you pause the execution process.

Setting Breakpoints and Executing Program in Debug Mode

2. Execute program in debug mode:

• The execution will pause at the statements marked by break points.

Setting Breakpoints and Executing Program in Debug Mode

3. Once the execution is paused, check variables to if they are given correct values.

• Move your cursor to a variable, and its value will pop out, or

• Key in equations to show the values of variables.

Type variable name you want to check and press “Down”.

Setting Breakpoints and Executing Program in Debug Mode

4. Click to resume execution, or click to stop the entire process immediately.