Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.

Slides:



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

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.
C++ Basics Part I Part II. Slide 2 Part I: basics.
C++ Basics. COMP104 C++ Basics / Slide 2 Introduction to C++ * C++ is a programming language for manipulating numbers and user-defined objects. * C++
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Chapter 2: Introduction to 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.
Programming Introduction to C++.
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
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
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.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Course websites CS201 page link at my website: Lecture slides Assistant’s Information Recitations Office Hours Make-up.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Introduction to C++ Programming COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 2 Introduction to C++ l C is a programming language developed.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
CSC 107 – Programming For Science. Announcements.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
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.
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++
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
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
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
Chapter 2: Introduction to C++
Computing and Statistical Data Analysis Lecture 2
Computing Fundamentals
Programming Introduction to C++.
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Chapter 2: Introduction to C++
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,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables in C Topics Naming Variables Declaring Variables
Variables, Identifiers, Assignments, Input/Output
Introduction to C++ Programming
Chapter 2: Introduction to C++.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Chap 2. Identifiers, Keywords, and Types
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Introduction to C++

// Program description #include directives int main() { constant declarations variable declarations executable statements return 0; }

Keywords appear in blue in Visual C++. Each keyword has a predefined purpose in the language. Do not use keywords as variable and constant names!! The complete list of keywords is on page 673 of the textbook. We shall cover the following keywords in this class: bool, break, case, char, const, continue, do, default, double, else, extern, false, float, for, if, int, long, namespace, return, short, static, struct, switch, typedef, true, unsigned, void, while

The Corresponding C++ Program #include using namespace std; int main() { int first, second, sum; cout << "Peter: Hey Frank, I just learned how to add” << “ two numbers together."<< endl; cout << "Frank: Cool!" <<endl; cout << "Peter: Give me the first number."<< endl; cout << "Frank: "; cin >> first; cout << "Peter: Give me the second number."<< endl; cout << "Frank: "; cin >> second; sum = first + second; cout << "Peter: OK, here is the answer:"; cout << sum << endl; cout << "Frank: Wow! You are amazing!" << endl; return 0; }

Identifiers appear in black in Visual C++. – An identifier is a name for a variable, constant, function, etc. – It consists of a letter followed by any sequence of letters, digits, and underscores. – Examples of valid identifiers: First_name, age, y2000, y2k – Examples of invalid identifiers: 2000y – Identifiers cannot have special characters in them. For example: X=Y, J-20, ~Ricky,*Michael are invalid identifiers. – Identifiers are case-sensitive. For example: Hello, hello, WHOAMI, WhoAmI, whoami are unique identifiers.

Comments appear in green in Visual C++. Comments are explanatory notes; they are ignored by the compiler. There are two ways to include comments in a program: // A double slash marks the start of a //single line comment. /* A slash followed by an asterisk marks the start of a multiple line comment. It ends with an asterisk followed by a slash. */

Compiler directives appear in blue in Visual C++. The #include directive tells the compiler to include some already existing C++ code in your program. The included file is then linked with the program. There are two forms of #include statements: #include //for pre-defined files #include "my_lib.h" //for user-defined files

C++ is a free-format language, which means that: Extra blanks (spaces) or tabs before or after identifiers/operators are ignored. Blank lines are ignored by the compiler just like comments. Code can be indented in any way. There can be more than one statement on a single line. A single statement can continue over several lines.

In order to improve the readability of your program, use the following conventions: Start the program with a header that tells what the program does. Use meaningful variable names. Document each variable declaration with a comment telling what the variable is used for. Place each executable statement on a single line. A segment of code is a sequence of executable statements that belong together. – Use blank lines to separate different segments of code. – Document each segment of code with a comment telling what the segment does.

Writing Code without detailed analysis and design Repeating trial and error without understanding the problem Debugging the program line by line, statement by statement Writing tricky and dirty programs

100 little bugs in the code, 100 bugs in the code, fix one bug, compile it again, 101 little bugs in the code. 101 little bugs in the code … Repeat until BUGS = 0 — The Internet Joke Book

// my second program in C++ #include using namespace std; int main () { cout << "Hello World! "; cout << "I'm a C++ program"; return 0; } int main () { cout << " Hello World! "; cout << " I'm a C++ program "; return 0; }

// defined constants: calculate circumference #include using namespace std; #define PI #define NEWLINE '\n‘ int main () { double r=5.0; // radius double circle; circle = 2 * PI * r; cout << circle; cout << NEWLINE; return 0; }

+addition -subtraction *multiplication /division %modulo C++ Oprators

expressionis equivalent to value += increase; value = value + increase; a -= 5;a = a - 5; a /= b;a = a / b; price *= units + 1; price = price * (units + 1);

expressionis equivalent to value += increase;value = value + increase; a -= 5;a = a - 5; a /= b;a = a / b; price *= units + 1;price = price * (units + 1); Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

// compound assignment operators #include using namespace std; int main () { int a, b=3; a = b; a+=2; // equivalent to a=a+2 cout << a; return 0; }

c++; c+=1; c=c+1 ;

==Equal to !=Not equal to >Greater than <Less than >=Greater than or equal to <=Less than or equal to (7 == 5) // evaluates to false. (5 > 4) // evaluates to true. (3 != 2) // evaluates to true. (6 >= 6) // evaluates to true. (5 < 5) // evaluates to false.

!(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true. !(6 <= 4) // evaluates to true because (6 <= 4) would be false. !true // evaluates to false !false // evaluates to true. !(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true. !(6 <= 4) // evaluates to true because (6 <= 4) would be false. !true // evaluates to false !false // evaluates to true. ( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ). ( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).

operatorasm equivalentdescription &ANDBitwise AND |ORBitwise Inclusive OR ^XORBitwise Exclusive OR ~NOT Unary complement (bit inversion) <<SHLShift Left >>SHRShift Right

If (condition) statement If (x == 100) cout << "x is 100";

if (x == 100) { cout << "x is "; cout << x; } if (x == 100) cout << "x is 100"; Else cout << "x is not 100"; if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0";

While (expression) statement #include using namespace std; int main () { int n; cout "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0; }

do statement while (condition);

int main () { unsigned long n; do { cout << "Enter number (0 to end): "; cin >> n; cout << "You entered: " << n << "\n"; } while (n != 0); return 0; }

for (initialization; condition; increase) statement ; int main () { for (int n=10; n>0; n--) { cout << n << ", "; } cout << "FIRE!\n"; return 0; }