12
COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 01 1

COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

Embed Size (px)

DESCRIPTION

Comments Provides explanatory notes Purpose of the program/each function Usage of each variable Appear in green in VC++ 1. Single line comments // 2. Multiple line comments /* … */ COMP102 Lab 013

Citation preview

Page 1: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

COMP 102

Programming Fundamentals I

Presented by : Timture Choi

COMP102 Lab 01 1

Page 2: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

Introduction to C++ C++ Extension of C Supports OO programming Free-format languageGeneral form Comment Include necessary header files “main()” function

COMP102 Lab 01 2

Page 3: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

CommentsProvides explanatory notes

Purpose of the program/each function Usage of each variableAppear in green in VC++

1. Single line comments //

2. Multiple line comments /* … */

COMP102 Lab 01 3

Page 4: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

Compiler DirectivesAppear in blue in VC++Import the required library in programPre-defined #include <xxx> E.g. <iostream>, <math>User-defined #include “xxx.h”

COMP102 Lab 01 4

Page 5: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

“main()” FunctionStart of the programVariables/constant declaration Must be declared before used Identifier

Name for variables/constant/function Should not be a keyword Case sensitive Start with letter

Statements Deal with user input/output Specific calculation

COMP102 Lab 01 5

Page 6: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

Programming StyleFree-format language Increase writability

How easily a language can be used to write programs Decrease readability

Measures how easy it can be understoodNote: Write comments for both variables and

functions Use meaningful variable names Only one statement for each line

COMP102 Lab 01 6

Page 7: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

DeclarationsAllocation Reserves memory to store constants and

variablesDeallocation Releases storage of constants and variables

from memoryBinding Associates the constants/variables with

memory location

COMP102 Lab 01 7

Page 8: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

DeclarationsConstant Cannot be changed throughout the

program Syntax:

const <type> <identifier> = <expression>;

const: keyword E.g.

const double pi=3.14, gravity=9.8;

COMP102 Lab 01 8

Page 9: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

DeclarationsVariable Can be changed during program

execution Note: will not be initialized automatically

Better to initialize when declared Syntax:

<type> <identifier>; <type> <identifier>, <identifier>; <type> <identifier> = <expression>;

COMP102 Lab 01 9

Page 10: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

Data Typevoid

no value and operationint

Integer 32 bits (4 bytes)

Float Floating point 32 bits (4 bytes)

Double 64 bits (8 bytes)

char Character

bool Either true/false

COMP102 Lab 01 10

Page 11: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

Data TypeNote: Precision Data size

Depend on design/compiler of the languageBytes/BitsOverflow/UnderflowAffect program size

Data range Signed/Unsigned

Signed bit

COMP102 Lab 01 11

Page 12: COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011

SUMMARYBy the end of this lab, you should be able to: Understand the general form of a program

Comments Compiler directives “main()” function Keyword and identifier

Defined and Initialized Constants Variables

With appropriate data types

COMP102 Lab 01 12