LESSON 2 Basic of C++.

Slides:



Advertisements
Similar presentations
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Advertisements

Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Chapter 2: Basic Elements of C++
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Introduction to C++ Programming
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Input & Output: Console
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.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
C++ Programming: Basic Elements of C++.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
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.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
LESSON 2 Basic of C++.
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
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
Bill Tucker Austin Community College COSC 1315
C++ Lesson 1.
C++ First Steps.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++ Chapter 1.
Computer Programming BCT 1113
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Computing Fundamentals
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Introduction to C++ October 2, 2017.
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2: Introduction to C++
2.1 Parts of a C++ Program.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Programming Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Reading from and Writing to Files
C++ Programming Basics
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files
Programming Fundamental-1
Presentation transcript:

LESSON 2 Basic of C++

Writing the first program //my first program in C++ #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Hello World!”; getch(); return 0; } Hello World!

Structure of C++ Program hash sign #include <iostream> #include <conio.h> using namespace std; int main() { }  Preprocessor directives  Namespace library  Main function header brace  Open the block  Write declarations and statements  Close the block brace

Writing the first program Source Code //my first program in C++ #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Hello World!”; getch(); return 0; }

Writing the second program //my second program in C++ #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Hello World!”; cout<< “I’m a C++ program”; getch(); return 0; } Hello World! I’m a C++ program Hello World!

Comments Text that are inserted into the source code to explain what the code is doing. Two types of comments: Line comment // Block comment /* */

Comments Example –1 #include<iostream> using namespace std; int main() { cout <<"I play Football"; // Abdullah // ID=3333 return 0; } Example -2 /* Abdullah ID =3333 */

Program with Comment //my first program in C++ #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Multi-line comment”; /*This line is part of the block or multi-line comment. This line is also part of the block comment */ cout<< “Line comment 1”; //This line comment takes up an entire line. cout<< “Line comment 2”; getch(); return 0; } Multi-line comment Line Comment1 Line comment2 Multi-line comment Multi-line comment Line Comment1

endl Command #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Hello”; cout<< “How are you.”; cout<< “What is your name?”; getch(); return 0; } Hello How are you. What is your name? Hello Hello How are you.

endl Command #include<iostream> #include<conio.h> using namespace std; int main() { cout<< “Hello” << endl; cout<< “How are you.” << endl; cout<< “What is your name?” << endl; getch(); return 0; } Hello How are you. What is your name? Hello Hello How are you.

Variables Simply a name chosen by the programmer that is used to refer computer storage location.

Variables Variables may consist of letters, digits and underscore ( _ ) character with the following condition: They must begin either with a letter or an underscore. Uppercase and lowercase are significant Space is not allowed Keywords are not allowed as variable names

List of C++ Keywords

Valid/Invalid Variable Names John int Name const char NAME s_name first_name _name first-name 1man first name @name v.a.l.u.e

Data Types Specific category of information that a variable contains. Type Category Description Keyword Integer Positive or negative number with no decimal int Floating Point Numbers containing decimal places float double long double Character Text char Boolean True(1) or False(0) bool

Declaration Statement Naming a variable and specifying the data type. int float double long double char bool type name; user defined name based on the naming RULES.

Declaration Statement - Example int sum; char gender; double studentgpa; int a,b,c; int a; int b; int c; int num = 15; float grade1=81.0;

Literals int float char string bool -259 -13 200 -6.16 -4.4 2.7541 200 float -6.16 -4.4 2.7541 10.5 char ‘z’ ‘p’ ‘x’ ‘a’ string “Hello World” “How do you do?” “Ali” “Diploma 1” bool true false

Operating with variables #include <iostream> #include <conio.h> using namespace std; int main() { int a,b; int result; a=5; b=2; a=a+1; result = a–b; cout <<“Result equals to”<<result; getch(); return 0; } a 6 a 5 a 2 b b result 4 result Result equals to 4

Constants Literal Symbolic Memory Expression with a fixed value. Ways to use constants: Literal Symbolic Memory float pi=3.1416; #define pi 3.1416; const double pi=3.1416;

Operating with const #include <iostream> raduis=20; #include <conio.h> using namespace std; int main() { const float PI=3.1416; float radius=5; float area; area=radius*radius*PI; cout<<“The area is”<<area <<“with a radius of 5.\n”; raduis=20; area=radius*radius*PI; cout <<“The area is ”<<area <<“with a radius of 20.\n”; getch(); return 0; } The area is 78.54 with the radius of 5. The area is 1256.64 with the radius of 20. The area is 78.54 with the radius of 5.

Operating with const #include <iostream> raduis=20; #include <conio.h> #define PI 3.1416; using namespace std; int main() { float radius=5; float area; area=radius*radius*PI; cout<<“The area is”<<area <<“with a radius of 5.\n”; raduis=20; area=radius*radius*PI; cout <<“The area is ”<<area <<“with a radius of 20.\n”; getch(); return 0; } The area is 78.54 with the radius of 5. The area is 1256.64 with the radius of 20. The area is 78.54 with the radius of 5.

cout is output command that sends data given to it to the standard output device.

Operating with cout #include <iostream> #include <conio.h> using namespace std; int main() { int a,b; float x,y; a=10; b=20; x=43.2892; y = 5597.00; cout <<“a=”<<a <<“, b=”<<b <<“, x=”<<x <<“, y=”<<y; getch(); return 0; } 10 a a 20 b b x 43.2892 x y 5597.00 y A=10, b=20, x=43.2892, y=5597.00

cin Command reads in information from the keyboard. int age; cin >> age; float salary; cin >> salary;

Operating with cin #include <iostream> #include <conio.h> using namespace std; int main() { int number; cout<<“Enter number: ” cin>> number; cout>>”You entered ” number; getch(); return 0; } 27 number number Enter number: 27 You have entered 27 Enter number: Enter number: 27

Backup Slides

Comments Line comment Sample 1 Sample 2 cout << “Hello world!” <<endl; //Everything from here to the right is ignored Sample 2 cout << “Hello world!” <<endl; //cout and endl live in the iostream library cout << “It is nice to meet you” <<endl; //this code is hard to read cout << “Yeah!” <<endl; //specially when lines are different length

Comments Sample 3 //cout and endl live in the iostream library cout << “Hello world!” <<endl; //this code is easir to read cout << “It is nice to meet you” <<endl; //don’t you think so? cout << “Yeah!” <<endl;

Comments Multi-line Comment Sample 1 Sample 2 /* This is a multi-line comment This line will be ignored. so will this one. */ Sample 2 /* This is a multi-line comment. * the matching asterisks to the left * can make this easier to read */