C++ Basics Variables, Identifiers, Assignments, Input/Output.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

Variables in C Amir Haider Lecturer.
Dale/Weems/Headington
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Basic Elements of C++ Chapter 2.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
KEAN UNIVERSITY Visual C++ Dr. K. Shahrabi. Developer studio Is a self-contain environment for creating, compiling, linking and testing windows program.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
C Tokens Identifiers Keywords Constants Operators Special symbols.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
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.
CPS120: Introduction to Computer Science
C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
CPS120: Introduction to Computer Science Variables and Constants.
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.
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
Variables, Identifiers, Assignments, Input/Output
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1.2 Introduction to C++ Programming
Data types Data types Basic types
BASIC ELEMENTS OF A COMPUTER PROGRAM
LESSON 3 IO, Variables and Operators
Documentation Need to have documentation in all programs
University of Central Florida COP 3330 Object Oriented Programming
Basics (Variables, Assignments, I/O)
CMSC 104, Section 4 Richard Chang
Reserved Words.
CMSC 104, Section 4 Richard Chang
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’.
Keywords.
Variables, Identifiers, Assignments, Input/Output
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Variables in C Declaring , Naming, and Using Variables.
Chapter 2: Introduction to C++.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Variables in C Topics Naming Variables Declaring Variables
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:

C++ Basics Variables, Identifiers, Assignments, Input/Output

Variables l variable can hold a number or a data of other types, it always holds something. A variable has a name l the data held in variable is called value l variables are implemented as memory locations and assigned certain memory address. The exact address depends on computer and compiler. l we think as though the memory locations are actually labeled with variable names 'c' y Temperature Letter Number

Identifiers l name of a variable (or any other item you define in program) is called identifier identifier must start with a letter or underscore symbol ( _ ), the rest of the characters should be letters, digits or underscores l the following are valid identifiers: x x1 x_1 _abc sum RateAveragE l the following are not legal identifiers. Why? 13 3X %change data-1 my.identifier a(3) l C++ is case sensitive: MyVar and myvar are different identifiers 3

What Are Good Identifiers? l careful selection of identifiers makes your program clearer l identifiers should be n short enough to be reasonable to type (single word is norm) –Standard abbreviations are fine (but only standard abbreviations) n long enough to be understandable l two styles of identifiers n C-style - terse, use abbreviations and underscores to separate the words, never use capital letters for variables n Pascal-style - if multiple words: capitalize, don’t use underscores –camel Case – variant of Pascal-style with first letter lowercased l pick style and use consistently l ex: Pascal-styleC-styleCamel Case Minminmin Temperaturetemperaturetemperature CameraAnglecamera_anglecameraAngle CurrentNumberPointscur_point_nmbr currentNumberPoints 4

Keywords l keywords are identifiers reserved as part of the language int, return, float, double n they cannot be used by the programmer to name things n they consist of lowercase letters only n they have special meaning to the compiler 5

Keywords (cont.) asm do if return typedef auto double inline short typeid bool dynamic_cast int signed typename break delete long sizeof union case else mutable static unsigned catch enum namespace static_cast using char explicit new struct virtual class extern operator switch void const false private template volatile const_cast float protected this wchar_t continue for public throw while default friend register true union delete goto reinterpret_cast try unsigned 6

Variable Declarations l every variable in C++ program needs to be declared l declaration tells the compiler (and eventually the computer) what kind of data is going to be stored in the variable l the kind of data stored in variable is called it’s type l a variable declaration specifies n type n name l declaration syntax: l two commonly used numeric types are: int - whole positive or negative numbers: 1,2, -1,0,-288, etc. double - positive or negative numbers with fractional part: 1.75, l example declarations: int numberOfBars; double weight, totalWeight; type id, id,..., id; known type list of one or more identifiers 7

Where to Declare l the variables should be declared as close to the place where they are used as possible. if the variable will be used in several unrelated locations, declare it at the beginning of the program: int main() {  right here l note that variable contains a value after it is declared. The value is usually arbitrary 8

Assignment l assignment statement is an order to the computer to set the value of the variable on the left hand side of the equation to what is written on the right hand side l it looks like a math equation, but it is not l Example: numberOfBars = 37; totalWeight = oneWeight; totalWeight = oneWeight * numberOfBars; numberOfBars = numberOfBars + 3; var = value; 9

Output To do input/output, at the beginning of your program you have to insert #include using std::cout; using std::endl; l C++ uses streams for input an output l stream - is a sequence of data to be read (input stream) or a sequence of data generated by the program to be output (output stream) variable values as well as strings of text can be output to the screen using cout (console output): cout << numberOfBars; cout << ”candy bars”; cout << endl; << is called insertion operator, it inserts data into the output stream, anything within double quotes will be output literally (without changes) - ”candy bars taste good” note the space before letter “ c ” - the computer does not insert space on its own keyword endl tells the computer to start the output from the next lin e 10

More Output n the data in output can be stacked together: cout << numberOf_Bars << ”candy bars\n” symbol \n at the end of the string serves the same purpose as endl n arithmetic expressions can be used with the output statement: cout << “The total cost is $” << (price + tax); 11

Escape Sequences n certain sequences of symbols make special meaning to the computer. They are called escape sequences escape sequence starts with a backslash ( \ ). It is actually just one special character. n Useful escape sequences: –new-line \n –horizontal tab \t –alert \a –backslash \\ –double quote \” n What does this statement print? cout << ”\” this is a \t very cryptic \” statement \\ \n”; 12

Input cin - (stands for Console INput) - is used to fill the values of variables with the input from the user of the program to use it, you need to add the following to the beginning of your program using std::cin; l when the program reaches the input statement it just pauses until the user types something and presses key l therefore it is beneficial to precede the input statement with some explanatory output called prompt: cout << “Enter the number of candy bars cout << “and weight in ounces.\n”; cout << “then press return\n”; cin >> numberOfBars >> oneWeight; >> is extraction operator l dialog – collection of program prompts and user responses l note how input statements (similar to output statements) can be stacked l input tokens (numbers in our example) should be separated by (any amount of) whitespace (spaces, tabs, newlines) l the values typed are inserted into variables when is pressed, if more values needed - program waits, if extra typed - they are used in next input statements if needed 13