0% found this document useful (0 votes)
12 views

Lecture 01

The document outlines the fundamentals of programming in C++, including data types, variables, constants, input/output functions, and basic instructions. It discusses integer, floating point, and character constants, declaring and initializing variables, and using assignment statements and input statements to save and retrieve data values. The key aspects covered are C++ character set, output function cout, input function cin, and the hierarchy of operators and control instructions in C++.

Uploaded by

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

Lecture 01

The document outlines the fundamentals of programming in C++, including data types, variables, constants, input/output functions, and basic instructions. It discusses integer, floating point, and character constants, declaring and initializing variables, and using assignment statements and input statements to save and retrieve data values. The key aspects covered are C++ character set, output function cout, input function cin, and the hierarchy of operators and control instructions in C++.

Uploaded by

syednajafturab
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 78

Programming Fundamental

Today’s lecture outline


C++ character set
Basic constants and variables
Output function – Cout
Input function – Cin
C++ instructions
Integer to float conversion
Hierarchy of operator
Control instructions

2
Learning C/C++

3
C++ Character Set

4
Constants
An entity that doesn’t change

5
Integer Constants
Must have at least one digit
It must not have a decimal point
It can be either positive or negative
If there is no sign an integer constant is assumed to be positive
No commas or blanks are allowed within an integer constant
Examples: 135, -67, 3401, -5670

6
Real Constants
Also called Floating Point constants
A real constant must have at least one digit
It must have a decimal point
It could be either positive or negative
Default sign is positive
No commas or blanks are allowed within a real constant
Examples: +325.34, 426.0, -32.76, -48.5792

7
Character Constants
A character constant is a
single alphabet
a single digit
or a single special symbol
Enclosed within single inverted commas
Both the inverted commas should point to the left
The maximum length can be 1 character
Examples: ’A’, ‘I’, ‘5’, ‘=‘

8
Variables
An entity that may vary during program execution
Names given to locations in memory

x=3 x=5

9
Variable
In a program a variable has:
1. Name
2. Type
3. Size
4. Value

10
Data Types
1. int
2. short
3. long
4. float
5. double
6. char

11
Range of Integer data Type

12
Variables..
Series of characters (letters, digits, underscores)
Must begin with a letter or underscore
Case sensitive
Meaningful naming scheme

13
Variables..
No commas or blanks are allowed within a variable name.
No Special symbol are used in name.
Examples: Interger1, Sum, _FirstNum
Invalid variable names
#sum, 12x, first name

14
Keywords
Meaning already explained to compiler
Cannot be used as variable name

15
Writing C/C++ Programs
Instructions as statements
Order of statements
Each statement must end with a ;
Blanks for readability or clarity
Case-Sensitive

16
Structure of a C++ program
Source code Output
1.// my first program in C++
2.# include <iostream> Hello Word!
3.
4. int main ()
5. {
6. std::cout<<“Hello
Word!”;
7. }

17
Structure of a C++ program
Source code Output
#include<iostream>
using namespace std; Hello Word!

int main()
{
cout<<“Hello Word!”;
return 0;
}

18
Components of a C++ program (Cont.)
• Line 2: #include <iostream>
• Lines beginning with a sign
directives read and interpreted (#) are
hash
known as the preprocessor. by what
is
• In this case, the directive #include <iostream>,
instructs the preprocessor to include a section
of standard C++ code, known as header
iostream, that allows to perform standard input
and output operations.

19
Components of a C++ program (Cont.)
• Line 3: A blank line.
• Blank lines have no effect on a program. They
simply improve readability.

20
Components of a C++ program (Cont.)
• Line 4: int main ( )
• This line initiates the declaration of a function.
Essentially, a function is a group of code
statements which are given a name: in this
case, this gives the name "main" to the group
of code statements that follow.
• The execution of all C++ programs begins with
the main function regardless of where the
function is actually located within the code.

21
Components of a C++ program (Cont.)
• Line 6: std::cout << "Hello World!";
• This statement has three parts: First, std::cout,
which identifies the standard character output
device (usually, this is the computer screen).
• Second, the insertion operator (<<), which
indicates that what follows is inserted into
std::cout.
• Finally, a within quotes ("Hello
world!"), sentencecontent inserted into
standard output.
is the
the
22
The Basics of a C++ Program

• Function: collection of statements; when


executed, accomplishes something
− May be predefined or standard
• Syntax: rules that specify which
statements (instructions) are legal
• Programming language: a set of rules,
symbols, and special words
• Semantic rule: meaning of the
instruction
23
Comments
• Comments are for the reader, not the compiler
• Two types:
− Single line
// This is a C++ program. It prints the sentence:
// Welcome to C++ Programming.

− Multiple line
/*
You can include comments that can
occupy several lines.
*/

24
Special Symbols

• Special symbols (Operators)

+ ?
- ,
* <=
/ !=
. ==
; >=

25
Gross salary C++ Program

26
Arithmetic Operators and Operator
Precedence
• C++ arithmetic operators:
− + addition
− - subtraction
− * multiplication
− / division
− % modulus operator
• +, -, *, and / can be used with integral and
floating-point data types
• Operators can be unary or binary
27
Expressions

• If all operands are integers


− Expression is called an integral expression
• Yields an integral result
• Example: 2 + 3 * 5
• If all operands are floating-point
− Expression is called a floating-point
expression
• Yields a floating-point result
• Example: 12.8 * 17.5 - 34.50

28
Mixed Expressions

• Mixed expression:
− Has operands of different data types
− Contains integers and floating-point
• Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 /
2

29
Mixed Expressions (continued)

• Evaluation rules:
− If operator has same types of operands
• Evaluated according to the type of the
operands
− If operator has both types of operands
• Integer is changed to floating-point
• Operator is evaluated
• Result is floating-point
− Entire expression is evaluated according
to precedence rules
30
string
Type
• Programmer-defined type supplied in
ANSI/ISO Standard C++ library
• Sequence of zero or more
characters
• Enclosed in double quotation marks
• Null: a string with no characters
• Each character has relative position
in string
− Position of first character is 0
• Length of a string is number of
characters in it 31
Input

• Data must be loaded into main memory


before it can be manipulated
• Storing data in memory is a two-step
process:
− Instruct computer to allocate memory
− Include statements to put data into
memory

32
Declaring & Initializing Variables

• Variables can be initialized when declared:


int first=13, second=10;
char ch=' ';
double x=12.6;
• All variables must be initialized before they
are used
− But not necessarily during declaration

33
Putting Data into Variables

• Ways to place data into a variable:


− Use C++’s assignment statement
− Use input (read) statements

34
Allocating Memory with Constants
and Variables
• Variable: memory location whose content
may change during execution
• The syntax to declare a named constant
is:

35
Allocating Memory with Constants
and Variables (continued)
• Named constant: memory location whose
content can’t change during execution
• The syntax to declare a named constant
is:

• In C++, const is a reserved word

36
Assignment Statement

• The assignment statement takes the form:

• Expression is evaluated and its value is


assigned to the variable on the left
side
• In C++, = is called the assignment
operator

37
Assignment Statement (continued)

38
Saving and Using the Value of an
Expression
• To save the value of an expression:
− Declare a variable of the appropriate data type
− Assign the value of the expression to
the variable that was declared
• Use the assignment statement
• Wherever the value of the expression is
needed, use the variable holding the value

39
Input (Read) Statement

• cin is used with >> to gather input

• The stream extraction operator is >>


• For example, if miles is a double variable
cin >> miles;
− Causes computer to get a value of type
double
− Places it in the variable miles

40
Input (Read) Statement (continued)

• Using more than one variable in cin


allows more than one value to be read at a
time
• For example, if feet and inches are
variables of type int, a statement such as:
cin >> feet >> inches;
− Inputs two integers from the keyboard
− Places them in variables feet and
inches
respectively
41
Input (Read) Statement (continued)

42
Variable Initialization

• There are two ways to initialize a variable:


int feet;
− By using the assignment statement
feet = 35;
− By using a read statement
cin >> feet;

43
Increment & Decrement Operators

• Increment operator: increment variable by 1


− Pre-increment: ++variable
− Post-increment: variable++
• Decrement operator: decrement variable by 1
− Pre-decrement: --variable
− Post-decrement: variable—
• What is the difference between the following?
x = 5; x = 5;
y = ++x; y = x++;

44
Output
 The syntax of cout and << is:

− Called an output statement


 The stream insertion operator is <<
 Expression evaluated and its value is printed at the current cursor
position on the screen

45
Output (continued)

• A manipulator is used to format the output


− Example: endl causes insertion point to
move to beginning of next line

46
Output (continued)

• The new line character is '\n'


− May appear anywhere in the string
cout << "Hello there.";
cout << "My name is James.";
• Output:
Hello there.My name is James.
cout << "Hello there.\n";
cout << "My name is James.";
• Output :
Hello there.
My name is James.

47
Output (continued)

48
Variable declaration
Any variable used in the program must be declared first before using it.

49
Output function

cout<<“This is my First Program”;


Cout<<a;
Cout<<10;
Cout<<a+b;

50
Input
To make gross salary calculation program general, the program ask the
user to input value of hours and payrate during execution

“ cin” function is used to input value from user during program
execution

51
Gross salary C Program

52
Input function syntax
Cin>> payrate;
Cin>> hours;

53
C++ Instructions
Type Declaration Instruction
To declare the type of variables used in a C++ program.

Arithmetic Instruction
To perform arithmetic operations between constants and variables

Control Instruction
To control the sequence of execution of various statements in a C+
+ program.

54
Type Declaration Instruction
This instruction is used to declare the type of variables being used in the
program.
The type declaration statement is written at the beginning of main( )
function.
 int hours, payrate, grosspay;
 float salary;
 char name;

55
Variations of type declaration instruction
 Initializing at declare time
int x =10, y = 20;
float a = 1.25, b = 1.99*2.4+1.5;
 Order of variable decleration
int i = 10, j = 25 ;
is same as
int j = 25, j = 10 ;
However,
float a = 1.5, b = a + 3.1 ;
is alright, but
float b = a + 3.1, a = 1.5 ;

56
Cont.
The following statements would work
int a, b, c, d ;
a = b = c = 10 ;
However, the following statement would not work
int a = b = c = d = 10 ;

57
Arithmetic Instruction
C++ arithmetic instruction consists of
variable name on the left hand side of =
and variable names & constants on the right hand side of =.


The variables and constants appearing on the right hand side of = are
connected by arithmetic operators like +, -, *, and /.

58
Example
int ad ;
float kot, deta, alpha, beta, gamma ;
ad = 3200 ;
kot = 0.0056 ;
deta = alpha * beta / gamma + 3.2 * 2 / 5 ;
The variables and constants together are called ‘operands’
that are operated upon by the ‘arithmetic operators’

59
Types of arithmetic statement
Integer mode arithmetic statement
This is an arithmetic statement in which all operands are
either integer variables or integer constants.
Example:
 int i, king, issac, noteit ;
i = i + 1 ;
 king = issac * 234 + noteit - 7689 ;

60
Cont.
Real mode arithmetic statement
This is an arithmetic statement in which all operands are either real constants
or real variables.
Example.
float x, y, si, prin, anoy, roi ;
x = y + 23.123 / 4.5 * 0.3442 ;
si = prin * anoy * roi / 100.0 ;

61
Cont..
Mixed mode arithmetic statement
This is an arithmetic statement in which some of the operands are integers and
some of the operands are real.
Example.
float si, prin, anoy, roi, avg ;
int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;

62
Arithmetic instructions
C++ allows only one variable on left-hand side
Modular operator
This operator returns the remainder on dividing one integer with another
Expression 10 / 2 yields 5 whereas, 10 % 2 yields 0
Expression 10 / 3 yields 3 whereas 10 % 3 yields 1

 An arithmetic instruction is often used for storing character constants


in character variables.
char a, b, ;
a = 'F' ;
b = 'G' ;
63
Cont.
Arithmetic operations can be performed on ints, floats and chars.
char x, y ;
int z ;
x = 'a' ;
y = 'b' ;
z=x+y;

64
Cont..
No operator is assumed to be present. It must be written explicitly.
a = c.d.b(xy)

b=c*d*b*(x*y)

65
Integer and float conversions
An arithmetic operation between an integer and integer always yields an integer
result
An operation between a real and real always yields a real result
An operation between an integer and real always yields a real result

66
Type Conversion in Assignments
k is int and a is float

67
Hierarchy of Operations

68
Example
i=2*3/4+4/4+8-2+5/8
i=6/4+4/4+8-2+5/8 operation: *
i=1+4/4+8-2+5/8 operation: /
i = 1 + 1+ 8 - 2 + 5 / 8 operation: /
i=1+1+8-2+0 operation: /
i=2+8-2+0 operation: +
i = 10 - 2 + 0 operation: +
i=8+0 operation : -
i=8 operation: +

69
Association of Operators
When an expression contains two operators of equal priority the tie
between them is settled using the associativity of the operators
It can be of two types
Left to right
Right to left

70
Example
Consider the expression
a = 3 / 2 * 5 ;
Left to right:
a = 3 / 2 * 5
a = 1 * 5
a = 5

Right to left :
 a= 3/2*5
 a = 3 / 10
 a=0

71
Control Instructions in C++
Enable us to specify the order in which the various instructions in a
program are to be executed by the computer.
Sequence Control Instruction
Selection or Decision Control Instruction
Repetition or Loop Control Instruction
Case Control Instruction

72
Cont.
Sequence Control Instruction
The Sequence control instruction ensures that the instructions are executed in
the same order in which they appear in the program.

73
Cont..
Selection or Decision Control Instruction
Decision instructions allow the computer to take a decision as to which
instruction is to be executed next.
Repetition or Loop Control Instruction
The Loop control instruction helps computer to execute a group of statements
repeatedly.

74
Cont…
Case Control Instruction
same as decision control instruction.

75
Which are invalid variables
BASICSALARY
_basic
basic-hra
#MEAN
group.
422
population in 2006
over time

76
Point out the errors

int = 314.562 * 150 ;


Char name = ‘Ajay’ ;
char = ‘3’ ;
3.14 * r * r * h = vol_of_cyl ;
k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ;
m_inst = rate of interest * amount in rs ;
count = count + 1 ;

77
Thank You
78

You might also like