1 / 33

Lecture# 2 Programming Concepts

Lecture# 2 Programming Concepts. In Last Lecture. Hardware Software Computer Architecture Machine Language Assembly Language High Level Language. C++ Programming Language. Extended version of c language C is evolved by Dennis Ritchie C++ is developed by Bjarne Stroustrup.

Download Presentation

Lecture# 2 Programming Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture# 2 Programming Concepts

  2. In Last Lecture • Hardware • Software • Computer Architecture • Machine Language • Assembly Language • High Level Language

  3. C++Programming Language • Extended version of c language • C is evolved by Dennis Ritchie • C++ is developed by Bjarne Stroustrup

  4. Programming in C++ Output C++ Code Compiler Error Messages

  5. First Program • Programs in c++ are written in blocks • Every block has name • The name of monitor block is main • Every statement is ended with semi-colon(;) • Our first program will display a message on output screen

  6. Simple Program • #include<iostream> • #include<conio> • int main(){ • cout<<"Every age has a language of its own\n"; • getch(); • return 0; • }

  7. Simple Program • Now this program will pass through two phases • Compile checking for errors • Run The output of program

  8. Simple Program • #include<iostream> • #include<conio> • int main(){ • cout<<"Every age has a language of its own\n"; • getch(); • return 0; • }

  9. Comments • #include<iostream> • #include<conio>//Preprocessor Directive • int main(){ //start of the function • cout<<"Every age has a language of its own\n"; • getch(); • return 0; //Return Control to OS • }

  10. Escape Characters • #include<iostream> • #include<conio> • int main(){ • cout<<“*\n**\n***\n*****\n"; • getch(); • return 0; • }

  11. Escape Characters

  12. Variables • A variable is a location in computer memory where a value can be stored • Every variable has two parts: • Variable type • Variable name (identifier) • i.e. int a; • Variables must be declared before they are used in program

  13. 100 101 102 103 104 105 106 RAM int abc; abc=10; 10 abc

  14. Variables • #include<iostream> • #include<conio> • int main(){ • int var1; • int var2; • var1=20; • var2=var1+10; • cout<<“var1+10 is :”; • cout<<var2<<endl; • getch(); • return 0; • }

  15. Initialize five variables a,b,c,d,e.Display them on screen • #include<iostream> • #include<conio> • int main(){ • int a,b,c,d,e; • a=3; • b=4; • c=6; • d=e=9; • cout<<a<<endl<<b<<endl<<c<<“ ”<<d; • return 0; • }

  16. Swapping of variables • #include<iostream> • #include<conio> • int main(){ • int x,y,temp; • x=3; • y=4; • temp=x; • x=y; • y=temp; • cout<<x<<endl<<y; • return 0; • }

  17. Variables Names • Following rules should follow for naming the variables: • Upper case, lower case letters and digits(0 to 9) • Underscore(_) can also be used • Name must start with a letter or underscore

  18. Variables Names • Keywords cannot be used as variable name i.e. if, else, while etc. • C++ is a case sensitive language uppercase and lower case are different: a1 is different from A1 • Valid identifiers: int abc, int aBc, int first_var, int _first • Invalid identifiers: int 3bc, int a*b, int #a

  19. Adding Two Integers • #include<iostream> • #include<conio> • int main(){ • int integer1, integer2, sum; • cout<<“Enter first integer\n”; • cin>>integer1; • cout<<“Enter second interger\n”; • cin>>integer2; • sum = integer1+integer2; • cout<<“Sum is :”<<sum<<endl; • getch(); • return 0; • }

  20. Arithmetic Operators

  21. Problem • Ask user to enter a three digit number.Then display the number in reverse order

  22. #include<iostream> • #include<conio> • void main(){ • cout<<"Enter a three digit number\n"; • int a; • cin>>a; • cout<<a%10; • a=a/10; • cout<<a%10; • a=a/10; • cout<<a; • getch(); • }

  23. Variables Types

  24. Using Character • #include<iostream> • #include<conio> • int main(){ • char x; • x=‘a’; • cout<<x; • getch(); • return 0; • }

More Related