250 likes | 361 Views
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);
E N D
Lecture# 5 Programming Concepts
Last lecture • Variables • Variables names • Input the data (cin>>) • Data Types • Arithmetic Operators
Using Character • #include<iostream> • #include<conio> • int main(){ • char x; • x=‘a’; • cout<<x<<endl<<"ASCII Value:"<<int(x); • getch(); • return 0; • }
Variables Types i.e.: unsigned char a; unsigned short c; unsigned long y;
> x>y < x<y >= x>=y <= x<=y == x==y != x!=y Relational Operators Relational Operator Example of C++ Equality Operators
#include<iostream> • #include<conio> • int main(){ • int x=3,y=5; • cout<<(x>y)<<endl<<(x<y)<<endl; getch(); • return 0; • }
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
if Statement • If statement is used in c++ for selection purposes • Structure of if statement is: if(condition) statement; True(1) OR False(0)
#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;
if(num1<num2) • cout<<num1<<" is less than "<<num2<<endl; • if(num1>num2) • cout<<num1<<" is greater "<<num2<<endl; • getch(); • return 0; • }
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
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; • }
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 • Jump 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; }