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

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 02 (Part III) Introduction to C++ Programming."— Presentation transcript:

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

2 Goal To review Part II. Example: –How to make decisions. If-statement Debugging skills.

3 Exercise #include 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).

4 Review

5 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

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

7 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 to 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;

8 Common Programming Error Placing a semicolon immediately after the 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;

9 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: GoodBad int A, B, C, count; int A, B, C, count; cout << “Hello” << end; cout << “Hello” << end;

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

11 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

12 Comparing Two Integers

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

14 using

15 Successively Obtaining Two Integers cin >> number1 >> number2; –The input will be split by space, tab, or newline characters, and separately stored into variables number1 and number2.

16 Comparing Two Integers

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

18 Commenting Codes Comment / Uncomment selected codes

19 Commenting Codes Condition: syntax error / semantic error Steps: –Comment codes until the program can properly execute. –Uncomment codes until the error is found.

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

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

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

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

24 Setting Breakpoints and Executing Program in Debug Mode 4.Click to resume execution, or click to stop the entire process immediately.


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

Similar presentations


Ads by Google