Chapter 1_1 Basics_Variables_DataTypes (2) PDF

Document Details

Jordan University of Science and Technology

Tags

C++ programming computer programming data types variables

Summary

This document introduces basic C++ programming concepts covering variables, data types, and operators. It explains the different data types found in C++ as well as different symbols associated with C++

Full Transcript

Reference: www.netacad.com 1 CHAPTER 1 Introduction to computer programming (Basics of C++, variables, and data types) Department of Computer Science Jordan University of Science and Technology Reference: www.netacad.com 2 Chapter 1 Objectives After com...

Reference: www.netacad.com 1 CHAPTER 1 Introduction to computer programming (Basics of C++, variables, and data types) Department of Computer Science Jordan University of Science and Technology Reference: www.netacad.com 2 Chapter 1 Objectives After completing this module, the student will be able to: Explain how a sample C++ program works Explain the concept of integers, floating-point numbers, operators and arithmetic operations in C++ programming Perform basic calculations Use the shortcut and pre/post increment/decrement operators Build simple expressions Translate verbal description into programming language Test code using known input and output data Compare values using relational operators Build Boolean expressions using logical operators Reference: www.netacad.com Programming with the Problem Analysis–Coding– Execution Cycle Programming is a process of problem solving One problem-solving technique: Analyze the problem Outline the problem requirements Design steps (algorithm) to solve the problem Algorithm: Step-by-step problem-solving process Solution achieved in finite amount of time C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3 Reference: www.netacad.com The Problem Analysis–Coding–Execution Cycle C++ Programming: From Problem Analysis to Program Design, Fifth Edition 4 Reference: www.netacad.com The Problem Analysis–Coding–Execution Cycle (cont'd.) Run code through compiler If compiler generates errors Look at code and remove errors Run code again through compiler If there are no syntax errors Compiler generates equivalent machine code Linker links machine code with system resources C++ Programming: From Problem Analysis to Program Design, Fifth Edition 5 Reference: www.netacad.com The Problem Analysis–Coding–Execution Cycle (cont'd.) Once compiled and linked, loader can place program into main memory for execution The final step is to execute the program Compiler guarantees that the program follows the rules of the language Does not guarantee that the program will run correctly C++ Programming: From Problem Analysis to Program Design, Fifth Edition 6 Reference: www.netacad.com 7 1.2 Your first program Preprocessor directives are commands supplied to the preprocessor All preprocessor commands begin with # A set of preliminary information that the compiler needs is included in header files such iostream header file. Reference: www.netacad.com 8 1.2 Your first program In C++ language, all elements of the standard C++ library are declared inside std namespace A namespace is an abstract container to hold a logical grouping of unique entities (blocks). Reference: www.netacad.com 9 1.2 Your first program C++ language assumes that a program must always have a function named main The function body begins with the first opening bracket { and ends with the corresponding closing bracket } Reference: www.netacad.com 10 1.2 Your first program Each instruction (statement) in C++ must end with a semicolon The cout entity (object) must be fed with something that.is intended to be shown on the screen.In our example, the feed is just text (string) Strings in the program in C++ are always enclosed in quotes Reference: www.netacad.com 11 1.2 Your first program The return statement causes the end of function execution The zero after the word return is how your program tells the operating system that the program performed successfully Reference: www.netacad.com 12 1.3, 1.4, 1.5 DATA TYPES, VARIABLES AND OPERATORS Reference: www.netacad.com 13 Comments Comments are for the reader, not the compiler To explain to other readers of the code how the tricks used in the code work To explain the means of variables and functions To document who the author is and when the program was written Whenever the compiler encounters a comment in your program, the compiler will skip it to the end of the comment. Two types: Single line // This is a C++ program. It prints the sentence: // Welcome to C++ Programming. Multiple line Reference: www.netacad.com Special Symbols Special symbols + ? - , * =< / =!. == ; => 14 Reference: www.netacad.com Reserved Words (Keywords) Reserved words, keywords, or word symbols Include: int float double char const void return C++ Programming: From Problem Analysis to Program Design, Fifth Edition 15 Reference: www.netacad.com 17 1.3 Variables Variables: are special “containers” used to store the results of C++ operations in order to use them in other operations As the name variables suggests, the content of a container can be varied Reference: www.netacad.com 1.3 Variables (identifiers) Consist of letters, digits, and the underscore character (_) Must begin with a letter or underscore C++ is case sensitive NUMBER is not the same as number Two predefined identifiers are cout and cin Unlike reserved words, predefined identifiers may be redefined, but it is not a good idea C++ Programming: From Problem Analysis to Program Design, Fifth Edition 18 Reference: www.netacad.com Legal variables (identifiers) in C++: first conversion payRate C++ Programming: From Problem Analysis to Program Design, Fifth Edition 19 Reference: www.netacad.com 20 C++ variables have: a name a type a value The variable exists as a result of a declaration A declaration is a syntactic structure that binds a name provided by the programmer with a specific type Type then variable name (or variable names separated by commas if there are more than one) Ends with a semicolon int Counter; int variable1, account_balance, invoices; Reference: www.netacad.com 21 The type is an attribute that uniquely defines which values can be stored inside the variable. To give a value to the newly declared variable, assignment operator is used. Examples: Counter = 1;  assign 1 to Counter or Counter becomes 1. Result = 100+ 200;  the new value of the variable Result will be the result of adding 100 to 200 X= X + 1;  Take the current value of the variable x, add 1 to it and store the result in the variable x. In effect, the value of variable x is incremented by one. Reference: www.netacad.com Data Types Data type: set of values together with a set of operations C++ data types fall into three categories: C++ Programming: From Problem Analysis to Program Design, Fifth Edition 22 Reference: www.netacad.com Simple Data Types Three categories of simple data Integral: integers (numbers without a decimal) Floating-point: decimal numbers Enumeration type: user-defined data type C++ Programming: From Problem Analysis to Program Design, Fifth Edition 23 Reference: www.netacad.com Simple Data Types (cont'd.) Integral data types are further classified into nine categories: char, short, int, long, bool unsigned char, unsigned short, unsigned int, unsigned long C++ Programming: From Problem Analysis to Program Design, Fifth Edition 24 Reference: www.netacad.com Simple Data Types (cont'd.) Different compilers may allow different ranges of values C++ Programming: From Problem Analysis to Program Design, Fifth Edition 25 Reference: www.netacad.com int Data Type Examples: -6728 0 78 +763 Positive integers do not need a + sign No commas are used within an integer Commas are used for separating items in a list C++ Programming: From Problem Analysis to Program Design, Fifth Edition 26 Reference: www.netacad.com bool Data Type bool type Two values: true and false Manipulate logical (Boolean) expressions true and false Logical values bool, true, and false Reserved words C++ Programming: From Problem Analysis to Program Design, Fifth Edition 27 Reference: www.netacad.com char Data Type Used for characters: letters, digits, and special symbols Each character is enclosed in single quotes 'A', 'a', '0', '*', '+', '$', '&' A blank space is a character Written ' ', with a space left between the single quotes The smallest integral data type. Computers store characters as numbers. Every character used by computers corresponds to a unique number, and vice versa C++ Programming: From Problem Analysis to Program Design, Fifth Edition 28 Reference: www.netacad.com 29 1.5 ASCII code A universal standard code implemented by (almost) all computers and operating systems all over the world ASCII (American Standard Code for Information Interchange) is the most widely used and nearly all modern devices (like computers, printers, mobile phones, tablets, etc.) use it. The code allows for 256 different characters. Reference: www.netacad.com 30 ASSCII Table the ASCII set is a UNICODE subset. UNICODE is able to represent virtually all characters used throughout the world. Reference: www.netacad.com 31 Char values are int values All of the following assignments are correct. Answers: 97, 97, 97, 65, 65, 65. Reference: www.netacad.com Floating-Point Data Types (cont'd.) float: represents any real number Range: -3.4E+38 to 3.4E+38 (four bytes) double: represents any real number Range: -1.7E+308 to 1.7E+308 (eight bytes) On most newer compilers, data types double and long double are same int i, z; float x, w; i = 10/4 ; //i =2 x = 10.0 / 4.0; //x=2.5 w = 10.0 / 4; //w= 2.5 z = 10.0 / 4; //z=2 C++ Programming: From Problem Analysis to Program Design, Fifth Edition 32 Reference: www.netacad.com Floating-Point Data Types (cont'd.) Maximum number of significant digits (decimal places) for float values is 6 or 7 Maximum number of significant digits for double is 15 Precision: maximum number of significant digits Float values are called single precision Double values are called double precision The scientific notation can be used to represent numbers that are very large or very small Examples : 300000000  3E8 6.62607 X 10-34  6.62607E-34 The exponent (the value after the “E”) must be an integer. The base (the value in front of the “E”) may or may not be an integer. C++ Programming: From Problem Analysis to Program Design, Fifth Edition 33 Reference: www.netacad.com 34 1.4 Floating-point numbers What happens when integer values converted into float values or vice versa? We can always transform from int into float. For example: int i ; float f; i=100; f=i; the value of the variable i will be 100. the value of the variable f is 100.0 Reference: www.netacad.com 35 1.4 Floating-point numbers We can always transform from float into int, but it can lead to a loss of accuracy. For example: int i ; float f; f=100.25 ; i=f; the value of the variable i will be 100 the value of the variable f will be 100.25 Reference: www.netacad.com string Type Programmer-defined type supplied in ANSI/ISO Standard C++ library Sequence of zero or more characters Enclosed in double quotation marks Null: a string with no characters Each character has relative position in string Position of first character is 0 Length of a string is number of characters in it Example: length of "William Jacob" is 13 ;string x x= “Hello World20 ?”; //15 characters C++ Programming: From Problem Analysis to Program Design, Fifth Edition 36

Use Quizgecko on...
Browser
Browser