C++ Programming Basics: Structure, Keywords, Data Types, and Statements, Slides of Introduction to Computers

A comprehensive introduction to the basics of c++ programming, covering topics such as program structure, preprocessor directives, main() function, c++ statements, variable rules, data types (int, float, double, char, and bool), and various keywords and statements. It also includes examples and explanations of const qualifier, define directive, input output stream, compound statements, increment and decrement operators, prefix and postfix operators, if and if-else statements, switch structure, and loops (while, do-while, and for).

Typology: Slides

2011/2012

Uploaded on 07/13/2012

qadir.ali
qadir.ali 🇵🇰

2 documents

1 / 50

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C++ basics
15 November 2011 Created by Tehreem Masood
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32

Partial preview of the text

Download C++ Programming Basics: Structure, Keywords, Data Types, and Statements and more Slides Introduction to Computers in PDF only on Docsity!

C++ basics

15 November 2011 Created by Tehreem Masooddocsity.com

Structure of C++ program

Preprocessor directives Main() function C++ statements

Preprocessor directives:

Instructions that are given to computer before the beginning of actual program. Also known as compiler directives Start with # sign and the keyword “include” and “define” They are used to include header files in the program

iostream

#include

Lines beginning with a hash sign (#) are directives for

the preprocessor. They are not regular code lines with

expressions but indications for the compiler's

preprocessor. In this case the directive #include

tells the preprocessor to include the

iostream standard file.

This specific file (iostream) includes the declarations of

the basic standard input-output library in C++, and it is

included because its functionality is going to be used

later in the program.

using namespace std;

All the elements of the standard C++ library are

declared within what is called a namespace, the

namespace with the name std. So in order to access

its functionality we declare with this expression that

we will be using these entities. This line is very

frequent in C++ programs that use the standard

library, and in fact it will be included in most of the

source codes.

Cont..

Keyword: The words that are used for special purposes Forexample Main, int, include Keywords cannot be used as variable names Variables: A quantity whose value may change during execution of the program It represents a storage or memory location in the computer memory Data is stored into the memory location Variable name remains fixed during the execution of the program but data stored in that location may change from time to time It is also known as object in C++ It consists of alphabets and digits

Rules for variable names

First character of variable name must be an alphabet character Underscore can be used as first character of variable name Blank spaces are not allowed in a variable name Special characters such as arithmetic operators #,$ cannot be used as variable names Reserved words cannot be used as variable names like cout A variable name declared for one data-type cannot be used to declare another data-type

INT DATA TYPE Represents integer data Used to declare integer type variables It is whole number, i.e. a number without a fraction or a decimal point o Forexample 45,-5, 56 are integers In MS DOS an integer type variable takes two bytes in the memory and range of values stored is

- 32768 to 32767  There are three qualifiers that can be applied to int type variables, these are: Short int Long int Unsigned int  Short int: Storage capacity is two bytes Range is - 32768 to 32767  Long int: Storage capacity is four bytes Range is - 2147483648 to 2147483647  Unsigned int: It can store only positive whole numbers Storage capacity is two bytes Store integer values from 0 to 65,535 docsity.com

FLOAT DATA TYPE It represents real floating type data Real type data is represented in decimal or exponential notation It may be signed or unsigned

o Forexample:

23.14, 12. Storage capacity is four bytes 3.410-^38 to 3.410+

Long float data type:

Its storage capacity is twice the storage capacity of float type variable which is 8 bytes

CHAR DATA TYPE

char stands for character It is used to declare character type variables In this data type alphabetic, numeric digits and special characters can be stored. Storage capacity for single character is 8 bit or 1 byte It can hold from 1 byte to 65535 bytes Arithmetic operations can also be performed on char type variables.

BOOL DATA TYPE

bool stands for Boolean It is used to declare logical type variables In it only two values true or false can be stored True is equivalent to 1 False is equivalent to 0

Output of program will be:

1

a

true

Const qualifier The data item that follows the keyword “const” cannot change its value during execution of the program A value is assigned to a data item at the time of its declaration Syntax: #include main() { const float pi= 3. int r=2; float a; a=2pir; cout<<a; } Output will be: 12.567 docsity.com

Input output stream

#include main() {

int a; cout<<”enter value”; cin>>a;

} Output will be: enter value

Note: After this you will type some integer value from your

keyboard

PRINTING OUTPUT TAKING INPUT

Compound statements

Compound assignment statement:

Assign one value to more than one variable

int x=y=z=0;

Compound assignment expression:

x+=9;

x=x+10;

x+=10;