BASIC ELEMENTS OF A COMPUTER PROGRAM

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
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 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Chapter 2: Using Data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
Constants, Data Types and Variables
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
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Numbers in ‘C’ Two general categories: Integers Floats
Chapter # 2 Part 2 Programs And data
Chapter 2: Basic Elements of C++
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Computer Programming BCT 1113
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Revision Lecture
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++
Introduction to C Programming
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Chapter 2 Elementary Programming
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
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.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Introduction to C Programming
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Chapter # 2 Part 2 Programs And data
elementary programming
Chapter 2: Introduction to C++.
WEEK-2.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Chapter 2 Variables.
Variables in C Topics Naming Variables Declaring Variables
Basic Elements of Computer Program
Programming Fundamental-1
Presentation transcript:

BASIC ELEMENTS OF A COMPUTER PROGRAM CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Arithmetic expression Contents Identifier 1 Rules for naming and declaring data variables 2 Basic data types 3 Arithmetic operators 4 Arithmetic expression 5 Assignment statement 6

IDENTIFIERS

Identifiers An identifier is a name assigned by the user. It is to represent programming entities such as variable, function and constants. Rules in naming an identifier: The 1st character must be an alphabet letter or an underscore ‘_’. Can only consists of letters, digits and underscore ‘_’. Cannot use C++ reserved words/keywords There must be no blank space character Identifiers are case sensitive

Reserved Word It has predefined meaning for the language. int char long float double default case for void break const else if private do goto switch while public class return continue

Variable A location in computer memory to store data. Can only store 1 value at a time Value of a variable may change during program execution. Must be declared before used. Must always assign/ initialize a value to a variable before using the variable.

Variable Declaration Syntax: data_type variable; Example: int x; int number1, number2, number3; Please take note of the coma to Separate the variable list & Semicolon to end the variable declaration

Variable: what happen when a variable is declared?? The compiler allocates a memory space for the variable. In the memory space, name, data type and value of the variable are stored. ex. Allocates a memory of 4 bytes with address 00000004. the information stored in the memory space are variable name (area), data type (float). float area;

Variable Rules Must begin with a letter or underscore (_), and may only contain only letters, underscore, or digits. Variable name cannot be a keyword. Variable name cannot consists of more than 31 characters.

Constant Values that are fixed and do not change throughout program execution Declaring a constant: Using const keyword

Constant Declaration Syntax: const data_type constant_name = value; const float PI = 3.14; type const keyword value Constant_name

Legal and Illegal Identifiers The following are legal identifiers in C++: first conversion payRate

STANDARD DATA TYPES

Data Types Categories of data type Numeric data type Character data type

Numeric Data Type Integer Floating point Whole number – positive, zero or negative C++ keyword int, long Floating point Numbers with decimal points float, double int area; float area;

int Data Type Examples: -6728 78 78 Positive integers do not have to have a + sign in front of them No commas are used within an integer Commas are used for separating items in a list

float, double Data Type Is a decimal number that contains the decimal point (.)or the exponent (e) or both. float data type can hold values up to six or seven significant digit accuracy. double data type can hold values up to 14 or 15 significant digit accuracy.

char Character Data Type char name; Single character Example: Store one character value C++ keyword char Example: char name;

char Data Type The smallest integral data type Used for characters: letters, digits, and special symbols Each character is enclosed in single quotes Some of the values belonging to char data type are: 'A', 'a', '0', '*', '+', '$', '&' A blank space is a character and is written ' ', with a space left between the single quotes

char[] Data Type Sequence of characters It stores sequence of zero or more characters Enclosed in double quotation marks Null: no characters is stored. Each character has relative position Position of first character is 0, the position of the second is 1, and so on Length: number of characters in a sequence C++ keyword char[] char name[5]; Note: the double quotes and Single quotes

SUMMARY OF DATA TYPE Category Data types Keywords Bits Range Examples Numeric integer int 16 -32768 to 32767 45, 0, -10 long integer long 32 -4294967296 to 4294967295 -37876 floating point Float 6 digit precision 32.88, -89.5 double floating point double 64 12 digit precision 12.7866754 Character char 8 ‘c’, ‘v’ Sequence of character char[ ] “hello”, “sarah”

Exercise Problem: to calculate area of a circle Data Data type Variable name Input radius float Output area areaCircle Process areaCircle = 3.142*radius*radius

Exercises Problem: to compute the area of triangle. Data Data type Variable name Input Output Process

Exercise Problem: to display user’s name and retirement age Data Data type Variable name Input Output Process

Initialization float rate = 0.0; double sale = 0.0; char grade = ‘ ’; A variable can be initialized to any value as long as the value’s data type matches the variable’s data type. Syntax: datatype variable [ = initialValue ]; Examples: int age = 0; float rate = 0.0; double sale = 0.0; char grade = ‘ ’; char company[20] =“ ”;

Exercises Write a C++ statement that declare a variable name numberOfPeople. Assign the data type int to the variable and initialize it appropriately. Write a C++ statement that declare a variable name interestRate. Assign the data type double to the variable and initialize it appropriately. Write a C++ statement that declare a float variable named rate. Initialize the variable to the number 5.6

operators

OPERATORS A Symbol that tells the compiler to perform specific mathematical or logical manipulations Types of operator: Arithmetic operators Assignment operators Relational operators Logical operators

Arithmetic operator Addition Subtraction Modulus Multiplication Division

Arithmetic operator Operation Operator Subtraction - Addition + Multiplication * Division / Modulus %

Precedence & Associativity Precedence of operator Precedence rules: specify which operator is evaluated first when two operators with different precedence are adjacent in an expression. Associativity rules: specify which operator is evaluated first when two operators with the same precedence are adjacent in an expression. Highest Operators Associativity () Left to right * / % + - Lowest

Precedence & Associativity Example 1: Example 2: 4 + 2 * 3 3 * 4 / 2

Arithmetic expression

operands One of the input of an operator Example: 3 + 6 Operator

Several rules that you need to follow to evaluate arithmetic expression: If one or more operand(s) in an arithmetic expression has/have data type double, the resulting expression is of data type double. If all operands in arithmetic expression are of type int, the resulting expression is of type int.

Arithmetic operators Require two operands Ex. +, -, *, /, % For the modulus (remainder) operator, all of its operands must be integer only. Be careful of the operands’ data types when using division operator. For example, Expressions Output 7 / 4 1 7.0 / 4 1.75 -1 / 4 -1 / 4.0 -0.25

Example 3 * 7 – 6 + 2 * 5 / 4 + 6 (3 * 7) – 6 + ( (2 * 5) / 4 ) + 6 = 21 – 6 + (10 / 4) + 6 (Evaluate *) = 21 – 6 + 2 + 6 (Evaluate /) = 15 + 2 + 6 (Evaluate -) = 17 + 6 (Evaluate first +) = 23 (Evaluate +)

Test yourself!!!! Examples : double a, b, c, d, e; Declaration double a, b, c, d, e; Initialization a = 10.0, b = 20.0, c = 15.0, d = 8.0, e = 40.0; Expression to be evaluated (a + b / ( c – 5.0 )) / ((d + 7.0) / (e – 37.0) / 3.0)

Answer: (a + b / ( c – 5.0 )) / ((d + 7.0) / (e – 37.0) / 3.0) The sequence : (c – 5.0) = 10.0 (d + 7.0) = 15.0 (e - 37.0) = 3.0 (a + b / 10.0) / (15.0 / 3.0 / 3.0) (a + 2) / (5.0 / 3.0) (12) / (1.667) 7.2

Thank You !