• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
3 intro basic_elements

3 intro basic_elements







Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    3 intro basic_elements 3 intro basic_elements Presentation Transcript

    • Basic Elements of C++ 09/04/131 VIT - SCSE By G.SasiKumar., M.E., (Ph.D)., Assistant Professor School of Computing Science and Engineering VIT University
    • Characters are used to form the words, numbers and expressions.  alphabets from A.....Z, a....z all decimal digits from 0....9 characters , . ; : ? ‘ “ ! | / ~ _ $ % # & ^ * - + < > ( ) [ ] { } Spaces blank New Line endl 09/04/132 VIT - SCSE Character Set
    • Keywords and Identifiers All keywords must be written in lowercase. auto double int struct break else long switch case enum register Typedef char extern return union const float short unsigned continue for signed void default goto sizeof 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. 09/04/133 VIT - SCSE
    • Data Types 09/04/134 VIT - SCSE C++ Data Types User Defined type Structure Union Class Enumeration Built-in type Derived type Array Function Pointer Integral Floating Character Void byte short int long float double
    • Constants Constants 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 Constants Floating point (real) Constants 2. Character Constants Single Character Constants String Constants 09/04/135 VIT - SCSE
    • 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) 09/04/136 VIT - SCSE
    • 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. 09/04/137 VIT - SCSE
    • Single Character Constants A 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 09/04/138 VIT - SCSE
    • 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." 09/04/139 VIT - SCSE
    • Escape Sequences Character Escape sequence bell a backspace b horizontal tab t vertical tab v newline n carriage return r quotation mark " apostrophe ' backslash null 0 09/04/1310 VIT - SCSE
    • Variables A variable is an identifier that is used to represent a single data item Declaration Definition 09/04/1311 VIT - SCSE
    • Operators An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Arithmetic Operators o Increment and Decrement Operators Relational and Logical Operators Assignment Operators Ternary(Conditional) Operator Sizeof Operator Comma Operator & Operator  -> Operators(a->b -Member b of object pointed to by a ) 09/04/1312 VIT - SCSE
    • Control Statements Conditional Statements If statement If-else statement Switch-case statement Loop Statements For loop While loop Do-while loop Breaking control statements Break Continue goto 09/04/1313 VIT - SCSE
    • Switch statement switch(expression) { case constant_1: statement; break; case constant_2: statement; break; ----------------- case constant_n: statement, break; default: statement; break; } 09/04/1314 VIT - SCSE
    • //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. 09/04/1315 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; } }
    • For statement //A program to find the sum and average of given numbers. 09/04/1316 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; }
    • Nested For Statement A 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; }}}} 09/04/1317 VIT - SCSE
    • While statement { sum+=digit; digit++; } cout<<“1+2+3+..... +100=“<<sum<<endl; } 09/04/1318 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)
    • Do while loop do { sum=sum+digit; digit=digit+2; } while(digit<=max); cout<<“2+4+...=“<<max<<“su m=“; } 09/04/1319 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;
    • Break statement if(a<=0) { cout<<”Zero or negative value found”; break; } i++; } } 09/04/1320 VIT - SCSE #include<iostream.h> void main() { int a,i; i=0; while(i<=10) { cout<<”Enter a number”<<endl; cin>>a;
    • Continue statement if(a<=0) { cout<<”Zero or negative value found”; continue; } i++; } } 09/04/1321 VIT - SCSE #include<iostream.h> void main() { int a,i; i=0; while(i<=10) { cout<<”Enter a number”<<endl; cin>>a;
    • Goto statement output1: cout<<”largest value=”<<a<<endl; goto stop; output2: cout<<”largest value=”<<b<<endl; stop: getch(); } 09/04/1322 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; else goto output2; goto label;