Basics (Variables, Assignments, I/O)

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Structure of a C program
1 Chapter 2 C++ Basics. 2 Overview Variables, Constants and Assignments (2.1) Input and Output (2.2) Data Types and Expressions (2.3) Simple Flow of Control.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 C++ Basics.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
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.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
Chapter # 2 Part 2 Programs And data
C++ Basics Lecture 2.
Variables, Identifiers, Assignments, Input/Output
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 2 C++ Basics. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow.
Chapter 2 C++ Basics.
Data Types and Expressions
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
LESSON 3 IO, Variables and Operators
Basic Elements of C++.
Basics (Variables, Assignments, I/O)
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
CMSC 104, Section 4 Richard Chang
Basic Elements of C++ Chapter 2.
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Basics (Variables, Assignments, I/O)
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
Govt. Polytechnic,Dhangar
Variables, Identifiers, Assignments, Input/Output
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
Programming Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Programming Language C Language.
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Basics (Variables, Assignments, I/O) Chapter 2.1-2.2 Basics (Variables, Assignments, I/O)

Overview 2.1 Variables and Assignments 2.2 Input and Output Slide 2- 2

2.1 Variables and Assignments 3 Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3

Variables and Assignments Variables are names places where we can store a value: If we place another value in a variable, the previous value is gone: Slide 2- 4 4

Variables

Variable names: Identifiers Variables names are called identifiers Choosing variable names is important! Use meaningful names that represent data tobe stored First character must be a letter the underscore character Remaining characters must be letters numbers underscore character Slide 2- 6 6

Variable names: Identifiers Variables names are called identifiers Choosing variable names is important! be stored Bad nxt cliN lb xc Good nextTempSample clientFirstName LoanBalance xCoordinate Slide 2- 7 7

Identifier Name Rules A valid identifier is a sequence of one or more letters, digits, or underscore characters (_). Spaces, punctuation marks, and symbols cannot be part of an identifier. In addition, identifiers shall always begin with a letter. Which are okay: temp4 4temp Today&forever Now,or,never lessonsILearnedFromCalculus

Keywords Keywords (also called reserved words) Are used by the C++ language the programming language Cannot be used as identifiers Auto const double float int short struct Unsigned break continue else for long Signed switch void case default enum goto register sizeof typedef volatile char do extern if return static union while Slide 2- 9 9

int i, j, k; char c, ch; float f, salary; double d; Declaring Variables (Part 1) Before use, variables must be declared Tells the compiler the type of data to store, and the variable name A type followed by a list of variables names 23, -5, 17, 20043 int i, j, k; char c, ch; float f, salary; double d; types names 'd', 'A', '&', '(' 3.45, -22.222 3.45, -22.222, Much larger . 10

Declaring Variables (Part 2) Immediately prior to use Generally is is considered good form to declare variables as close as possible to first use. Two locations for variable declarations int main() { … int sum; sum = score1 + score 2; return 0; } int main() { int sum; … sum = score1 + score2; return 0; } At the beginning Right before used Slide 2- 11 11

Variable being assigned Assignment Statements Direction An assignment statement changes the value of a variable On the right of the assignment operator can be Literals -- age = 21; Variables -- my_cost = your_cost; Expressions -- circumference = diameter * 3.14159; total_weight = one_weight + number_of_bars; Assignment operator Variable being assigned Source of value Slide 2- 12 12

Assignment Statements and Algebra The ‘=‘ operator in C++ is not an equal sign The following statement cannot be true in algebra is the previous value of number_of_bars plus 3 = is an operation, not a comparison number_of_bars = number_of_bars + 3; Slide 2- 13 13

Initializing Variables Declaring a variable does not give it a value Before a variable is given a value is value is unknown, and thus garbage! Giving a variable its first value is initializing the variable Declaration and initialization can be combined using two methods Define Initialize Comment mpg = 26.3; // initialize the variable double mpg = 26.3, area = 0.0 , volume; double mpg(26.3), area(0.0), volume; Slide 2- 14 14

Section 2.1 Conclusion Can you The variables are named feet and inches. Both should be initialized to the appropriate form of 5. Give good variable names for identifiers to store the speed of an automobile? an hourly pay rate? the highest score on an exam? Slide 2- 15 15

Variables Hands on Discussion: What did you get? Why? int i=13; float x=1.5; x = (x * i) + 8; i = (x * i) + 8; cout << “x is equal to:” << x << “\n”; cout << “i is equal to:” << i << “\n”;

2.2 Input and Output Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 17

Input and Output A data stream is a sequence of data Typically in the form of characters or numbers An input stream is data for the program to use Typically originates at the keyboard at a file An output stream is the program’s output Destination is typically the monitor a file Slide 2- 18 18

Output using cout Example: This line sends two items to the monitor The value of number_of_bars The quoted string of characters " candy bars\n" Notice the space before the ‘c’ in candy The ‘\n’ causes a new line to be started following the ‘s’ in bars cout << number_of_bars << " candy bars\n"; Slide 2- 19 19

Examples Using cout cout << " candy bars\n"; cout << "Total cost is $" << (price + tax); Quoted strings are enclosed in double quotes ("Walter") Don’t use two single quotes (') in " candy bars\n" Slide 2- 20 20

Include Directives Include Directives add library files to our programs #include <iostream> Using Directives include a collection of defined names using namespace std; Slide 2- 21 21

Escape Sequences Escape sequences tell the compiler to treat characters in a special way '\' is the escape character cout << endl; \" -- a quote character Slide 2- 22 22

Formatting Real Numbers cout << "The price is $" << price << endl; The price is $7.850000e01 The price is $78.50 Slide 2- 23 23

Showing Decimal Places cout includes tools to specify the output of type double To specify fixed point notation setf(ios::fixed) To specify that the decimal point will always be shown setf(ios::showpoint) To specify that two decimal places will always be shown precision(2) << price << endl; Slide 2- 24 24

Input Using cin cin is an input stream bringing data from the keyboard The extraction operator (>>) removes data to be used cin >> one_weight; This code prompts the user to enter data then reads two data items from cin The first value read is stored in number_of_bars The second value read is stored in one_weight Data is separated by spaces when entered Slide 2- 25 25

Reading Data From cin Multiple data items are separated by spaces Data is not read until the enter key is pressed Allows user to make corrections cin >> v1 >> v2 >> v3; Requires three space separated values to be entered 34 45 12 <enter key> Slide 2- 26 26

Designing Input and Output Prompt the user for input that is desired cin >> age; Notice the absence of a new line before using cin Echo the input by displaying what was read cout << age << " was entered." << endl; Slide 2- 27 27

Section 2.2 Conclusion Can you write an input statement to place a value in the variable the_number? Write the output statement to prompt for the value to store in the_number? Write an output statement that produces a newline? Format output of rational numbers to show 4 decimal places? Slide 2- 28 28