Lecture 6
Other operators Some times a simple comparison is not enough to determine if our criteria has been met. For example: (and operation) If a person wants to login to bank account, the user name and the password both need to match. Another example: (or operation) Find all the person with age greater than 60 and less than 10. Yet another example: (negation operation) A person is not on the authorized list of users,abort.
And operator The and operator in C++ is denoted with the double & ampersand sign. && Example of usage: if(age > 0 && age < 120)
Or operator The or operator in C++ is denoted with the double pipe symbol. Pipe key is usually located above the enter key Example of usage: if(age < 0 age> 120) // that is not a valid age
Not operator The not operator in C++is denoted with the! exclamation symbol.! Same symbol as the! in!= Example of usage: if(!(number %2==0) ) // do some thing for odd numbers
Boolean logic Symbol Operation C++ syntax Action && AND expr1 && expr2 Evaluate expr1. If it s true, evaluate expr2. Then, if both are true, return true; otherwise, return false. OR expr1 expr2 Evaluate expr1. If false, evaluate expr2. Then return true unless both are false.! NOT! expr1 Evaluate expr1. Reverse the true/false value.
Boolean Logic - Order Logical operators have lower precedence than the relational operators (<, >, >=, <=,!, and ==), which in turn have lower precedence than arithmetic operators (such as + and *). if (x + 2 > y && a == b) cout << "The data passes the test"; // is equal to if ((x + 2 > y) && (a == b) cout << "The data passes the test ; // is equal to if (x + 2 > y && a == b) cout << "The data passes the test";
Logical And (&&): expr 1 -> expr 2
Logical And (&&) EX1 #include <iostream> using namespace std; int main() { int i=1,n=10; if (1==0 && 1/0==1){ cout << Hi"; cout <<" Bye!"; return 0; Result
Logical And (&&) EX2 #include <iostream> using namespace std; int main() { int i=1,n=10; if (1/0==1 && 1==0 ){ cout << Hi"; cout <<" Bye!"; return 0; Result
Ex.3 Testing a Person s Age The program here determines whether a number is in a particular range in this case, the range of teen numbers, 13 through 19. Get a subject age and show whether subject is teenager or not. the range of teen numbers, 13 through 19 means 13<=age and age<=19 (inclusive) Equivalently, 12<age and age <20
Ex.3 Testing a Person s Age #include <iostream> using namespace std; int main() { int age; cout << "Enter an age and press ENTER: "; cin >> age; if (age > 12 && age < 20) cout << "Subject is a teenager." << endl; else cout << "Subject is not a teenager." << endl; return 0;
Exercise 1 Exercise 1: Ask user to input one number that range between 0 to 100, if the number is not in this range keep asking user for a valid number. After get the valid number then convert from Celsius to Fahrenheit by using the user input number. Formula: F=9/5 C + 32
Exercise 1 solution #include <iostream> using namespace std; main(){ int c; cout<<"please enter a number between 0 and 100:"; cin>>c; while(c<0 c>100){ cout<<"please re enter a valid number:"; cin>>c; float f; f= 9.0/5*c+32; cout<<c<<" degree celcius is "<<f<<" degree Fahrenheit."<<endl; return 0;
Exercise 2 Exercise 2: Ask user enter an integer. 1, check is the number divisible by 3 not 5, if yes, divide by 3, and print result. 2, check is the number divisible by 5 not 3, if yes, divide by 5, and print result. 3, check is the number divisible by 3 and 5, if yes, divide by 3 and 5, and print result.
Exercise 2 solution #include <iostream> using namespace std; main(){ int n; cout<<"please enter an integer:"; cin>>n; if(n%3==0 && n%5!=0) cout<<n/3<<endl; if(n%3!=0 && n%5==0) cout<<n/5<<endl; if(n%3==0 && n%5==0) cout<<n/15<<endl; return 0;
Conversions double x = sqrt(2.0); // Assign square root of 2 to x int n = sqrt(2.0); // Place the value 1 into n, // after truncating 1.41421. double x = sqrt(2); // Ok: int converted to double. Do not forget to use sqrt we should include #include <cmath>
Prime-Number Test Prime number? a natural number greater than 1 that has no positive divisors other than 1 and itself. Ex 2,3,5,7,11,11, How many prime numbers do we have? There are infinitely many primes.
Prime-Number Test Goal: Determine whether a number is prime. Main idea: Check whether a number is divisible by another. Ex. Determine whether 7 is prime. 7 % 2= 1 0 (Not divisible by 2) 7 % 3= 1 0 (Not divisible by 3) 7 % 4= 3 0 (Not divisible by 4) 7 % 5= 2 0 (Not divisible by 5) 7 % 6= 1 0 (Not divisible by 6) Divide by 2,3,,n 1
Exercise 3 Exercise 3: Ask user input an integer, and test if it is a prime number. (prime test) prime test: a natural number greater than 1 that has no positive divisors other than 1 and itself.
Prime Test part Assume n is a given number. bool is_prime=true; i=2; while( i < n){ if (n%i==0) is_prime=false; i++; Explanation Using is_prime variable, we save the result of prime test. 1<i<n n is divisible by i, means n is not prime.
Exercise 3 solution #include <iostream> using namespace std; main(){ int n; cout<<"please enter an integer:"; cin>>n; int count=2; while(count<n){ if(n%count==0){ cout<<n<<" is divisible by "<<count<<", so it is not prime."<<endl; return 0; count++; cout<<n<<" is a prime number."<<endl; return 0;
Optimize prime test. Key Idea. Assume n is divisible by i. Then n is also divisible by n/i. If i > n/i, we already know n is not a prime when we divide by n/i. So we can assume i n/i i sqrt(n)
Prime Test part again Assume n is a given number. bool is_prime=true; i=2; while(i <= sqrt(n)) { if (n%i==0) is_prime=false; i++; Explanation Using is_prime variable, we save the result of prime test. 2 i sqrt(n) n is divisible by i, means n is not prime.