1 / 25

Lecture# 5 Programming Concepts

Lecture# 5 Programming Concepts. Last lecture. Variables Variables names Input the data (cin>>) Data Types Arithmetic Operators. Variables Types. Using Character. #include<iostream> #include<conio> int main(){ char x; x=‘a’; cout<<x<<endl<<"ASCII Value:"<<int(x);

Download Presentation

Lecture# 5 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# 5 Programming Concepts

  2. Last lecture • Variables • Variables names • Input the data (cin>>) • Data Types • Arithmetic Operators

  3. Variables Types

  4. Using Character • #include<iostream> • #include<conio> • int main(){ • char x; • x=‘a’; • cout<<x<<endl<<"ASCII Value:"<<int(x); • getch(); • return 0; • }

  5. Variables Types i.e.: unsigned char a; unsigned short c; unsigned long y;

  6. > x>y < x<y >= x>=y <= x<=y == x==y != x!=y Relational Operators Relational Operator Example of C++ Equality Operators

  7. #include<iostream> • #include<conio> • int main(){ • int x=3,y=5; • cout<<(x>y)<<endl<<(x<y)<<endl; getch(); • return 0; • }

  8. Conditional Statements Consider the following statements: • if today is Thursday I have test today • If you will run fast, u will catch the bus • if x is dividable by 2 then x is even number

  9. if Statement • If statement is used in c++ for selection purposes • Structure of if statement is: if(condition) statement; True(1) OR False(0)

  10. #include<iostream> • #include<conio> • int main(){ • int num1,num2; • cout<<"Enter two integers-->:"; • cin>>num1>>num2; • if(num1==num2) • cout<<num1<<" is equal to "<<num2<<endl; • if(num1!=num2) • cout<<num1<<" is not equal to "<<num2<<endl;

  11. if(num1<num2) • cout<<num1<<" is less than "<<num2<<endl; • if(num1>num2) • cout<<num1<<" is greater "<<num2<<endl; • getch(); • return 0; • }

  12. Problem Prompt user to enter a value.Check if the number is even then display a message that number is even. If number if odd then display message number is odd

  13. int main(){ • int num; • cout<<"Enter a number --> "; • cin>>num; • if(num%2 == 0) • cout<<endl<<num<<": is an even number"; • if(num%2 != 0) • cout<<endl<<num<<": is an odd number"; • getch(); • return 0; • }

  14. Conditional Statements • if today is Thursday I have test today else we have match today • If you will run fast, you will catch the bus else you have to take a cab • if x is dividable by 2 then x is even number else x is odd

  15. if-else Statement • Used for double selection • Syntax of if-else if as: if(condition) statement; else statement;

  16. #include<iostream> • #include<conio> • int main(){ • int num; • cout<<"Enter your grade --> "; • cin>>num; • if(num>= 50) • cout<<endl<<"Congraulations u r Passed \n"; • else • cout<<"You are failed"; • getch(); • return 0; • }

  17. Problem Ask user to enter two numbers.Display which number is greater between two

  18. #include<iostream> • #include<conio> • int main(){ • int num1,num2; • cout<<"Enter first num --> "; • cin>>num1; • cout<<"Enter second num --> "; • cin>>num2; • if(num1>=num2) • cout<<endl<<num1<<": is greater \n"; • else • cout<<num2<<": is greater"; • getch(); • return 0; • }

  19. Code Flow • In any programming language the flow of the the code can be of four types • Sequential • Selection • Repetition • Jump  Obsolete Style

  20. Code Flow • Sequential • Selection - Single Selection(if) - Double Selection (if-else) -Multiple Selection (else-if,switch) • Repetition - while Loop - do-while Loop -for Loop

  21. Multiple Selection(else-if) • The structure of else if can be defined as: if(condition) statement; else if(condition) statement; else if(condition) statement; …… else statement;

  22. #include<conio> #include<iostream> int main(){ int x; cout<<"Please enter a number between 1 to three:"; cin>>x; if(x==1) cout<<"\n You will go to Heaven"; else if(x==2) cout<<"\n You will go to Hell"; else if(x==3) cout<<"\n You will remain on earth"; else cout<<"You have enter some thing other than 1,2 ,3"; getch(); return 0; }

More Related