Introduction to Programming - 3

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

Types and Variables. Computer Programming 2 C++ in one page!
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Data types and variables
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
String Escape Sequences
Variable & Constants. A variable is a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines.
Objectives You should be able to describe: Data Types
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
C-Language Keywords(C99)
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
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.
Chapter 2. Variable and Data type
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Introduction to C++. 2 What Is a Program Made Of?
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
28 Formatted Output.
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Variables.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
Chapter 2: Introduction to C++
Documentation Need to have documentation in all programs
Wel come.
Computing Fundamentals
Chapter 2: Introduction to C++
Chapter 2: Introduction to C++
C++ Basics.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Basics of ‘C’.
Dr. Khizar Hayat Associate Prof. of Computer Science
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
C++ Programming Basics
Dr. Khizar Hayat Associate Prof. of Computer Science
Chapter 2 Variables.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Programming Fundamental-1
Presentation transcript:

Introduction to Programming - 3 Dr. Khizar Hayat Associate Prof. of Computer Science

Scope of a variable All the variables that we intend to use in a program must be declared first with a valid identifier (name) and data type. A variable may either be global or local in scope. A global variable is declared outside all the functions of the program while a local variable is declared inside a block of code (e.g. function) where needed. A global variable is visible (known) from all parts of the program that come after it’s declaration, whereas a local variable is limited to only its delimiters {}, i.e. ‘{‘ just before it’s declaration and it’s corresponding ‘}’.

Scope of a variable: Two examples

Initialization of Variables When declaring a regular local variable, its value is undetermined by default You may want a variable to store a concrete value at the moment it is declared; this is called initialization. In C++, there are two ways to initialize a variable: C-like initialization by using the ‘=‘ sign, e.g. int sum = 0; Constructor initialization using parenthesis, e.g. int sum(0);

Constants Constants are expressions with a fixed value. Three types: Literals, e.g. in the statement a=5; 5 is the constant Defined constants (#define) #define PI 3.14 Declared constants (const) const int pathw=25; /* pathw like a normal variable but you cannot modify its value*/

Literal constants (using the const keyword) //program-area of circle #include<iostream> using namespace std; int main() { float r,area; cout<<"Enter the value for radius"<<endl; cin>>r; area=3.14*r*r; cout<<"Area of Circle is"<<area<<endl; return(0); }

Defined constants (#define) //program-area of circle #include<iostream> #define PI 3.14 using namespace std; int main() { float r,area; cout<<"Enter the value for radius"<<endl; cin>>r; area=PI*r*r; cout<<"Area of Circle is"<<area<<endl; return(0); }

Declared constants (using the const keyword) //program-area of circle #include<iostream> using namespace std; int main() { float r,area; const float pi=3.14; cout<<"Enter the value for radius"<<endl; cin>>r; area=pi*r*r; cout<<"Area of Circle is"<<area<<endl; return(0); }

Constants: Integer Literals An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal. An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.

Constants: Integer Literals

Constants: Floating-point Literals You can represent floating point literals either in decimal form or exponential form. A floating-point literal has an integer part, a decimal point, a fractional part, and/or an exponent part. The signed exponent is introduced by e or E.

Constants: Boolean Literals Data type bool Has just two valid values, true and false In the classic C, 1 meant true and 0 meant false Even with C++ the bool values, true and false, stands for small integer values of 1 and 0, respectively.

Constants: Character and String Literals Non-numerical constants, like: 'z’ // character constant 'p' //character constant "Hello world" // String constant "How do you do?“// String constant To represent a single character we enclose it between single quotes (') To express a string (generally consists of more than one character) we enclose it between double quotes ("). Escape sequence Meaning \\ \ character \' ' character \" " character \a Alert or bell \b Backspace \f Form feed \n Newline \r Carriage return \t Horizontal tab \v Vertical tab \o Octal number (follows) \x Hexadecimal number (follows)

Example to demonstate data types page 16 of notes #include<iostream> using namespace std; int main() { int a; long b; unsigned long c; short int d; a=45; b=189000; c=4294967295; d=12; cout<<“Memory size of integer is "<<sizeof(a)<<" bytes"<<endl; cout<<“The value of a is "<<a<<endl; cout<<“Memory size of long integer is "<<sizeof(b)<<" bytes"<<endl; cout<<“The value of b is "<<b<<endl; cout<<“Memory size of unsigned long integer is "<<sizeof(c)<<" bytes"<<endl; cout<<“The value of c is "<<c<<endl; cout<<“Memory size of short integer is "<<sizeof(d)<<" bytes"<<endl; cout<<“The value of d is "<<d<<endl; return(0); }

Example 2: The character literals #include<iostream> using namespace std; int main() { char letter; letter=‘A’; cout<<letter<<endl; letter=‘B’; cout<<“The size of character is "<<sizeof(letter)<<“ bytes"<<endl; return(0); }

Example: The character literals #include<iostream> using namespace std; int main() { char letter; int n; letter=48; cout<<letter<<endl; letter=97; n=‘9’; cout<<n<<endl; n=‘Z’; return(0); }

Example 3: Floating point literals #include<iostream> using namespace std; int main() { float distance; double mass; distance=1.495979E11f; mass=1.989E30; cout<<“The sun is "<<distance<<“ meters away\n”; cout<<“The sun’s mass is "<<mass<<“ kilograms\n”; cout<<“The size of float is "<<sizeof(distance)<<endl; cout<<“The size of double is "<<sizeof(mass)<<endl; cout<<“The size of long double is "<<sizeof(long double)<<endl; return(0); }

What happens to your C++ code?

Manipulators Operators used with the insertion operator << tomodify or manipulate the way of data display. Examples: endl for linefeed, i.e ‘\n’ or ‘\r’ setw(n) like an imaginary box with a certain width n - see examples on page 23-25 in notes setprecision(n) for the number of digits to be printed on the right of decimal point. Don’t forget (for the latter 2) #include<iomanip>

setw(n) #include<iostream> #include<iomanip> using namespace std; int main() { long int pop1=2425785, pop2=47, pop3=9761; cout<<“LOCATION "<<“POPULATION”<<endl; cout<<“Portcity "<<pop1<<endl; cout<<“Hightown "<<pop2<<endl; cout<<“Lowville "<<pop3<<endl; return(0); }

setw(n) #include<iostream> #include<iomanip> using namespace std; int main() { long int pop1=2425785, pop2=47, pop3=9761; cout<<“LOCATION "<<“POPULATION”<<endl; cout<<“Portcity "<<setw(12)<<pop1<<endl; cout<<“Hightown "<<setw(12)<<pop2<<endl; cout<<“Lowville "<<setw(12)<<pop3<<endl; return(0); }

setw(n) #include<iostream> #include<iomanip> using namespace std; int main() { long int pop1=2425785, pop2=47, pop3=9761; cout<<“LOCATION "<<“POPULATION”<<endl; cout<<setw(8)<<“Portcity "<<setw(12)<<pop1<<endl; cout<<setw(8)<<“Hightown "<<setw(12)<<pop2<<endl; cout<<setw(8)<<“Lowville "<<setw(12)<<pop3<<endl; return(0); }

setprecision(n) #include<iostream> #include<iomanip> using namespace std; int main() { double b=0.234009 cout<<setprecision(2)<<b<<endl; return(0); }