1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.

Slides:



Advertisements
Similar presentations
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Advertisements

Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Types and Variables. Computer Programming 2 C++ in one page!
General Computer Science for Engineers CISC 106 Lecture 28 Dr. John Cavazos Computer and Information Sciences 04/29/2009.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
CS150 Introduction to Computer Science 1
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
CS2311 Computer Programming Dr. Yang, Qingxiong (with slides borrowed from Dr. Xu, Henry) Lecture 2: Basic Syntax – Part I: Variables and Constants.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
CS346 Javascript -3 Module 3 JavaScript Variables.
 Building blocks of a C++ program  Each function has a name, which is used to call the function; functions call each other  You will write your own.
Chapter 3 The Basic Data Types C/C++ Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Character Sequences. strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
A Sample Program #include using namespace std; int main(void) { cout
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
ECE Application Programming
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Lecture 8b: Strings BJ Furman 15OCT2012.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Pointers, Dynamic Data, and Reference Types
Local Variables, Global Variables and Variable Scope
CS150 Introduction to Computer Science 1
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
CS31 Discussion 1D Winter19: week 4
C Programming Lecture-17 Storage Classes
The Fundamentals of C++
Presentation transcript:

1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail

2 Constants Constants are specific values of any data type, such as ‘f’“hello” Character constants are enclosed in single quotes char ch = ‘z’; String constants are enclosed in double quotes string message = “Hello”;

3 Types of Constants Character constants char c = ‘z’ Numeric constants int a = 10; Hexadecimal constants int hex = 0xFF; Octal constants int oct = 011; String constants char greetings = “Hello”; Backslash constants \b, \f, \n, \r, \t, \”, \’, \0, \\, \v, \a, \?, \OCTAL, \xHEX

4 #include int main() { char ch = ‘M’; //assign ASCII code for M to c int number = ch; //store same code in an int cout << “The ASCII code for “ << ch << “ is ” << in << ‘\n’; cout << “Add one to the character code\n”; ch = ch + 1; in = ch; cout << “The ASCII code for “ << ch << “ is “ << in << endl; return 0; }

5 Variables Needed to store information Program must remember three properties: where, what value, what kind int age; age = 40; double radius = 0.0; Have to be declared first; why? Uninitialized variables have garbage values C++ is case-sensitive

6 Scope of Variables Local Global Block Function File Program Class ?

7 Local Variables Declared inside a function Die when function finishes; unknown outside their function Initialized each time the function containing them is entered; uninitialized have garbage values

8 #include void func() { int x; // local to func() x = -199; cout << x; // displays ? } int main() { int x; // local to main() x = 10; func(); cout << "\n"; cout << x; // displays ? return 0; }

9 Global Variables Declared outside any function; have life as long as the program runs Can be used by all following functions Usually placed at the beginning of program Initialized only at the start of the program; uninitialized default to zero An identically named local variable masks global one Should be avoided if possible

10 #include int i = 2;//global void func() { cout << i << endl; int i = 3;//local cout << i << endl; } int main() { cout << i << endl; func(); cout << i << endl; int i = 5; cout << i << endl; return 0; }