0% found this document useful (0 votes)
73 views36 pages

Computer Programming in C++: (PRACTICAL#03)

The document discusses the basics of C++ programming including: - The structure of a basic C++ program including main functions, return statements, and input/output statements. - The differences between source code, object code, and machine code. Source code is human readable while object code is machine readable. - Language translators like compilers that convert source code to object code so programs can run on computers. - Primitive data types in C++ like integers, floats, characters and how to declare and initialize variables of these types.
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)
73 views36 pages

Computer Programming in C++: (PRACTICAL#03)

The document discusses the basics of C++ programming including: - The structure of a basic C++ program including main functions, return statements, and input/output statements. - The differences between source code, object code, and machine code. Source code is human readable while object code is machine readable. - Language translators like compilers that convert source code to object code so programs can run on computers. - Primitive data types in C++ like integers, floats, characters and how to declare and initialize variables of these types.
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/ 36

Computer Programming

in
C++
(PRACTICAL#03)
 Objective: To become familiar with the basic C++
program structure, source code and object code,
language translators, primitive data types,
identifiers and escape sequence.
 Program statements /Program instructions and Program
 Syntax
 Source code and object code , source file and object file
 Executable file
 Language Translators (Interpreter, Compiler, Assembler)
 Data Types
 Escape Sequence

Introduction to C++

 Program statements /Program instructions


 A C++ program consists of a set of statements.
 A Computer program is a sequence of instructions, written to perform a
specified task with a computer.
 A computer program is a list of instructions that tell a computer what to do.
 Instruction is any command given to the computer.
 For example:
 1)Add two variables A and B int SUM=A+B;
 2)Display result Cout <<“Hello World.” ;
 3)Read file
 Each of these is the individual instruction to the computer.
Introduction to C++

Do this

cout << “Batch 17SW ”;


Introduction to C++

 Program: is a set(collection) of instruction to do a meaningful task.


 Present Simple Tense
 Structure of Sentence: 1st form of the verb will be used as a main verb in the sentence


Introduction to C++

 Syntax: the syntax of a computer language is the set of rules


that defines the combinations of symbols that are considered to
be a correctly structured document or fragment in that language
 Syntax refers to the spelling and grammar of a 
programming language. 
 Computers are inflexible machines that understand what you
type only if you type it in the exact form that the computer
expects. The expected form is called the syntax.


Anatomy of C++ basic program

 #include<iostream>
 #include<conio.h>
 int main()
{
 cout<<“17 Software ";
 getch();
 return 0;
}
Anatomy of C++ basic program
Anatomy of C++ basic program
main() function
 The main is the name of the function.
 Every C++ program must contain at least one function i.e. main()
function.
 Main function is the gateway of any C++ program because every
program starts from the main function.
 Every function name is followed by () parenthesis.
 The int is the return type of the function, which specifies that, at the
end, the main function will return one value to the operating system
whose data type will be integer.
Outputting with cout
 In C++, cout is Standard Output Stream, cout statement is used to
print/display the text or numbers on the monitor screen.

 cout is used in conjunction with the insertion operator(<<).


 Any thing on the right side of << will be displayed on monitor screen.
 “17SW” is the string literal / constant to be displayed.
 ;(semicolon)is called as terminator, it shows the end of any
statement.
 
getch() function
 In order to view the output we used getch() function.
 The getch() function is used to get one character from the keyboard.
 Whenever the computer encounters getch() function, it will wait for
the user to press any key from the keyboard.
 Once the user presses any key on the keyboard that will be get by the
getch() function.
return statement

 The return statement is used to return the control to another


portion in the program or to the operating system.
 In this program, the return statement returns the value from the
main function to the operating system.

 When a Zero is returned to the operating system, it assumes that
the program has executed Normally(Successfully).
Source code and object code

 The set of instructions written in any language other than machine


language is called as source code.
 It is not directly understood by the machine (computer).
 It is in the form of text.
 It is human readable.
 It is generated by programmer.
 It is input to the language translator.
Source code and object code

 Object Code Computer only understands object code(machine


code).
 The set of instructions written in machine language is called as object code. It is also
known as machine code.
 It is the only code which is directly understood by the machine (computer).
 It is in the form of binary numbers.
 It is machine (computer) readable.
 It is generated by the language translator.
 It is the output of the language translator.
Source code and object code
Language Translators
 Computer only understands
object code(machine code).
 It does not understand any source code.
 There must be a program that converts
source code into the object code so that
the computer can understand it.
 The language translator is one which
does this job.
 Language translator is a program that converts
The source code into the object code.
Machine Language

 Machine languages are the only languages directly understood by the


computers.
 It is the native language of the machines (computers).
 It is the language of 0s and 1s. example 10010001
 Here all the instructions are written as code of binary sequence.
 While easily understood by computers, machine languages are almost
impossible for humans to use because they consist entirely of numbers
(0sand1s).
Language Translators

 Language translator is a program that converts the source code into the
object code.
Language Translators (Compiler)

 Compiler is the language translator that converts high level language


code in to the object code (machine code).
 It converts the whole code at a time.
Compiler
Data Types

• Primitive data types

• int • float
• short • double
• long • boolean
• char
Integer data Types

 Variable: A variable is a space in the computer’s memory set a side for a certain kind of data
and gives it a name for easy reference.
 Integers : store whole number, integers have no fractional part. The value can be –ve or +ve.
 There are several integer types.
Data Type Description
int Variables of this type occupy 4 bytes (32 bits) in memory
and can have values from -2147483648 to 2147483647

short Variables of this type occupy 2 bytes (16 bits) in memory


and can have values from -32768 to 32767 and

long Variables of this type can have values from


-9223372036854775808 to 9223372036854775807 and
Variable Declaration Definition and
Initialization
 Variable: A variable is a space in the computer’s memory set a side for a certain kind of data
and gives it a name for easy reference.
 A declaration introduces a variable’s name into a program and specifies its type.
 However, if a declaration also sets aside memory for the variable, it is called definition.
 Example

int var1;
int var2;
Variable Declaration Definition and
Initialization
 variable Initialization: To combine a variable declaration with an assignment
operator so that a variable is given a value at the same time.
 Example Assignment statement int var1 = 5 ;
Identifiers : the names given to variables are called identifiers.
Short

 short : short is a 16-bit type. It has a range from –32,768 to 32,767.


 examples of short variable declarations:
 short s;
 short t;
Int & long

 The most commonly used integer type is int. It is a signed 32-bit type that has a range from –2,147,483,648
to 2,147,483,647.
 In addition to other uses, variables of type int are commonly employed to control loops and to index arrays.
Although you might think that using a byte or short would be more efficient than using an int in situations in
which the larger range of an int is not needed.

 long : is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold
the desired value. The range of a long is quite large.
 This makes it useful when big, whole numbers are needed.
 long milliseconds;
 long population;
Integer Data Types Example
Program
 int main(){
 int numberOfClasses, studsPerClass, totalStudents;
 numberOfClasses = 10;
 studsPerClass = 6;
 totalStudents = numberOfClasses * studsPerClass;
 cout<< studsPerClass <<" students Per Class and";
 cout<<number Of Classes<< " Clsses, then");
 cout<<"the total number of students:" + totalStudents;
 getch();
 Return 0;
Floating Point Data Types

 Float: variables represent numbers with a decimal places.


 Double: They are similar to float except they require more memory
space and provide wide range of values.
Data Type Description
float Variables of this type can have values from
-3.4E38 to +3.4E38 and occupy 4 bytes in
memory.

double Variables of this type can have values from


-1.7E308 to +1.7E308 and occupy 8 bytes in
memory
Floating-point Data Types Example
Program
 // Compute the area of a circle.
 int main() {
 double pi, r, a;
r = 10.8; // radius of circle
 pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
 Cout<<"Area of circle is “<< a;
}
Characters

 char is a 8-bit type. The range of a char is -128 to 127.


 Internally characters are stored as numbers. There are commonly used to store ASCII characters.
 An ASCII character set is a way of representing characters such as ‘a’, ‘B’;
 CharacterExample Program
 // Demonstrate
 int void main() {
 char ch1, ch2;
 ch1 = 65; // code for A
 ch2 = 'Y';
 Cout<<"ch1 and ch2: ";
 Cout<<ch1 << ch2);
 }
Booleans

 C++ has a primitive type, called boolean, for logical values.


 It can have only one of two possible values, true or false.
 It takes only one bit.
 This is the type returned by all relational operators, as in the case of a < b.
 Booleans Example Program
 int main(){
 bool b=true, c=false;
 cout<<"b = "+b;
 cout<<"c = "+c;
 cout<<"b is equal to c: “<< (b==c) ;
 cout<<"b is NOT equal to c: “<< (b!=c) );

 }
Escape Sequences

 Are special characters , they don’t appear on the screen rather their effect can be seen.
 Escape Sequence Description
 \n Newline. Position the screen cursor to the beginning of the next line.
 \t Horizontal tab. Move the screen cursor to the next tab stop.
 \r Carriage return. Position the screen cursor to the beginning of the current line;
do not advance to the next line. Any characters output after the carriage
return overwrite the characters previously output on that line.
 \\ Backslash. Used to print a backslash character.
 \’ Single quote.
 \" Double quote. Used to print a double-quote character. For example,
 Cout<<"\"in quotes\"" ; // displays
 "in quotes"
Input with cin>>

 cin is a predefined object in c++.


 Corresponds to the standard input stream.
 >> is extraction or get from operator
Tasks for Lab#3
Task #1
Write a program in which declare some variables with
valid identifiers and conventions rule, to hold your total
marks in previous semester, percentage, your grade etc,
assign them explicitly and print them. Try to declare
variables of all data types and assign the appropriate
values.
Task # 2
Write a C++ program that accepts the base and height
of a right angle triangle from the user and displays the
area of the triangle.

Task #3
Write a program in C++ that asks the user to enter mass
in Kilograms and converts into grams and displays on
the monitor screen.

You might also like