1 / 27

Lecture# 6 Programming Concepts

Lecture# 6 Programming Concepts. Last lecture. Character variables Unsigned Variables Relational Operators Conditional Statements. if Statement. If statement is used in c++ for selection purposes Structure of if statement is: if(condition) statement;.

saeran
Download Presentation

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

  2. Last lecture • Character variables • Unsigned Variables • Relational Operators • Conditional Statements

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

  4. 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

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

  6. #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; • }

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

  8. #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; • }

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

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

  11. 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;

  12. #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; }

  13. Multiple Selection (switch) switch(n){ case 1: statement; statement; break; case 2: statement; statement; break; ……. default: statement; statement; }

  14. Two Number Calculator • Ask the user to enter two number • Then ask to enter the operator(+,-,\,*,%) • Display the result

  15. Ternary Operator(?:) • Ternary operator exactly work as if-else statement do • Structure of ternary operator is: • (condition ? statement : statement)

  16. Logical Operators • These operators are used to combine multiple conditions • Three logical operators are: And && Or  || Not  !

  17. Problem A bank gives loan in two situations • If customer is male and his age is more than 25 and his salary is more than 25000 • If customer is a female and her age is more than 30 and her income is 20000 Program: Ask user to enter age,gender(m\f) and income.Check whether he\she is eligible to get loan

  18. int main(){ • int age,income; • char gender; • cout<<"Please enter ur Age :"; • cin>>age; • cout<<"\nPlease enter ur Income :"; • cin>>income; • cout<<"\nPlease enter ur gender(m for male and f for female) :"; • cin>>gender;

  19. if(age>25 && gender=='m' && income>25000) • cout<<"\n\n\n Sir,You can apply for Loan"; • else if(age>30 && gender=='f'&& income>30000) • cout<<"\n\n\n Madam,You can apply for Loan!!!"; • else • cout<<"\n\n\n You are not eligible for loan!!!"; • getch(); • return 0; • }

More Related