0% found this document useful (0 votes)
8 views118 pages

Programming Fundamental 4 Lec

The document outlines the fundamental concepts of C++ programming, covering topics such as basic elements, data types, syntax rules, and arithmetic operations. It emphasizes the importance of understanding functions, variables, and the structure of a C++ program while providing guidance on how to learn programming effectively. Additionally, it discusses the significance of comments, identifiers, and the various types of data, including integral and floating-point types.

Uploaded by

cyberexpert997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views118 pages

Programming Fundamental 4 Lec

The document outlines the fundamental concepts of C++ programming, covering topics such as basic elements, data types, syntax rules, and arithmetic operations. It emphasizes the importance of understanding functions, variables, and the structure of a C++ program while providing guidance on how to learn programming effectively. Additionally, it discusses the significance of comments, identifiers, and the various types of data, including integral and floating-point types.

Uploaded by

cyberexpert997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 118

Programming

Fundamental
s
Week 2 Day 1

Instructor: Noor Ullah Khan


Chap 2: Basic
Elements of C+
+
Become familiar
with functions, Explore Simple
Special Symbols data types
and identifiers.

Today’s How to use


arithmetic
Evaluate
arithmetic
Agenda operators? expressions.

Assignments
String data type statement and
what it does?
Previously

• A computer program, or a program, is a sequence of


statements whose objective is to accomplish a task.
• Programming is a process of planning and creating a program.
How to learn
programming/coding?

• Learning a programming language is like learning to become a


chef or learning to play a musical instrument.
• All three require direct interaction with the tools.
• You cannot become a good chef just by reading recipes.
• Similarly, you cannot become a musician by reading books
about musical instruments.
How to learn
programming/coding?

• You must have a fundamental knowledge of the language.


• you must test your programs on the computer to make sure
that each program does what it is supposed to do.
A quick look at a c++ program
Various parts of a c++
program
Basics of a c++ Program
What is variable?
What is variable?
Syntax rules

• If you have never seen a program written in a programming


language, the C++ program may look like a foreign language.
• To make meaningful sentences in a foreign language, you
must learn its alphabet, words, and grammar.
• The same is true of a programming language.
• To write meaningful programs, you must learn the
programming language’s special symbols, words, and syntax
rules.
Syntax rules

• The syntax rules tell you which statements (instructions) are


legal or valid, that is, which are accepted by the programming
language and which are not
Programming Language

• A set of of rules, symbols and special words.


Comments

• The program that you write should be clear not only to you, but
also to the reader of your program.
• Part of good programming is the inclusion of comments in the
program.
Comments
Comments

• There are two types of comments:


• Single-line comments
• Double-line comments
Comments
Special Symbols

• The smallest individual unit of a program written in any


language is called a token.
• C++’s tokens are divided into special symbols, word
symbols, and identifiers.
Special Symbols
Special Symbols

• Tokens made up of two characters are regarded as a single


symbol.
• Blank is a special symbol.
Reserved words(Keywords)

• A second category of tokens is reserved word symbols.


• Example: int, float, double, char, const, void, return
• Reserved words are also called keywords.
• Imp: The letters that make up a reserved word are always
lowercase.
Reserved words(keywords)

• Reserved words cannot be redefined within any program.


• They cannot be used for anything other than their intended use
Identifiers

• A third category of tokens is identifiers.


• Identifiers are names of things that appear in programs, such
as variables, constants, and functions.
• All identifiers must obey C++’s rules for identifiers.
Identifier Rules

• A C++ identifier consists of letters, digits, and the underscore


character (_).
• It must begin with a letter or underscore.
• Identifiers can be made of only letters, digits, and the
underscore character; no other
• Symbols are permitted to form an identifier.
Identifier Rules

• C++ is case sensitive—uppercase and lowercase letters are


considered different.
• Thus, the identifier NUMBER is not the same as the identifier
number.
• Similarly, the identifiers X and x are different.
Identifiers

• Some identifiers are predefined; others are defined by the user.


• Examples for predefined are: cout, cin etc.
• Examples of user-defined are: length, height etc.
Identifiers
Identifiers
Whitespaces

• Every C++ program contains whitespaces.


• Whitespaces include blanks, tabs, and newline characters.
• Whitespaces are nonprintable in the sense that when they are
printed on a white sheet of paper, the space between special
symbols, reserved words, and identifiers is white.
Data type

• The objective of a C++ program is to manipulate data.


• A program designed to calculate an employee’s paycheck will
add, subtract, multiply, and divide numbers.
• Some of the numbers might represent hours worked and pay
rate.
• Similarly, you wouldn’t use a program designed to perform
arithmetic calculations to manipulate alphabetic characters.
Data Type

• Reflecting these kinds of underlying differences, C++


categorizes data into different types.
• Only certain operations can be performed on particular types
of data.
Data Type

• A set of values together with a set of allowed operations.


• C++ data types fall into the following three categories:
• Simple Data Type
• Structured Data Type
• Pointers

• For now, on we will only be concerned about Simple Data


type
Simple Data Type

• The simple data type is the fundamental data type in C++.


• There are three categories of simple data:
• Integral, which is a data type that deals with integers, or numbers
without a decimal part.
• Floating point, which is a data type that deals with decimal numbers.
• Enumeration, which is a user-defined data type.
Integral Data Types

• Integral data types are further classified into the following


categories:
• char, short, int, long, bool, unsigned char, unsigned short, unsigned int,
unsigned long, long long, and unsigned long long.
Integral Data Types

• Why are there so many categories of the same data type?


• Every data type has a different set of values associated with it.

• char data type is used to represent integers between -128


and 127.
• The int data type is used to represent integers between 
-2147483648 and 2147483647.
• The data type short is used to represent integers between 
-32768 and 32767
Integral Data Types

• Which data type you use, depends on how big a number your
program needs to deal with.
Integral Data Types
int Data Type

• Integers in C++, as in mathematics, are numbers such as the


following:
• -6728, -67, 0, 78, 36782, +763

• Note the following two rules from these examples:


• Positive integers do not need a + sign in front of them.
• No commas are used within an integer. Recall that in C11, commas are
used to separate items in a list.
• So, 36,782 would be interpreted as two integers: 36 and 782.
bool Data Type

• The data type bool has only two values: true and false.
• Also, true and false are called the logical (Boolean) values.
• In C++, bool, true, and false are reserved words.
char Data Type

• The data type char is mainly used to represent single


characters—that is, letters, digits, and special symbols.
• Thus, the char data type can represent every key on your
keyboard.
• When using the char data type, you enclose each character
represented within single quotation marks.
• Example:
• 'A’, 'a’, ‘0’, '*’, '+’, '$’, '&’, ' '
char Data Type

• Note that a blank space is a character and is written as ‘


’, with a space between the single quotation marks.
• The data type char allows only one symbol to be placed
between the single quotation marks.
• Thus, the value 'abc' is not of the type char.
Floating-Point Data Type

• To deal with decimal numbers, C++ provides the floating-point


data type.
Floating-Point Data Type
Floating-Point Data Type

• To represent decimal numbers, C++ uses a form of scientific


notation called floating-point notation.
Floating-Point Data Type
Floating-Point Data Type

• C++ provides three data types to manipulate decimal


numbers: float, double, and long double.
float

• The data type float is used in C++ to represent any decimal


number between -3.4 * 1038 and 3.4 * 1038.
• The memory allocated for a value of the float data type is four
bytes.
double

• The data type double is used in C++ to represent any decimal


number between -1.7 * 10308 and 1.7 * 10308.
• The memory allocated for a value of the double data type is
eight bytes.
Data Types, Variables,
and Assignment
Statements
How to declare a variable?

• When we declare a variable.


• We specify the name of the variable.
• We also specify what type of data a variable can store.
Syntax of declaring a variable
How to store a value in a variable?

• Using assignment operator.


• Syntax:
• variable = expression

• Where expression is evaluated, and its value is assigned to


variable. In C++, = is called the assignment operator.
How to store a value in a variable?
Arithmetic Operators, Operator Precedence,
and Expressions

• One of the most important uses of a computer is its ability to


calculate.
• You can use the standard arithmetic operators to manipulate
integral and floating-point data types.
Arithmetic Operators, Operator Precedence,
and Expressions

• There are five arithmetic operations:


• + (addition)
• - (subtraction or negation)
• * (multiplication)
• / (division)
• % (mod, (modulus or remainder))
Arithmetic Operators, Operator Precedence,
and Expressions
Arithmetic Operators,
Operator Precedence, and
Expressions
Arithmetic Operators, Operator Precedence,
and Expressions

• What is operand?
• What is operator?
• Unary operand
• Binary Operator
• Arithmetic expressions.
Operator Precedence

• same level of precedence.


•3*7-6+2*5/4+6
• Operator associativity.
Operator Precedence
How arithmetic operator works?
How arithmetic operator works?
Expresssions

• There are three types of arithmetic expressions in C++:


• Integral expressions—all operands in the expression are
integers.
• An integral expression yields an integral result.
• Floating-point (decimal) expressions—all operands in the
expression are floating points (decimal numbers). A floating-
point expression yields a floating-point result.
Expressions

• Mixed expressions—the expression contains both integers


and decimal numbers.
Integral Expressions
Floating-point Expressions
Mixed Expressions
Apply when evaluating a mixed
expression

• If the operator has the same types of operands (that is, either
both integers or both floating-point numbers), the operator is
evaluated according to the type of operands.
• Integer operands thus yield an integer result; floating-point
numbers yield a floating-point number.
Apply when evaluating a mixed
expression

• If the operator has both types of operands (that is, one is an


integer and the other is a floating-point number), then during
calculation, the integer is changed to a floating-point number
with the decimal part of zero and the operator is evaluated.
• The result is a floating-point number.
Apply when evaluating a mixed
expression

• The entire expression is evaluated according to the precedence


rules; the multiplication, division, and modulus operators are
evaluated before the addition and subtraction operators.
• Operators having the same level of precedence are evaluated
from left to right.
• Grouping using parentheses is allowed for clarity.
Apply when evaluating a mixed
expression
Apply when evaluating a mixed
expression
Variables, Assignment
Statements,
and Input Statements
Assignment

• Storing data in the computer’s memory is a two-step process:


• Instruct the computer to allocate memory.
• Include statements in the program to put data into the
allocated memory.
NAMED CONSTANTS

• A memory location whose content is not allowed to change


during program execution.
NAMED CONSTANTS
NAMED CONSTANTS
NAMED CONSTANTS

• Moreover, if the name of a named constant is a combination of


more than one word, called a run-together word, then the
words are typically separated using an underscore.
VARIABLES
VARIABLES
VARIABLES

• If a variable name is a combination of more than one word,


then the first letter of each word, except the first word, is
uppercase.
• (For example, see the variable amountDue in the preceding
example.)
Putting Data into Variables

• In C++, you can place data into a variable in two ways:


• Use C++’s assignment statement.
• Use input (read) statements.
Putting Data into Variables
Putting Data into Variables
Putting Data into Variables
Putting Data into Variables
Putting Data into Variables
Putting Data into Variables
Saving and Using the Value of an
Expression

• To save the value of an expression and use it in a later


expression, do the following:
• Declare a variable of the appropriate data type. For example, if
the result of the expression is an integer, declare an int
variable.
• Assign the value of the expression to the variable that was
declared, using the assignment statement. This action saves
the value of the expression into the variable.
Saving and Using the Value of an
Expression

• Wherever the value of the expression is needed, use the


variable holding the value.
Saving and Using the Value of an
Expression
Declaring and Initializing
Variables
Input (Read) Statement
Input (Read) Statement
Input (Read) Statement
Input (Read) Statement
Variable Initialization
Variable Initialization
Variable Initialization
Variable Initialization
Increment and Decrement
Operators
Output
Output
C++ program Skeleton
Types of statements

• Declaration statements:
• Declaration statements are used to declare things, such as variables.

• Executable statements
• Executable statements perform calculations, manipulate data, create
output, accept input, and so on.
Declaration Statements
Executable statements
Debugging: Understanding and Fixing
Syntax Errors
Debugging: Understanding and Fixing
Syntax Errors

You might also like