3 intro basic_elements

22
Basic Elements of C++ 05/16/22 1 VIT - SCSE By G.SasiKumar., M.E., (Ph.D)., Assistant Professor School of Computing Science and Engineering VIT University

Upload: vit-university

Post on 05-Dec-2014

602 views

Category:

Technology


0 download

DESCRIPTION

Basic_elements

TRANSCRIPT

Page 1: 3 intro basic_elements

Basic Elements of C++

04/10/231 VIT - SCSE

By

G.SasiKumar., M.E., (Ph.D).,Assistant Professor

School of Computing Science and EngineeringVIT University

Page 2: 3 intro basic_elements

Characters are used to form the words, numbers and expressions.

 alphabets from A.....Z, a....zall decimal digits from 0....9characters , . ; : ?

‘ “ ! | / \ ~ _ $ % # & ^* - + < > ( ) [ ] { } SpacesblankNew Line endl

04/10/232 VIT - SCSE

Character Set

Page 3: 3 intro basic_elements

Keywords and IdentifiersAll keywords must be written in lowercase.

auto double int struct breakelse long switch case enum registerTypedef char extern return unionconst float short unsigned continuefor signed void default gotosizeof volatile do if static while

Identifiers refer to the names of variables, functions and arrays.

These are user -defined names and consist of a sequence of letters and digits.

04/10/233 VIT - SCSE

Page 4: 3 intro basic_elements

Data Types

04/10/234 VIT - SCSE

C++ Data Types

User Defined type

StructureUnionClassEnumeration

Built-in type Derived type

ArrayFunctionPointer

Integral Floating Character

Void

byte short int longfloat double

Page 5: 3 intro basic_elements

ConstantsConstants in C++ refer to fixed values that do

not change during the execution of a program.C++ supports several types of constants as

shown below: -1. Numeric Constants

Integer ConstantsFloating point (real) Constants

2. Character ConstantsSingle Character ConstantsString Constants

04/10/235 VIT - SCSE

Page 6: 3 intro basic_elements

Integer Constants Decimal integers consist of a set of digits

from 0 through 9. The decimal integers can be positive or negative.(base 10)

Octal integers consist of any combination of digits from 0 through 7, with a leading zero.(base 8)

Hexadecimal integers consist of a sequence of digits 0 through 9 and alphabets from A (a) through F (f). The letters 'A' (a) through 'F ' (f) represent the integers 10 through 15.(16)

04/10/236 VIT - SCSE

Page 7: 3 intro basic_elements

Floating-Point Constants

Floating-point constants are represented by numbers containing fractional parts like in 549.4545.

Floating-point constants are also sometimes called as real number constants.

04/10/237 VIT - SCSE

Page 8: 3 intro basic_elements

Single Character ConstantsA character constant is a single character enclosed

within a pair of single quotes.Example:

'A''3''?'';'' '

Constant ASCII value'a' 97'A' 65'&' 38';' 59

04/10/238 VIT - SCSE

Page 9: 3 intro basic_elements

String Constants A string constant is a sequence of

characters enclosed within a pair of double quotes.

The string constant may also include special characters, numbers and blank spaces.

Example:" Hello!"" I'm going for shopping today. Will you

come?"" 549, The Mall, Shimla."

04/10/239 VIT - SCSE

Page 10: 3 intro basic_elements

Escape SequencesCharacter Escape sequence

bell \abackspace \bhorizontal tab \tvertical tab \vnewline \ncarriage return \rquotation mark \"apostrophe \'backslash \\null \0

04/10/2310 VIT - SCSE

Page 11: 3 intro basic_elements

VariablesA variable is an identifier that is used to

represent a single data itemDeclarationDefinition

04/10/2311 VIT - SCSE

Page 12: 3 intro basic_elements

OperatorsAn operator is a symbol that tells the compiler to

perform specific mathematical or logical manipulations.

Arithmetic OperatorsoIncrement and Decrement Operators

Relational and Logical OperatorsAssignment OperatorsTernary(Conditional) OperatorSizeof OperatorComma Operator& Operator -> Operators(a->b -Member b of object pointed to by a )

04/10/2312 VIT - SCSE

Page 13: 3 intro basic_elements

Control StatementsConditional Statements

If statementIf-else statementSwitch-case statement

Loop StatementsFor loopWhile loopDo-while loop

Breaking control statementsBreakContinuegoto

04/10/2313 VIT - SCSE

Page 14: 3 intro basic_elements

Switch statementswitch(expression){ case constant_1:

statement;break;case constant_2:statement;break;-----------------case constant_n:statement,break;default:statement;break;}

04/10/2314 VIT - SCSE

Page 15: 3 intro basic_elements

//A program to display the name of the day in a week, depending upon the number entered through the keyboard using the switch-case statement.

04/10/2315 VIT - SCSE

#include<iostream.h>void main(){int day;cout<<”Enter a number between 1 to 7”<<endl;cin>>day;switch(day){case 1:cout<<”Monday”<<endl;break;case 2:cout<<”Tuesday”<<endl;break;

case 3:cout<<”Wednesday”<<endl;break;case 4:cout<<”Thursday”<<endl;break;case 5:cout<<”Friday”<<endl;break;case 6:cout<<”Saturday”<<endl;break;case 7:cout<<”Sunday”<<endl;break;}}

Page 16: 3 intro basic_elements

For statement//A program to find the sum and average of given numbers.

04/10/2316 VIT - SCSE

#include<iostream.h>void main(){int n;cout<<”How many numbers?”<<endl;cin>>n;float sum=0;float a=0;float a;

for(int i=0; i<=n-1;i++){cout<<”Enter a number”<<endl;cin>>a;sum=sum+a;}float av;av=sum/n;cout<<”sum=”<<sum<<endl;cout<<”Average=”<<av<<endl;}

Page 17: 3 intro basic_elements

Nested For StatementA program to display a name 15 times using the nested for

loops.#include<iostream.h>void main(){cout<<”Nested for loop”;for(int i=0; i<=2; i++){cout<<i; for(int j=0;j<=2;j++){cout<<j;for(int k=0;k<=2;k++){cout<<k;}}}}

04/10/2317 VIT - SCSE

Page 18: 3 intro basic_elements

While statement{sum+=digit;digit++;}cout<<“1+2+3+.....

+100=“<<sum<<endl;

}

04/10/2318 VIT - SCSE

//program to find the sum of the first 100 natural numbers

#include<iostream.h>

void main(){int sum,digit;sum=0;digit=1;while(digit<=100)

Page 19: 3 intro basic_elements

Do while loopdo{sum=sum+digit;digit=digit+2;}while(digit<=max);cout<<“2+4+...=“<<m

ax<<“sum=“;}

04/10/2319 VIT - SCSE

//program to find the sum of the even numbers using do-while loop. #include<iostream.h>void main(){int max,sum,digit;digit=2;cout<<“enter a number“;cin>>max;sum=0;

Page 20: 3 intro basic_elements

Break statementif(a<=0){cout<<”Zero or negative

value found”;break;}i++;}}

04/10/2320 VIT - SCSE

#include<iostream.h>void main(){int a,i;i=0;while(i<=10){cout<<”Enter a number”<<endl;cin>>a;

Page 21: 3 intro basic_elements

Continue statementif(a<=0){cout<<”Zero or negative

value found”;continue;}i++;}}

04/10/2321 VIT - SCSE

#include<iostream.h>void main(){int a,i;i=0;while(i<=10){cout<<”Enter a number”<<endl;cin>>a;

Page 22: 3 intro basic_elements

Goto statementoutput1:cout<<”largest value=”<<a<<endl;goto stop;output2:cout<<”largest value=”<<b<<endl;stop:getch();}

04/10/2322 VIT - SCSE

#include<iostream.h>#include<conio.h>void main(){int a,b;cout<<”Enter two numbers;cin>>a>>b;if(a>b)goto output1;elsegoto output2;

goto label;