How Create a C++ Program. #include using namespace std; void main() { cout<<“Hello World”; } If your compiler gives error then.

Slides:



Advertisements
Similar presentations
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Advertisements

1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Chapter 2: Basic Elements of C++
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
// A simple C++ program # include using namespace std; int main () { cout
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Chapter 2: Basic Elements of C++
Arash Rafiey TA : Xu Cheng Office Hours M-W 10:30 – 11:30.
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
Chapter 2 types, variables, methods Pages Horstmann.
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Data types Fundamental data types –Integer, floating point, character Derived data types –Arrays –Strings –Structures.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Introduction to Programming
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
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++
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
A Sample Program #include using namespace std; int main(void) { cout
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Lesson 1.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Documentation Need to have documentation in all programs
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Basic Elements of C++.
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Primitive Types and Expressions
Use a variable to store a value that you want to use at a later time
Presentation transcript:

How Create a C++ Program

#include using namespace std; void main() { cout<<“Hello World”; } If your compiler gives error then

#include void main() { cout<<“Hello World”; cout<<endl; // for going to new line cout<<“ good bye”; } cout is an object for printing out some thing on the screen.

#include void main() { cout<<“Hello World \n”; cout<<“ good bye”; } we can use \n instead of cout<<endl;

How to run your program Using Linux or unix: g++ myFile.cpp./a.out g++ myFile.cpp myFile.out./myFile.out

Declare Statement & Variable Each variable must be declare before use Each variable has a type: For example int, char, float. int for Integer type; char for character like ‘A’; float for real number;

Example int LuckyNumber=17; float RealNumber; char a=‘A’;

Identifiers Identifier: name of a variable, function, or class Rules for identifiers in C++: 1 Can be made up of letters, digits, and the underscore (_) character 2 Cannot start with a digit 3 Cannot use other symbols such as ? or % 4 Spaces are not permitted inside identifiers 5 You cannot use reserved words 6 They are case sensitive

Self Check 1. What is the type of the values 0 and ‘0’? 2. Which of the following are legal identifiers? Greeting1 g void 101dalmatians Hello, World

Answer int and char Only the first two are legal identifiers

Syntax : Variable Definition typeName variableName = value; or typeName variableName; Example : int number = 12; Purpose: To define a new variable of a particular type and optionally supply an initial value

The Assignment Operator Assignment operator: = Not used as a statement about equality Used to change the value of a variable int number1; int number2, number3; number1=number2=number3=88;

number2=number2-1; number3=number2+1;

How to read a variable #include using namespace std; void main() { int number; cout<<“please enter a number \n”; cin>>number; number=number+1; cout<<“the number is “<<number; }

Integer Types The short, int and long Integer Types A short integer is at least 16 bits An int integer is at least as big as short A long integer is at least 32 bits and at least as big as int.

E.g. A 16-bit int might run from to The sizeof operator returns the size (in bytes)

#include int main() { int n_int = INT_MAX; short n_short = SHRT_MAX; long n_long = LONG_MAX; cout << “int is “ << sizeof (int) << “ bytes” << endl; cout << “short: “ << n_short << endl; cout << “long: “ << n_long << endl; return 0; }

int is 4 bytes Maximum values: –Short: –Long:

Characters and small integers #include int main() { char ch = ‘M’; // assign ASCII code int a = ch; cout << “ASCII code: “ << ch << “ is “ << a << endl; ch = ch + 1; a = ch; cout << “ASCII code: “ << ch << “ is “ << a << endl; return 0; }

Output M is 77 N is 78

Boolean type bool isReady = true; int ans = true; // ans assigned 1 int promise = false; // promise assigned 0 bool start = -100; // true bool stop = 0; // false

Floating-point number E.g We have float, double, long double

Arithmetic operators Summation: + Multiplication: * Division: / Subtraction: -

Operator Precedence int number = * 5 // 35 or 23? float logs = 120 / 4 * 5 // 150 or 6??

Type Casting Conversion between types: (typeName) value // c typeName (value) // c++ e.g. – cout << int(‘A’) << endl; // 65 – float one, –int two; one = ; two = (int) (int) 2.1;