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

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

Jan 02, 2016

Download

Documents

Angela Hoover
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 02 (Part III) Introduction to C++ Programming.

Chapter 02 (Part III)

Introduction to C++ Programming

Page 2: Chapter 02 (Part III) Introduction to C++ Programming.

Goal

• To review Part II.

• Example:– How to make decisions.

• If-statement

• Debugging skills.

Page 3: Chapter 02 (Part III) Introduction to C++ Programming.

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).

Page 4: Chapter 02 (Part III) Introduction to C++ Programming.

Review

Page 5: Chapter 02 (Part III) Introduction to C++ Programming.

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

Page 6: Chapter 02 (Part III) Introduction to C++ Programming.

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.

Page 7: Chapter 02 (Part III) Introduction to C++ Programming.

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;

Page 8: Chapter 02 (Part III) Introduction to C++ Programming.

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;

Page 9: Chapter 02 (Part III) Introduction to C++ Programming.

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;

Page 10: Chapter 02 (Part III) Introduction to C++ Programming.

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.

Page 11: Chapter 02 (Part III) Introduction to C++ Programming.

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

Page 12: Chapter 02 (Part III) Introduction to C++ Programming.

Comparing Two Integers

Page 13: Chapter 02 (Part III) Introduction to C++ Programming.

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.

Page 14: Chapter 02 (Part III) Introduction to C++ Programming.

using

Page 15: Chapter 02 (Part III) Introduction to C++ Programming.

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.

Page 16: Chapter 02 (Part III) Introduction to C++ Programming.

Comparing Two Integers

Page 17: Chapter 02 (Part III) Introduction to C++ Programming.

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.

Page 18: Chapter 02 (Part III) Introduction to C++ Programming.

Commenting Codes

• Comment / Uncomment selected codes

Page 19: Chapter 02 (Part III) Introduction to C++ Programming.

Commenting Codes

• Condition: syntax error / semantic error

• Steps:– Comment codes until the program can

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

Page 20: Chapter 02 (Part III) Introduction to C++ Programming.

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.

Page 21: Chapter 02 (Part III) Introduction to C++ Programming.

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.

Page 22: Chapter 02 (Part III) Introduction to C++ Programming.

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.

Page 23: Chapter 02 (Part III) Introduction to C++ Programming.

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”.

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

Setting Breakpoints and Executing Program in Debug Mode

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