A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include<iostream> using namespace std; int main () { cout <<

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Structure of a C program
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.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
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.
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
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
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.
1 C++ Syntax and Semantics, and the Program Development Process.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
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++
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 Structure of Simple C++ Program Chapter 1 09/09/13.
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.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
A Sample Program #include using namespace std; int main(void) { cout
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
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Bill Tucker Austin Community College COSC 1315
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
CMPT 201.
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
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++.
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Introduction to C++ Programming
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Subject:Object oriented programming
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Presentation transcript:

A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include<iostream> using namespace std; int main () { cout << "Hello world!\n"; return 0; }

A simple C++ program // // This program prints the phrase "Hello world!" // on the screen #include<iostream> using namespace std; int main () { cout << "Hello world!\n"; return 0; } C++ - style comments. Everything between the // and the end of line is ignored by the compiler.

A simple C++ program // // This program prints the phrase "Hello world!" // on the screen #include<iostream> using namespace std; int main () { cout << "Hello world!\n"; return 0; } #include is a preprocessor directive. Preprocessing occurs before compilation. This directive instructs the preprocessor to include the file iostream at that point. iostream is a library containing routines that perform I/O

A simple C++ program // // This program prints the phrase "Hello world!" // on the screen #include<iostream> using namespace std; int main () { cout << "Hello world!\n"; return 0; } Informally, a namespace is a set of all the names that are declared at some point. We will talk about namespaces much later. For now, just remember to add this line after the #include directives.

A simple C++ program // // This program prints the phrase "Hello world!" // on the screen #include<iostream> using namespace std; int main () { cout << "Hello world!\n"; return 0; } Program execution always begins in the ‘main’ function. You may invoke other functions from within main() The body of main()

Indentation It helps make the code readable. The compiler ignores white space. int main() { int i; for(i=0; i<10; i++) { cout << "1TBS" << endl; } return 0; int main() { int i; for(i=0; i<10; i++) { cout << "Allman" << endl; } return 0; Choose ONE style and STICK with it!

Variables The syntax for declaring a variable is: data_type variable_name ; or, for several variables of the same type: data_type variable_name1, variable_name2 ; Variables may be declared at any point in the program, but they must always be declared before they are used. The name of a variable is a sequence of letters, digits and underscores that does not begin with a digit. C++ is case sensitive

Readability Pick meaningful variable names that describe the entity they refer to. Things to avoid: the letter l (el) which can be confused with the digit 1 (one) single-letter variables (there's one exception) variable names starting with one or two underscores (many internal C++ names follow this convention) extremely long names names similar to C++ reserved words names similar to one another

Basic types int for integers (4 bytes*) float and double for single- and double-precision floating point numbers (4 and 8 bytes respectively*) char for characters (1 byte*) * for our architecture

Literals integer literal double literal string literals #include<iostream> using namespace std; int main () { cout << 10 << "USD = " << 8.2 << "EUR\n"; return 0; } integer literal double literal string literals

Literals Major disadvantages: Good idea: Look ma, no semicolon! a literal cannot be reused (you have to type it in every time) it is easy to make a typo if you want to change it, you have to make sure you change all of its occurrences inside the program. Good idea: Use #define (C++ preprocessor directive) to define a literal Look ma, no semicolon! #define NUM_DOLLARS 10 #include<iostream> using namespace std; int main () { cout << NUM_DOLLARS; return 0; }

#defined constants Location: with other directives (e.g. #include) Syntax: #define UNIQUE_NAME some_value Before compiling, pre-processor does 'find & replace' on your file: every occurrence of UNIQUE_NAME is replaced by some_value. Convention: UNIQUE_NAME is always in UPPERCASE. Major advantages: One single, central location for fixed values prevents scattered, hard-to-find errors in literals makes it easy to modify the value Works for ANY repeated text, not just numbers string literals, even executable text statements…

named constants Declared like variables BUT preceded with the keyword const initialized at declaration their value CANNOT change afterwards. #include<iostream> using namespace std; int main () { const int num_dollars = 10; cout << num_dollars; return 0; }

Operators Assignment operator: = Arithmetic operators: +, , *, /, % Relational operators: <, >, <=, >=, ==, != Logical operators: &&, ||, !

Operators ( ) ! Unary – * / % higher precedence + – * / % + – < <= >= > = = != && || = higher precedence lower precedence Associativity: left-to-right (except = and unary operators)