A first program 1. #include 2. using namespace std; 3. int main() { 4. cout <<“\tHello World\n"; 5. return 0; 6. }

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 2: Introduction to C++.
Basic Elements of C++ Chapter 2.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
 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++
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.
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.
Input & Output: Console
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
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.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
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
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 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++.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
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
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
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.
LESSON 2 Basic of C++.
Chapter 2: Introduction to C++
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Introduction to C Programming
Presentation transcript:

A first program 1. #include 2. using namespace std; 3. int main() { 4. cout <<“\tHello World\n"; 5. return 0; 6. }

Line 1. Pre-processor line #include using namespace std;

function name type of returned value argument Line 2. main() function, entry point int main() Next Slide

Line 3. Use cout to display information cout << “\tHello World\n”; \t is a tab \n is a new line Redirection (insertion) Next Slide

Programs with simple input and output

cout <<"Here is 5: "<<5<<"\n"; Output Here is 5: 5 Example of cout Cursor ends up here

cout <<"A big number:\t"<<70000<<endl; Output A big number: Another example cout Tab moves to next position Another way of Forcing a new line

cout <<"The sum of 8 & 5 is "<<8 + 5<<endl; Output The sum of 8 & 5 is 13 Yet another example of cout Calculations

cout <<"Big # : "<< *7000.0<<endl; Output Big # : 4.9e+07 A Look at cout very big numbers

cout <<"A fraction: "<<5.0/8.0 <<endl; Output A fraction: A Look at cout fractions

Getting information from the user using cin, usually paired with cout cout > num1; The contents of the address named num1 is... Extraction operator

cout << “Enter 3 numbers: “; cin >> num1 << num2 << num3; The contents of the address named num1 is … num2 is … num3 is... A Look at cin

Reserved Words Words that have special meanings in the language. They must be used only for their specified purpose. Using them for any other purpose will result in a error. e.g. coutdoifswitch cinwhileelsereturn *

Reserved Words C++ is case-sensitive. Thus: cout COUTCoutcOut are all different. The reserved words are all in lowercase.

Statements A statement controls the sequence of execution, evaluates an expression, or does nothing, and ends with a semicolon.

Statements { cout <<"A fraction: "<<5.0/8.0 <<endl; cout <<"Big # : "<< *7000.0<<endl; cout <<8 + 5 <<" is the sum of 8 & 5\n"; cout << “Hello world”; } There are 4 statements in this block. Block denoted by Curly braces

Comments, for PDL to Code These are important parts of a program. Two types of comments // ignore rest of line /* ignore between these */ or /* ignore over These lines */

Programming Style one main() function and use consistent layout of braces int main ( ) { statements; } indent open close

Declaration statements Any variable must be declared (announced) before it is used. This is a law of C/C++. Declaration instructs compiler to reserve memory to store values #include Using namespace std; int main() { int num1; cout << “Enter a number: “; cin >> num1; cout << “You entered number “ << num1 << endl; return 0; } declaration

Programming Style Group declarations at the beginning, just after the opening brace of main void main () { declaration statements; other statements; }

Programming Style Put blank lines before and after control structures int main () { declaration statements; statements if (expression){ statement; statement; } statements; }

Terminology: Atomic Data Something that is no decomposable. int float char +some others

int Any number +ve or –ve without a decimal point. Will truncate (cut off) any decimal points E.g. 5/8 is 0 not 0.625* big cause of errors E.g 15/2 is 7 not 7.5

float or double Sometimes called real. Contains decimal point. Precision Accuracy, note computers have problems with accuracy of floating point numbers. E.g. Pi Default is 6 significant digits Exponential notation E.g is 7.0e+03

char A single character, letter or digit. Enclosed in single quotes ‘A’‘a’‘1’ Stored using ascii code.

Simple strings with char [] To input non numeric data, like names, and addresses, use character arrays. char FirstName[20] stores a string of up to 20 characters.

Simple String Example #include using namespace std; int main(void) { char FirstName[10]; cout << "Enter your first name : "; cin >> FirstName; cout << "Hello " << FirstName << endl; return 0; }

Output Enter your first name : Andreas Hello Andreas Press any key to continue

Demonstration – PDL –Code incremental development Pendulum problem Create defining diagram InputsProcessesOutputs periodIntitialise g,Pi. Get period Compute Plength Plength

Verification Tests Try Period = 2*Pi = Why? Length = g = 9.8

PDL for pendulum 1. Assign 9.8 to g 2. Assign to Pi 3. Prompt for a period in seconds 4. Calculate the length in metres 5. Display length for required period Use formula (given) length = g*(period/2*Pi) 2

Start Visual C++ environment Create console project in new solution Select Empty Project in Application Settings Create new C++ source file – add to project Enter skeleton program Check it works! Enter design as comments Add program description comment at top Test! Expand design one line at a time testing as you go