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

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 2 Introduction to C Programming
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Introduction to C++ Programming
Introduction to C++ Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Week 1 Algorithmization and Programming Languages.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Chapter 05 (Part III) Control Statements: Part II.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
計算機程式語言 Lecture 02-1 國立台灣大學生物機電系 林達德 2 2 Introduction to C++ Programming.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Structure of Simple C++ Program Chapter 1 09/09/13.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
Chapter 02 (Part II) Introduction to C++ Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Programming
Introduction to C# Applications
Chapter 2 Assignment and Interactive Input
CSC113: Computer Programming (Theory = 03, Lab = 01)
Introduction to Scripting
Control Statements Kingdom of Saudi Arabia
Introduction to C++ Programming
Introduction to C Programming
Introduction to C Programming
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
2.6 The if/else Selection Structure
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Introduction to C Programming
Presentation 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 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 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;

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;

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;

Relational operators. 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 newline 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.