Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

A Lesson2-Basic-Elements-of-a-Program

This document summarizes some basic elements of a C++ program, including symbols, comments, reserved words, identifiers, data types, variables, operators, expressions, and more. Key points are that it discusses symbols like commas and semicolons, comment syntax, important reserved words, rules for valid identifiers, basic data types, how variables are used to store and represent data, common operators like assignment, arithmetic, relational, and logical, and how expressions are used for computations and conditional logic.

Uploaded by

Satoru Fujinuma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

A Lesson2-Basic-Elements-of-a-Program

This document summarizes some basic elements of a C++ program, including symbols, comments, reserved words, identifiers, data types, variables, operators, expressions, and more. Key points are that it discusses symbols like commas and semicolons, comment syntax, important reserved words, rules for valid identifiers, basic data types, how variables are used to store and represent data, common operators like assignment, arithmetic, relational, and logical, and how expressions are used for computations and conditional logic.

Uploaded by

Satoru Fujinuma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIT 2

C++ Basics

IT 103 Computer Programming 1


Lesson 2
Basic Elements of a Program

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Symbols

, Commas are used to separate items in a list


; Semicolons are used to end statement

+ - * / mathematical symbols
<= != == >= relational operators

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Symbols
Comment symbols
// single-line comment symbol

For example,
// This is my first program
// C++ Language

/* */ multiple-line comment symbol


For example;
/*This is my first program
C++ Language
*/

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Reserved Words
Reserved words are also called keywords that cannot be redefined
within any program; that is, they cannot be used for anything
other than their intended use.
Some of the important reserved words are :
char float int void
break if else switch
case return for do
while main

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Identifier
Identifier refers to the names of variables, constants, functions
and other objects defined in a C++ program. It allows us to name
data and other objects in the program.

Rules for identifiers are:


•The only valid name symbols are the capital letters A
through Z, the lowercase letters a through z.
•the digits 0 through 9, and the underscore.
•The first character of the identifier cannot be a digit or an
underscore.

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Identifier
Rules for identifiers are:
•An identifier cannot contain punctuation marks, math
symbols or other special symbols.
•ANSI guarantees only the first 32 characters to be
significant.
•C++ identifier is case-sensitive
•The last rule is that the name we create cannot be
keywords.

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Identifier
Examples of Valid and Invalid identifiers
Valid Identifiers Invalid Identifiers
grade grade+3
area1 1area
area_of_triangle area of triangle

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Data Type
A data type defines a set of values that a variable can hold.
Based on the data type of a variable; the operating system
allocates memory and decides what can be stored in the
reserved memory.
Type Keyword Data type Examples
Boolean boolean
Character char boolean true, false
Integer int
Floating point float char ‘A’ , ‘b’ ,’+’
Double floating double
point int 10,200,550
Valueless void
float /double 10.54, 2.56

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Variable

A variable can hold a number or data of other types. The


number or other type of data held in a variable is called its
value. In programming languages, variables are implemented
as memory location. The compiler assigns a memory location
to each variable name (identifier) in the program. Each
variable in a program must be declared. When you declare a
variable you are telling the compiler what kind of data you
will be storing in the variable

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Variable
Variable Declaration

A syntax rule to declare a variable is:

datatype identifier;

For example:
int x;

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Operator

An operator is a symbol that tells the compiler to perform


specific mathematical or logical manipulations.

Types of operators:
•Assignment Operator
•Arithmetic Operators
•Relational Operators
•Logical Operators

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Operator

•Assignment Operator

The assignment operator (=) is used in the assignment


statement which takes the following form:

variable = expression;

Example:
y = 5;

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Operator
•Arithmetic Operators
Operator Meaning Example
* Multiplication x*y
/ Division x/y
% Modulus x%y
+ Addition x+y
- Subtraction x–y
++ Increment x++
-- Decrement x--
+ (unary) Positive sign +x
- (unary) Negative sign -x

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Operator
Order of Precedence

It is possible to build mathematical expressions with several operators. When


more than one arithmetic operator is used in an expression, C++ uses the
operator precedence rules to evaluate the expression. According to the order
of precedence rules for arithmetic operators,

* , / ,% are at higher level of precedence than + , -

However, when operators have the same level, the operations are performed
from left to right.

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Operator
•Relational Operators

Two expressions can be compared using relational and equality operators.


The result of such an operation is either true or false
The relational operators in C++ are:

< > <= >= == !=


•Logical Operators

&& (AND) || (OR) ! (NOT)

IT 103 Computer Programming 1


UNIT 2
LESSON 2 Basic Elements of a Program

• Expression
Arithmetic Expression

An Arithmetic expression is any expression that is used for


computations/mathematical manipulations.

Boolean Expression (Conditional Expression)

A Boolean expression is any expression that is either true


or false.

IT 103 Computer Programming 1

You might also like