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

C++ Basics Variables, Identifiers, Assignments, Input/Output.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
Input & Output: Console
C Tokens Identifiers Keywords Constants Operators Special symbols.
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.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
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.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
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
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.
Bill Tucker Austin Community College COSC 1315
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
C++ Basics Lecture 2.
Variables, Identifiers, Assignments, Input/Output
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 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
Basic Elements of C++.
University of Central Florida COP 3330 Object Oriented Programming
Compiler Construction
Basics (Variables, Assignments, I/O)
Reserved Words.
Basic Elements of C++ Chapter 2.
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,
C++ Basics.
Introduction to C++ Programming
درس برنامه‌سازي کامپيوتر
Variables in C Topics Naming Variables Declaring Variables
Basics of ‘C’.
Keywords.
Govt. Polytechnic,Dhangar
Variables in C Topics Naming Variables Declaring Variables
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
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
Introduction to C++ Programming
Chapter 2: Introduction to C++.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Introduction to C++.
Programming Language C Language.
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
The Fundamentals of C++
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

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

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

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

Identifier Style careful selection of identifiers makes your program clearer identifiers should be short enough to be reasonable to type (single word is norm) standard abbreviations are acceptable long enough to be understandable two styles of identifiers C-style - terse, use abbreviations and underscores to separate the words, never use capital letters for variables Camel Case - if multiple words: capitalize, do not use underscores variant: first letter lowercased pick style and use consistently ex: Camel Case 1 C-style Camel Case 2 Min min min Temperature temperature temperature CameraAngle camera_angle cameraAngle CurrentNumberPoints cur_point_nmbr currentNumberPoints

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

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

Variable Declarations every variable in C++ program needs to be declared type – the kind of data stored in a variable declaration – introduces a variable to a compiler. variable has to be declared before use a variable declaration specifies type name declaration syntax: 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, -0.55 example declarations: int numberOfBars; double weight, totalWeight; list of one or more identifiers type id, id, ..., id ;

Declaration Location, Initial Value a variable should be declared as close to its use as possible variable contains a value after it is declared until assigned, this value is arbitrary

Assignment variable = value; assignment statement is an order to the computer to set the value of the variable on the left hand side equal sign to what is written on the right hand side it looks like a math equation, but it is not example: numberOfBars = 37; totalWeight = oneWeight; totalWeight = oneWeight * numberOfBars; numberOfBars = numberOfBars + 3; Alt+8 to dsiplay in MSVS executable code

Output to do input/output, at the beginning of your program insert #include <iostream> using std::cout; using std::endl; C++ uses streams for input an output stream – 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 line

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

Input cin - (stands for Console INput) – stream used to give variables user-input values need to add the following to the beginning of your program using std::cin; when the program reaches the input statement it just pauses until the user types something and presses <Enter> key 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 dialog – collection of program prompts and user responses input operator (similar to output operator) can be stacked input token – sequence of characters separated by white space (spaces, tabs, newlines) the values typed are inserted into variables when <Enter> is pressed, if more values needed - program waits, if extra typed - they are used in next input statements if needed