Lecture 4 Computer Programming -1-. 2-2 // sample C++ program #include using namespace std; int main() { cout << "Hello, there!"; return 0; } 2-3 comment.

Slides:



Advertisements
Similar presentations
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Advertisements

Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
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.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Chapter 2: Introduction to C++.
Basic Elements of C++ Chapter 2.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
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.
Introduction to C The Parts of a C++ Program –Anatomy of a simple C++ program.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
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++
Basic Program Construction
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Chapter 2 part #1 C++ Program Structure
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Lecture 2 Variables, Types, Operators Sampath Jayarathna Cal Poly Pomona Based on slides created by Bjarne Stroustrup & Tony Gaddis CS 128 Introduction.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
C++ First Steps.
© by Pearson Education, Inc. All Rights Reserved.
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.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
What Actions Do We Have Part 1
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.
Chapter 2: Introduction to C++
Chapter 2, Part I Introduction to C Programming
Computing Fundamentals
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2 – Getting Started
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
COMS 261 Computer Science I
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

Lecture 4 Computer Programming -1-

2-2

// sample C++ program #include using namespace std; int main() { cout << "Hello, there!"; return 0; } 2-3 comment preprocessor directive which namespace to use beginning of function named main beginning of block for main output statement string literal send 0 to operating system end of block for main

2-4 CharacterNameMeaning // Double slashBeginning of a comment # Pound signBeginning of preprocessor directive Open/close bracketsEnclose filename in #include ( ) Open/close parentheses Used when naming a function { } Open/close braceEncloses a group of statements " Open/close quotation marks Encloses string of characters ; SemicolonEnd of a programming statement

The cout Object

Displays output on the computer screen You use the stream insertion operator < < to send output to c out : cout << "Programming is fun!"; 2-6

Can be used to send more than one item to cout: cout << "Hello " << "there!"; Or: cout << "Hello "; cout << "there!"; 2-7

This produces one line of output: cout << "Programming is "; cout << "fun!"; 2-8

You can use the e ndl manipulator to start a new line of output. This will produce two lines of output: cout << "Programming is" << endl; cout << "fun!"; 2-9

2-10 Programming is fun! cout << "Programming is" << endl; cout << "fun!";

You do NOT put quotation marks around e ndl The last character in e ndl is a lowercase L, not the number endl This is a lowercase L endl

You can also use the \ n escape sequence to start a new line of output. This will produce two lines of output: cout << "Programming is\n"; cout << "fun!"; 2-12 Notice that the \n is INSIDE the string.

2-13 Programming is fun! cout << "Programming is\n"; cout << "fun!";

The #include Directive

Inserts the contents of another file into the program This is a preprocessor directive, not part of C++ language #include lines not seen by compiler Do not place a semicolon at end of #include line 2-15

The General form of the program written in a language C++ (General Syntax) : #include int main () { ……….. return 0 ; }

The parts of C++ program // This program will display a message on the screen. #include int main () { cout<< “ welcome to c++ ! \n “ ; Return 0 ; } Output : welcome to C++ !

1- comments : // This program will display a message on the screen. // comments are text that is ignored by the compiler but tell notes or descriptions at any statement in the program. Types of comments : 1- //double-slash comment : Tell compiler to ignore everything that follow this comment until the end of the line. 2- /* slash –star comment : Tell the compiler to ignore everything that follows the comment until it find Star-slash */

2- Preprocessor Directive : ( #include ) this file must be included for any program that outputs data to the screen or inputs data from the keyboard using (cout) and (cin) statements.

3- main function : ( int main () ) Every C++ program has main () Function. main () Function is a special Function because it is called automatically when the program start but the other Functions are called by other Functions. 4- { } begin, end the body of every function.

5- Output ( Cout<<“welcome to c++ ! \n“ ; ) cout statement used to print messages and values to the screen. Example : #include int main ( ) { cout << 7 << " is an integer.\n"; cout << 'a' << "is a character.\n"; return 0 ; } Output : 7 is an integer. a is a character

- Escape Sequences : \n : tell cout to put new line after the printer line \t : insert a tab character Cout<<"Hello world \t I'm nora"; (program) Hello world I'm nora (output) 6- return 0 ; mean that program ended successfully.

Examples Cout<<"Hello world"; Cout<<"I'm a C++ programmer"; Hello world I'm a C++ programmer (output) Cout<<"Hello world\n"; Cout<<"I'm a C++ programmer"; Hello world I'm a C++ programmer (output). Cout<<"Hello world"<<"I'm a C++ programmer"; Hello world I'm a C++ programmer (output)

Cout<<"Hello world\n"<<"I'm a C++ programmer"; Hello world I'm a C++ programmer (output) Age=19; Cout<<''I'm "<<Age << "years old'' ; I'm 19 years old (output) Cin>>Age; Cout<<''I'm "<<Age<< "years old" ; 18 I'm 18 years old (output)

Example : What prints when each of the following c++ statement is executed let x=2 and y=3 a- cout<< x; b- cout<< x+x ; c- cout<<"x=" ; d- cout<<"x="<<x ; e- //cout<< x+y ; f- cout<< x+y <<"="<<y+x ; g- cout <<“ \n “ ;