Download presentation
Presentation is loading. Please wait.
Published byAmie Holmes Modified over 9 years ago
1
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
2
Objectives Visual C++ Programming 2 Develop algorithms to solve a problems Learn about the standard C++ data types Declare and initialize variables Read data using the TryParse() method Use the standard C++ arithmetic operators
3
Objectives (continued) Visual C++ Programming 3 Process data using arithmetic expressions Abbreviate lines of code using shorthand assignment operators Use the Math::Pow() and Math::Sqrt() methods Display output using the ToString() method
4
Solving Problems Visual C++ Programming 4 An algorithm is an ordered list of steps required to solve a problem Algorithms are written in pseudocode Algorithms are language independent High-level algorithms provide general list of tasks Low-level algorithms provide specific details
5
Solving Problems (continued) Visual C++ Programming 5
6
Data and Data Types Visual C++ Programming 6 Data includes everything entered into your program (numbers and characters for example) Data is represented using binary digits called bits A grouping of eight bits is called a byte Different kinds of data are called data types
7
Visual C++ Programming 7
8
8
9
Data and Data Types (continued) Visual C++ Programming 9 Primitive data types Boolean ( bool ) used to represent true or false Character ( char ) represents a single character ASCII codes represent char data in one byte Integer ( int ) includes positive and negative whole numbers and zero Real numbers ( float and double ) includes all values that may have decimal places float – used to represent single precision numbers double – represents double-precision numbers
10
Data and Data Types (continued) Visual C++ Programming 10
11
Data and Data Types (continued) Visual C++ Programming 11 Derived data types Built on primitive types The String data type is used to represent text strings Text strings Example: “Hello world!”
12
Variables Visual C++ Programming 12 A variable is a named location in memory that stores data Variables are created by a process called “declaration” When a variable is declared One or more memory cells are allocated in memory The allocated space is assigned a name The allocated space is associated with a data type
13
Variables (continued) Visual C++ Programming 13
14
Variables (continued) Visual C++ Programming 14
15
Visual C++ Programming 15
16
Variables (Continued) Visual C++ Programming 16
17
Variables (Continued) Visual C++ Programming 17 Variable naming conventions Names must begin with a letter or an underscore (_) and can only use letters, digits or underscores in the body of the name Names are case sensitive ( height is not the same name as Height ) Spaces cannot be used as separators
18
Variables (continued) Visual C++ Programming 18
19
Variables (continued) Visual C++ Programming 19
20
Initializing Variables Visual C++ Programming 20 Initialization refers to the act of assigning a value to a variable before it is used By default, Visual C++ initializes all numeric variables to 0 The programmer can initialize variables using assignment statements
21
Initializing Variables (continued) Visual C++ Programming 21
22
Initializing Variables (continued) Visual C++ Programming 22
23
Initializing Variables (continued) Visual C++ Programming 23
24
Data Input Visual C++ Programming 24 Data input refers to the process of reading data into a variable from the program interface Data is often read in from a TextBox
25
Data Input (continued) Visual C++ Programming 25
26
The TryParse() Method Visual C++ Programming 26 Parsing is the process of extracting data from a string of text The TryParse() method parses the Text contained in a TextBox Every data type class has a built-in TryParse() method Scope refers to the class or program segment to which something belongs The scope resolution operator ( :: ) identifies the data type class that TryParse() belongs to
27
The TryParse() Method (continued) Visual C++ Programming 27
28
The TryParse() Method (continued) Visual C++ Programming 28 Parameters are items of information that a method requires A parameter list is a groups of parameters separated by commas
29
The TryParse() Method (continued) Visual C++ Programming 29
30
Data Processing Visual C++ Programming 30 Computations often involve arithmetic expressions An arithmetic operator is a symbol that stands for an arithmetic operation Arithmetic operations are Multiplication (*) Division (/) Mod (%) Addition (+) Subtraction (-)
31
Arithmetic Operators Visual C++ Programming 31 Operands are the variables or values that arithmetic operations are performed on Arithmetic operators are binary (require two operands) Arithmetic operations are performed from left to right (left to right associative) Examples: num = num1 + num2; num = num + 1;
32
Visual C++ Programming 32
33
Visual C++ Programming 33
34
Operator Precedence in Arithmetic Expressions Visual C++ Programming 34 Complex arithmetic expressions have more than one arithmetic operator. Operator precedence rules dictate which operations are performed first Multiplication (*), division (/) and mod (%) have higher precedence than addition (+) and subtraction (-) When two or more operators of the same precedence are in an expression they are performed from left to right
35
Operator Precedence in Arithmetic Expressions (continued) Visual C++ Programming 35 Parentheses have higher precedence than all arithmetic operators Parentheses can be used to perform lower precedence operations before higher ones Expression trees are used to illustrate the order in which operations are completed when a complex expression is evaluated
36
Visual C++ Programming 36
37
Visual C++ Programming 37
38
Visual C++ Programming 38
39
Visual C++ Programming 39
40
Arithmetic Operators and Strings Visual C++ Programming 40 The addition operator (+) is used to add numeric data When the operator (+) is used with strings it joins them together (concatenation) Example: textBox3->Text = textBox1->Text + textBox2->Text;
41
Visual C++ Programming 41
42
Shorthand Assignment Visual C++ Programming 42 Shorthand operators are abbreviations for assignment statements involving arithmetic operations Shorthand operators: +=, /=, %=, +=, -= Example: num1 = num1 + num2; is num1 += num2;
43
The Math Library Visual C++ Programming 43 The System Math Class contains methods that perform commonly-used tasks Example: Math::Pow(2,3) Math::Sqrt(45) c = Math::Sqrt(Math::Pow(a,2) + Math:Pow(b,2)); Common Math methods: Exponentiation, Math::Pow() Square Root, Math::Sqrt() Cosine, Math::Cos() Sine, Math::Sin() Tangent, Math::Tan()
44
Visual C++ Programming 44
45
Data Output Visual C++ Programming 45 To display a value in the Text property of a TextBox it must be converted to a String The ToString() method Example: textBox3->Text = sum.ToString();
46
Visual C++ Programming 46
47
Summary Visual C++ Programming 47 Solving problems requires algorithms Step-by-step lists of instructions Commonly used data types are Bool, char, int, float and double The String data type stores character strings Data is stored in variables Variable declarations allocate, name and assign a data type to memory cells Assignment statements are a common way to initialize variables
48
Summary (continued) Visual C++ Programming 48 Data Input Captures the contents of a TextBox and stores the result in a variable TryParse() Data Processing Common arithmetic operations (*,/,%,+,-) Precedence rules (*,/ and % before +,-) Expression trees are used to evaluate arithmetic expressions
49
Summary (continued) Visual C++ Programming 49 Data Output Transferring data from a variable to a TextBox Use the ToString() method
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.