270 likes | 407 Views
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;.
E N D
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; True(1) OR False(0)
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
if-else Statement • Used for double selection • Syntax of if-else if as: if(condition) statement; else statement;
#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; • }
Problem Ask user to enter two numbers.Display which number is greater between two
#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; • }
Code Flow • In any programming language the flow of the the code can be of four types • Sequential • Selection • Repetition • Go to Obsolete Style
Code Flow • Sequential • Selection - Single Selection(if) - Double Selection (if-else) -Multiple Selection (else-if,switch) • Repetition - while Loop - do-while Loop -for Loop
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;
#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; }
Multiple Selection (switch) switch(n){ case 1: statement; statement; break; case 2: statement; statement; break; ……. default: statement; statement; }
Two Number Calculator • Ask the user to enter two number • Then ask to enter the operator(+,-,\,*,%) • Display the result
Ternary Operator(?:) • Ternary operator exactly work as if-else statement do • Structure of ternary operator is: • (condition ? statement : statement)
Logical Operators • These operators are used to combine multiple conditions • Three logical operators are: And && Or || Not !
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
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;
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; • }