BASIC ELEMENTS OF A COMPUTER PROGRAM

Slides:



Advertisements
Similar presentations
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Advertisements

Chapter 2: Basic Elements of C++
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Chapter 2: Basic Elements of C++
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 2: Basic Elements of C++
Chapter 3: Input/Output
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
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.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Basic Elements of C++ Chapter 1.
C++ Programming: Basic Elements of C++.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
1 CS161 Introduction to Computer Science Topic #3.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
1 Input (Read) Statement. Variable Declaration (Definition) Initialization Assignment Displaying variables: using cout statement Reading variables from.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
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 3: Input/Output Samples.
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
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
TK1913 C++ Programming Basic Elements of C++.
Arithmetic Expressions
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++ Chapter 1.
Chapter-01 A Sample C++ Program.
Topic Pre-processor cout To output a message.
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Introduction to C++.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
User Defined Functions
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Programming Funamental slides
Programming Funamental slides
Chapter 3: Input/Output
Chapter 3 Input output.
CS150 Introduction to Computer Science 1
elementary programming
CHAPTER 2: COMPONENTS OF PROGRAMMING LANGUAGE
Chapter 2: Introduction to C++.
What Actions Do We Have Part 1
C++ Programming Basics
Unit 3: Variables in Java
Chapter 1 c++ structure C++ Input / Output
Basic Elements of Computer Program
Presentation transcript:

BASIC ELEMENTS OF A COMPUTER PROGRAM

Contents 1. Statements 2. C++ Program Structure 3. Programming process 4. Control Structure

statements

Assignment statements

Assigns a value to respective memory space allocated to a variable. Assignment statement Assigns a value to respective memory space allocated to a variable. May involve arithmetic expressions. Syntax: Examples variable = value/ variable; a = 3; x = 5.6; y = ‘z’; pi = 3.14; 5 = z; //invalid!!

Assignment statement Syntax: The expressions may contains variables, constant and arithmetic operators, Example: variable = expression; float radius, pi, area; radius = 3; pi = 3.14; area = pi * radius * radius;

A sequence of character: Assignment statement A sequence of character: Using assignment statement: Using strcpy function: char a [20] = “sample string”; char a [20]; strcpy(a ,“sample string”);

Exercises Write a C++ assignment statement that assigns the letter T to a char variable named insured. Write a C++ assignment statement that multiplies the number 10.75 by the content of a double variable named hours, and assigns the result to a double variable named grossPay. Assuming the answer variable is an int variable, the assignment statement answer=8/4*3-5%2; assign the value ______ to the answer variable. What will be assigned to the answer variable in question 3 if the variable is a double variable?

Converting algebraic expression into C++ statements

Writing algebraic expression using C++ statement. Algebraic statement pow(a,b) ab sqrt(a - b) √ (a – b) abs(x - y) | x – y |

Arithmetic Expression Examples Algebraic Expression Arithmetic Expression 17– 12 + 74 23 – 7 5 23 – 7 / 5 12 + 1 + 76 4 (12 + 1 + 76) / 4 42 pow ( 4 , 2 )  ( a – b) sqrt (a – b )  x – y  abs ( x – y )

Examples: -b ± √b2 – 4ac 2a Solution : Or -b + sqrt ((pow (b,2) – (4 * a * c))/(2 * a) Or -b - sqrt (b * b – 4 * a * c) / (2 * a)

Exercises s = t3 – uv3 g = 4∏2 a3 p2(m1+m2) 3) a = 2x + y 3(x – y)+ 2 4) a = 2(x2 + y) + 1 x – y x + y

Exercises a = √ x + y a = 2 √ x + y2 7 a = p + 5√x – y √ x + y

Input output statements

Used to display information on screen. Output statements Used to display information on screen. The header file iostream.h must be included in the preprocessor directives. Syntax: The symbol << is called insertion operator cout<<variable<<variable<<…<<variable;

Output statements Sample output: … year = 1999; cout << “my year of birth is”<<year; my year of birth is 1999

cout << (2003 – year); Output statements The operand after the symbol << can be a constant, variable or an expression. The following are escape sequence which can be used in a string literal in a cout statement. cout << (2003 – year); \n – new line \’ – single quote \t - tab \” - double quote \b - backspace \\ - backslash

Used to read in information from a keyboard. Syntax: Input statement Used to read in information from a keyboard. Syntax: The symbol >> is called an extraction operator. The operand after the symbol >> must be a variable. cin>>variable>>variable>>…>>variable;

Input statement: example … cout << “enter your age and matric number:”; cin>>age>>matric;

When reading a sequence of character (including whitespace), used: Input statement cin only takes input up to the first blank (ignores whitespace such as blanks, tabs). When reading a sequence of character (including whitespace), used: cin.getline(name, 20);

Example Using cin statement: Sample output cout<< “Enter your name:”; cin>>name; cout<< “Welcome, ”<<name<<“!”; Enter your name: Adam Ahmad Welcome, Adam!

Using cin.getline statement: Example Using cin.getline statement: Sample output cout<< “Enter your name:”; cin.getline(name,20); cout<< “Welcome, ”<<name<<“!”; Enter your name: Adam Ahmad Welcome, Adam Ahmad!

C++ Program structure

Components of a program Preprocessor directives Constant and type definition section Main program heading Begin block Main block Declaration section Statement section End block

Syntax #include <headerFileName> const <dataType><identifier> = <value> dataType <identifier> <functionType> <functionName> (formal parameter list) { cout << statement; cin >> variables; }

A C++ program is a collection of one or more subprograms, called functions A subprogram or a function is a collection of statements that, when activated (executed), accomplishes something Every C++ program has a function called main

Preprocessor directives Examples: #include <iostream.h> To declares the basic C++ streams (I/O) routines. #include <math.h> Declares prototypes for the math functions and math error handlers. #include <conio.h> Declares various functions used in calling the operating system console I/O routines.

Example of program Preprocessor Directives main function #include <iostream.h> #include <conio.h> #include <string> void main() { string name; int age; cout<< "Please enter your name:\n"; cin>>name; cout<< "Please enter your age:\n"; cin>>age; cout<<endl; cout<< "Your name is:"<<name<<endl; cout<< "Your age is:"<<age<<endl; getch(); } Preprocessor Directives main function

Programming process

Compilation process

Running a program

Control structures

Sequential Control Structure The simplest of all the structures The program instruction has one starting point and one ending point. Each step is carried out in order of their position and is only done once.

Sequential Control Structure Flowchart Begin Fill a kettle with water Boil the water in the kettle Put tea leaves in the pot Pour boiling water into the pot End

Selection Control Structure The selection structure allows comparison of expression, and based on the comparison, to select certain course of action

Selection Control Structure Telephone rings? F T Continue reading Answer phone

Repetition control structure The repetition structure allow a sequence of instructions to be executed repeatedly until a certain condition is achieved.

Repetition control structure beg has items? false true Take item out

Modular Is a method of dividing a problem into separate tasks, each with a single purpose. Separate task with a single purpose = FUNCTION

Modular Company IT Accountancy Human resource

Thank You !