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.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 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.
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.
Structure of a C program
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++.
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Programming 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.
Chapter 01: Introduction to Computer Programming
Introduction to C++ Programming
COMPUTER SCIENCE I C++ INTRODUCTION
 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++
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.
The Java Programming Language
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Week 1 Algorithmization and Programming Languages.
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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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++
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
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++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
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 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
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Completing the Problem-Solving Process
Computing Fundamentals
Basic Elements of C++.
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
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
1.13 The Key Software Trend: Object Technology
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Programming Introduction to C++.
Presentation transcript:

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

An Introduction to Programming with C++, Fifth Edition 2 Creating a C++ Program source code Object code Executable code: exe file created using an IDE

Processing a C++ Program (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3

The Problem Analysis–Coding– Execution Cycle (cont’d.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 4

Structure of C++ Program #include void main( ) { cout<<“Welcome to Computer Scientists”; getch(); } Program Body main function Starting point of program execution Preprocessor Directives Header File To include header files in our program Contains built-in-functions. These are also called pre-defined or already developed functions File name(standard input output ) h stands for header file Function Name Return type of main function. It is used to determines whether the program is executed successfully or not. Void means nothing Parenthesis shows function. It is used to pass parameters/arguments Body Begin Body End Cout stands for console output.. It uses insertion operator (<<) print the output on the monitor/console. Angle or pointed brackets String or message to be displayed on monitor. It must be enclosed in double quotes(“ “) Each statement must end with semicolon. A statement without semicolon generates syntax error Get character function. It get a character at runtime. But we use it to stay screen to see output 5

>> … <<… cin cout cerr Memory Key Board Standard input stream Standard output stream Monitor Standard input and output streams

Simple Steps to write and run a program in C Go to this path C:\TC\BIN then double click on short to open C++ language. 2. Now save your program by choosing a meaningful program file name. e.g. welcome.cpp is a file name for a program that will display welcome message at runtime. 3. Now write your program as explained in the previous slide. (translation of algorithm to a c++ program). 7

4. Press Alt+F9 key to compile program. If your program is error free then following screen will appear: 8

5. Now press Ctrl+F9 key to run the program. Following screen shows the output of the program: Now press enter to go back to the source code of the program 9

Another Sample Program Program: #include void main() { int number; cout<<” Enter an integer: \n ” ); //message for user cin>>number; //get input cout<<” The number you entered is: “<<number; getch(); } Output: Enter an integer: 25 The number you entered is: 25 10

Programming Style 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. 11

Alphabets from A to Z or a to z The digits from 0 to 9 Underscore(_) can be used The first character of an identifier can not be a digit The name of an identifier can not be a reserve word No space allowed in the name of identifier Rules to Declare an Identifier (variable) Valid Name: A Student_Name _Fname Pi Inalid Name: $Sum //special ch. 6StName // 1 st letter digit F name // no space allowed int // reserve word 12 Variable: Location in memory where value can be stored An identifier is a name for a variable, constant, function, etc.

Syntax Data-Type Space Variable-Name(Indentifier); e.g. int frstNumber; char choice; float divide; long output; Identifier (variable) Declaration Data-Type Space Variable-Name(Indentifier) = Value; e.g. int frstNumber=10; char choice=‘y’; float divide=0.0; Syntax Identifier (variable) Initialization 13

Memory Concepts cin>> first; Assume user entered 45 cin>>second; Assume user entered 72 sum = first + second; first 45 first 45 second 72 first 45 second 72 sum 117 Identifier Variable 14

Assignment Operator (=) = (assignment operator) Assigns value to variable Binary operator (two operands) Example: sum = variable1 + variable2; 15

How many bytes I am eating? integer data short2 bytes int2 bytes(16 bit system) 4 bytes (32 bit system) long4 bytes Floating point data float4 bytes double8 bytes long double10 bytes Characterchar 1 byte Booleanbool 1 byte 16

Arithmetic Rules of operator precedence Operators in parentheses evaluated first Nested/embedded parentheses  Operators in innermost pair first Multiplication, division, modulus applied next Operators applied from left to right Addition, subtraction applied last Operators applied from left to right 17

Comments Non - executable statements Comments are used for program documentation Two formats Single Line CommentsMulti Lines Comments // This program is used to show the Welcome Message. /* This program is used to show the square of even numbers from 10 to 100. */ 18

Tokens Tokens are individual words and punctuation marks in passage of text. In C++, program the smallest individual units are known as C Tokens. C++ has Six types of Tokens. The Tokens are shown in figure. C++ programs are written using these tokens and the syntax of the language. 19

20

C++ keywords Each keyword has a predefined purpose in the language. Do not use keywords as variable and constant names!! Exmples: 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 etc etc.. 21

22 Thank You New Computer Scientists

Memory Concepts Variable names Correspond to actual locations in computer's memory Every variable has name, type, size and value When new value placed into variable, overwrites previous value 23