BASIC ELEMENTS OF A COMPUTER PROGRAM

Similar documents
Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Objectives. In this chapter, you will:

Creating a C++ Program

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter-8 DATA TYPES. Introduction. Variable:

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

DEPARTMENT OF MATHS, MJ COLLEGE

Fundamental of Programming (C)

BITG 1233: Introduction to C++

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

The C++ Language. Arizona State University 1

Fundamentals of Programming

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

UNIT- 3 Introduction to C++

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C: How to Program. Week /Mar/05

CS242 COMPUTER PROGRAMMING

COMP 202 Java in one week

2 nd Week Lecture Notes

Chapter 2 - Introduction to C Programming

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Chapter 2: Introduction to C++

Chapter 2 Basic Elements of C++

Language Reference Manual

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Fundamentals of Programming CS-110. Lecture 2

Computer System and programming in C

REVIEW. The C++ Programming Language. CS 151 Review #2

Chapter 2: Using Data

Computers and Programming Section 450. Lab #1 C# Basic. Student ID Name Signature

6.096 Introduction to C++ January (IAP) 2009

LECTURE 02 INTRODUCTION TO C++

Full file at

Introduction to C# Applications

Lecture 3. More About C

Programming Lecture 3

Computer Programming : C++

BTE2313. Chapter 2: Introduction to C++ Programming

Lab # 02. Basic Elements of C++ _ Part1

Introduction to the C++ Programming Language

Computer Components. Software{ User Programs. Operating System. Hardware

Java Notes. 10th ICSE. Saravanan Ganesh

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Differentiate Between Keywords and Identifiers

Typescript on LLVM Language Reference Manual

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Data Types and Variables in C language

Programming and Data Structures

Chapter 2: Overview of C++

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

JAVA Programming Fundamentals

Basics of Programming

Introduction to Programming EC-105. Lecture 2

3. Java - Language Constructs I

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Visual C# Instructor s Manual Table of Contents

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Basics of Java Programming

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Lecture 3 Tao Wang 1

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Programming. C++ Basics

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

VARIABLES AND CONSTANTS

CS102: Variables and Expressions

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Accelerating Information Technology Innovation

CSc Introduction to Computing

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CHIL CSS HTML Integrated Language

Decaf Language Reference Manual

Chapter 2. Lexical Elements & Operators

GraphQuil Language Reference Manual COMS W4115

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CS313D: ADVANCED PROGRAMMING LANGUAGE

egrapher Language Reference Manual

COMP 202 Java in one week

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Datatypes, Variables, and Operations

CSC 1214: Object-Oriented Programming

CHAPTER 3 BASIC INSTRUCTION OF C++

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

1 Lexical Considerations

The SPL Programming Language Reference Manual

Basic Elements of C. Staff Incharge: S.Sasirekha

CS 106 Introduction to Computer Science I

Transcription:

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

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

IDENTIFIERS

IDENTIFIERS Identifier : the name of something that appears in a program. Example of identifiers: variables, function and constants. Identifiers Rules: The 1 st character must be an alphabet letter (a,b,c, z) or an underscore _. Can only consists of letters, digits and underscore _. No symbol allowed (!@#$%^&*.) Cannot use C++ reserved words/keywords There must be no blank space character (Room 3, Level 1) Identifiers are case sensitive Examples : NO1, no1, No1,nO1 (Different)

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

Legal and Illegal Identifiers The following are legal identifiers in C++: _first conversion2 payrate student_name

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 Represent storage location in the computer s memory to store value. A symbolic name in the form of a letter, word or phrase that linked to a value. Every variable stored in the computer s memory has a name, a value and a type. Variable Rules Must begin with a letter or underscore (_), and may only contain only letters, underscore, or digits. Variable name cannot consists of more than 31 characters. Example: _names; number_of_cars; No1;

Variable Declaration Syntax: data_type variable; Example: int x; int number1, number2, number3; char codemi7 ; float after_sale_price; 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. float length = 6.0;

Identifiers

Whitespaces Every C++ program contains whitespaces Include blanks, tabs, and newline characters Used to separate special symbols, reserved words, and identifiers Proper utilization of whitespaces is important Can be used to make the program more readable

STANDARD DATA TYPES

DATA TYPES Data type: set of values together with a set of operations C++ data types fall into three categories: Simple data type Structured data type Pointers

Simple Data Type Three (3) categories of simple data Integral: integers (numbers without a decimal) Can be further categorized: char, short, int, long, bool, unsigned char, unsigned short, unsigned int, unsigned long Floating-point: decimal numbers Enumeration type: user-defined data type

DATA TYPES (Cont.) Every variable used in program hold data, and every data must have their own type. It is the way how we can measure the variable s data value as exist in the real world. Example of data types int, float, double, char

Data Types Categories of data type Numeric data type integer floating points Character data type Single Character String of characters

Numeric Data Type Integer (without a decimal) Whole number positive, zero or negative -543,-414,-30,-28,-1,0,1,232,83,94,725.. C++ keyword int, short, long Floating point Numbers with decimal points -87.345, 90.5443, 1.25,-88.60 C++ keyword float, double int area; float area;

int Data Type May be a zero (0), positive (1,2,3..) or negative (-1,-2,-3..) whole number. int will process integers within the range of -32768 to 32767 long integers will process integers within the range of -4294967296 to 4294967295 Examples: -6728 0 78

float, double Data Type Is a decimal number that contains the decimal point (.)or the exponent (e) or both. float data type range approximately 6 to 7 digits of precision Examples 32.88-89.5 double data type takes approximately 13 decimal digits of precision Examples 12.786675434-25.78301465

Bool Data Type bool type Two values: true and false Manipulate logical (Boolean) expressions Logical values bool, true, and false Reserved words

Character Data Type Single character ( keyword = char ) Store one character value ( a digit, letter or special symbol ) Single character value is enclosed in a single quotation mark ( ) Some of the values belonging to char data type are: 'A', 'a', '0', '*', '+', '$', '&' A blank space is a character Written ' ', with a space left between the single quotes Example: char grade = A ;

char[] Data Type String of characters ( keyword = char []) A string = a sequence of characters that terminated by a null character, \0 which has the value zero. The null character denotes the end of a string and is added to the string by compiler automatically. Example, to declare a string text that could hold 5 characters, the following statement is written. char text[6] = Hello ; Enclosed in double quotation marks ( ). Position of first character is 0, the position of the second is 1, and so on 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 32 6 digit precision 32.88, -89.5 double floating point double 64 12 digit precision 12.7866754 Character Character char 8 c, v Sequence of character char[ ] hello, sarah

Exercises 1) Give the most appropriate data type of the following values. i. 305 ii. A iii. 6.85 iv. RM45.60 v. 20123.12345 vi. + vii. 305 viii.80617

Exercises 1) Which of the following identifiers are VALID? i) Count_number ii) Net salary iii) countryname iv) tax6% 2) If the variable has_membership are declared to store an input from user which is expected to store Y, y, N or n. What is the suitable data type to be used for variable declaration?

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 keyword type const float PI = 3.14; Constant_name value

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

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

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

Initialization 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 1) Write a C++ statement that declare a variable name numberofpeople. Assign the data type int to the variable and initialize it appropriately. 2) Write a C++ statement that declare a variable name interestrate. Assign the data type double to the variable and initialize it appropriately. 3) 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 Highest Lowest Operators Associativity () Left to right * / % Left to right + - Left to right 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.

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

ARITHMETIC EXPRESSION

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

Several rules that you need to follow to evaluate arithmetic expression: 1. If one or more operand(s) in an arithmetic expression has/have data type double, the resulting expression is of data type double. 2. 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 0-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 : 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 : 1. (c 5.0) = 10.0 2. (d + 7.0) = 15.0 3. (e - 37.0) = 3.0 4. (a + b / 10.0) / (15.0 / 3.0 / 3.0) 5. (a + 2) / (5.0 / 3.0) 6. (12) / (1.667) 7. 7.2

LOGO