Chapter 2 Elementary Programming

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Chapter 2 Elementary Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Chapter 2 Elementary Programming
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Basic Elements of C++ Chapter 1.
Beginning C++ Through Game Programming, Second Edition
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Topic 2: Input / Output Formatting Lecturer:
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
TK1913 C++ Programming Basic Elements of C++.
Topic 2 Elementary Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++ Chapter 1.
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 Elementary Programming
Computing Fundamentals
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Chapter 2 Elementary Programming
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
elementary programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 Primitive Data Types and Operations
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Chapter 2 Elementary Programming

Motivations In the preceding chapter, you learned how to create, compile, and run a program. Starting from this chapter, you will learn how to solve practical problems programmatically. Through these problems, you will learn primitive data types and related subjects, such as variables, constants, data types, operators, expressions, and input and output.

Objectives To write C++ programs to perform simple calculations (§2.2). To read input from the keyboard (§2.3). To use identifiers to name elements in the program (§2.4). To use variables to store data (§§2.5-2.6). To program with assignment statements and assignment expressions (§2.6). To name constants using the const keyword and #define directive (§2.7). To declare variables using numeric data types (§2.8). To use operators to write numeric expressions (§2.8). To convert numbers to a different type using casting (§2.9). To represent character using the char type (§2.10). To become familiar with C++ documentation, programming style, and naming conventions (§2.12). To distinguish syntax errors, runtime errors, and logic errors (§2.13). To debug logic errors (§2.14).

Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area of the circle. ComputeArea Run

Trace a Program Execution animation Trace a Program Execution allocate memory for radius #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area cout << "The area is "; cout << area << endl; } radius no value

Trace a Program Execution animation Trace a Program Execution #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } memory radius no value area no value allocate memory for area

Trace a Program Execution animation Trace a Program Execution assign 20 to radius #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } radius 20 area no value

Trace a Program Execution animation Trace a Program Execution #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } memory radius 20 area 1256.636 compute area and assign it to variable area

Trace a Program Execution animation Trace a Program Execution #include <iostream> using namespace std; int main() { double radius; double area; // Step 1: Read in radius radius = 20; // Step 2: Compute area area = radius * radius * 3.14159; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; } memory radius 20 area 1256.636 print a message to the console

Reading Input from the Keyboard You can use the cin object to read input from the keyboard. #include <iostream> int main() { // Step 1: Read in radius double radius; std::cout << "Enter a radius: "; std::cin >> radius; // Step 2: Compute area double area = radius * radius * 3.14159; // Step 3: Display the area std::cout << "The area is " << area << std::endl; system("PAUSE"); return 0; } ComputeArea1.cpp

Reading Multiple Input in One Statement Example : Compute average of three numbers. #include <iostream> int main() { double x1,x2,x3, average; std::cin>> x1>> x2>> x3; average=(x1+x2+x3)/3; std::cout << "The average is " << average << std::endl; system("PAUSE"); return 0; }

Identifiers An identifier is a sequence of characters that consists of letters, digits, and underscores (_). An identifier must start with a letter or an underscore. It cannot start with a digit. An identifier cannot be a reserved word. (See Appendix A, “C++ Keywords,” for a list of reserved words.) An identifier can be of any length, but your C++ compiler may impose some restriction. Use identifiers of 31 characters or fewer to ensure portability. For example: area and radius are legal identifiers, whereas 2A and d+4 are illegal identifiers. (compiler reports syntax errors).

Variables // Compute the first area radius = 1.0; area = radius * radius * 3.14159; cout << area; // Compute the second area radius = 2.0;

Declaring Variables int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable; Variables of the same type can be declared together: Example: int i,j,k; //declare i,j,and k as int variables.

Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a; x=5*(3/2)+2 //Assign the value of the expression to x. x=y+1 // assign the addition of y and 1 to x. x=x+1 //the result of x+1 is assigned to x. If x is 1 before the statement is executed, then it becomes 2 after the statement is executed.

Declaring and Initializing in One Step int x = 1; This is equivalent to: int x; x=1; double d = 1.4; int i=1, j=2; int i(1), j(2);

Named Constants const datatype CONSTANTNAME = VALUE; const double PI = 3.14159; const int SIZE = 3;

Named Constants ComputeArea4.cpp #include <iostream> using namespace std; int main() { const double PI = 3.14159; // Step 1: Read in radius double radius = 20; // Step 2: Compute area double area = radius * radius * PI; // Step 3: Display the area cout << "The area is "; cout << area << std::endl; system("PAUSE"); return 0; } ComputeArea4.cpp