Lecture 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Lecture 4

• Introduction To Computing
By Iqra Tahir
Programming

Program Programming
• A computer program, or a program, is a sequence • Programming is a process of planning and creating
of statements whose objective is to accomplish a a program
task.

Programming language: A set of rules, symbols, and


special words.
Given the length and width of a rectangle, write a C++ program that computes and outputs the perimeter and area of the
rectangle.
C++ Concepts
• A C++ program can contain various types of expressions such as arithmetic and strings. For example, length +
width is an arithmetic expression. Anything in double quotes is a string. For example, "Program to compute
and output the perimeter and " is a string. Similarly, the "area of a rectangle." is also a string. Typically, a
string evaluates to itself. Arithmetic expressions are evaluated according to rules of arithmetic operations,
which you typically learn in an arithmetic course. Later in this chapter, we will explain how arithmetic
expressions and strings are formed and evaluated.
• A C++ program is a collection of functions, one of which is the function main. Roughly speaking, a function is
a set of statements whose objective is to accomplish something. The preceding program consists of only the
function main; all C++ programs require the function main.
• C++ program is a collection of one or more subprograms which are called functions while function is a
collection of statements that are activated or executed and gives some output
• Prefix or standard functions are already written and are provided as part of the system.
• Every C11 program has a function called main. Thus, if a C11 program has only one function, it must be the
function main.
Continue..
• cout << "7 + 8 = "
• << 7 + 8 << endl;
• In this output statement, the expression "7 + 8 = ", which is a string, evaluates to itself.
• Let us consider the second expression, 7 + 8. This expression consists of the numbers 7 and 8, and the C++
arithmetic operator +.
• Therefore, the result of the expression 7 + 8 is the sum of 7 and 8, which is 15. Thus, the output of the
preceding statement is: 7 + 8 = 15
Syntax of C++
• Comments: The first four lines of the program begins with the pair of symbols // Comments are for the user;
they typically explain the purpose of the programs, the meaning of the statements.
• The next line is about #include allows us to use (the predefined object) cout to generate output and (the
manipulator) endl.
• The statement using namespace std; allows to use cout and endl without the prefix std. It means that if you
do not include this statement, then cout should be used as std::cout and endl should be used as std::endl.
• Next consider the following line: int main() This is the heading of the function main.
• The next line consists of a left brace {. This marks the beginning of the (body) of the function main. The right
brace } (at the last line of the program) matches this left brace and marks the end of the body of the
function main
• . Note that in C++, << is an operator, called the stream insertion operator.
Syntax Explanation
Comments in Program
• There are two common types of comments in a C++ program—
• single-line comments and multiple-line comments.
• Single-line comments begin with // and can be placed anywhere in the line. Everything encountered in that
line after // is ignored by the compiler. For example, consider the following statement: cout << "7 + 8 = " << 7
+ 8 << endl; You can put comments at the end of this line as follows: cout << "7 + 8 = " << 7 + 8 << endl;
//prints: 7 + 8 = 15 This comment could be meaningful for a beginning programmer.
• Multiple-line comments are enclosed between /* and */. The compiler ignores anything that appears
between /* and */. For example, the following is an example of a multiple-line comment:
/*
You can include comments that can occupy several lines.
*/
Special Symbols
• The smallest individual unit of a program written in any language is called a token.

• C++ tokens are divided into special symbols, word symbols, and identifiers. Following are some of the special
symbols:

• The first row includes mathematical symbols for addition, subtraction, multiplication, and division.
• The second row consists of punctuation marks taken from English grammar.
• Note that the comma is also a special symbol. In C++, commas are used to separate items in a list.
Semicolons are also special symbols and are used to end a C++ statement.
• The third row consists of tokens made up of two characters that are regarded as a single symbol. No
character can come between the two characters in the token, not even a blank.
Reserved Words (Keywords)
• A second category of tokens is reserved word symbols.

• Some of the reserved word symbols include the following: int, float, double, char, const, void, return
Identifiers
• Third category of tokens

• Names of things that appear in the program such as variables, functions

• Consists of letters, underscore, and characters (_) and must begin with an underscore or letter

• Some predefined are available some can be defined by the programmer


Data Types
• All ”variables” use data type during declaration to restrict the data storage type.
• Data types tell the variables the type of data they can store. Whenever a variable is defined in
C++, the compiler allocates some memory based on the data type with which it is declared.
• Every data type requires a different amount of memory.
• Data types specify the size and types of values to be stored
• C++ supports the following data types:
1. Primary or Built-in or Fundamental data type (built-in or predefined data types and can be used
directly by the user to declare variables.)
2. Derived data types(derived from the primitive or built-in datatypes are called Derived Data
Types.)
3. User-defined data types(are defined by the user itself. Like defining a class in C++ or a
structure. )
Primitive Data Types
• Integer: The keyword used for integer data types is int. Integers typically require 4 bytes of memory space and range from -2147483648 to
2147483647.
• Character: Character data type is used for storing characters. The keyword used for the character data type is char. Characters typically
require 1 byte of memory space and range from -128 to 127 or 0 to 255.
• Boolean: Boolean data type is used for storing Boolean or logical values. A Boolean variable can store either true or false. The keyword used
for the Boolean data type is bool.
• Floating Point: Floating Point data type is used for storing single-precision floating-point values or decimal values. The keyword used for the
floating-point data type is float. Float variables typically require 4 bytes of memory space.
• Double Floating Point: Double Floating Point data type is used for storing double-precision floating-point values or decimal values. The
keyword used for the double floating-point data type is double. Double variables typically require 8 bytes of memory space.
• void: Void means without any value. void data type represents a valueless entity. A void data type is used for those function which does not
return a value.
• Wide Character: Wide character data type is also a character data type but this data type has a size greater than the normal 8-bit data type.
Represented by wchar_t. It is generally 2 or 4 bytes long.
• sizeof() operator: sizeof() operator is used to find the number of bytes occupied by a variable/data type in computer memory.
Example of Primitive Data Types

/ C++ Program to Demonstrate the correct size


// of various data types on your computer.
#include <iostream>
using namespace std;

int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;

cout << "Size of long : " << sizeof(long) << endl;


cout << "Size of float : " << sizeof(float) << endl;

cout << "Size of double : " << sizeof(double) << endl;

return 0;
}
Output

Size of char : 1
Size of int : 4
Size of long : 8
Size of float : 4
Size of double : 8

You might also like