Beruflich Dokumente
Kultur Dokumente
1 of 4
http://www.charlesli.org/pic10a/lectures/lecture4/index.html
PIC10A Lecture 4
Reading
p44-61
Boolean expression
named after famous Mathmatician George Boole
In 1854 he published An investigation into the Laws of Thought,
on Which are founded the Mathematical Theories of Logic
andProbabilities Boole approached logic in a new way reducing it
to a simple algebra, incorporating logic into mathematics. He
pointed out the analogy between algebraic symbols and those that
represent logical forms. It began the algebra of logic called
Boolean Algebra which now finds application in computer
construction, switching circuits etc.
Examples
bool a = true;
bool b = 5; // number not equal to 0 means true
bool c = 0; // false
C++ notation
English
Sample
!=
not equal to
ans != 'n'
==
<
<=
>
>=
equal to
less than
x+7==2*y
10/12/2015 3:50 PM
Pic10A Lecture4
2 of 4
http://www.charlesli.org/pic10a/lectures/lecture4/index.html
Logical operators
C++ notation
English
||
or
&&
!
Sample
and
not
(grade=='A') || (grade=='B')
int a = ????;
cout << (a > 1 && a < 10 && a%2==1);
2)
If a = 3, what is !(a > 5 || a <7)?
if-else statements
Syntax
if(condition)
one line statement;
Meaning
Example
if(condition) {
statement1;
statement2;
.........
}
char gender;
cin << gender;
if(gender=='M' || gender=='m')
cout << "You are a male.\n";
You must use braces when there are more than one statements.
When there is only one statement, the braces are optional.
You can always put braces (even for one line statement).
/*
Enter your score (within 0 to 100).
The program will find out your grade.
A: 90-100
B: 80-90
C: 65-80
D: 50-65
*/
#include<iostream>
using namespace std;
int main() {
double score;
cout << "Please enter your score: ";
cin >> score;
10/12/2015 3:50 PM
Pic10A Lecture4
3 of 4
http://www.charlesli.org/pic10a/lectures/lecture4/index.html
if(score>100 || score<0) { // score is within 0 to 100
cout << "Invalid score.\n";
cout << "Please check the score and run the program again.\n";
}
if(score>=90 && score<=100)
cout << "Your grade is A.\n";
if (score>= 80 && score<90)
cout << "Your grade is B.\n";
if (score >= 65 && score < 80)
cout << "Your grade is C.\n";
if(score>=50 && score<65)
cout << "Your grade is D.\n";
if(score>=0 && score<50)
cout << "You fail!\n";
return 0;
Syntax
if(condition)
yes_statement;
else
no_statement;
Meaning
Example
if(condition) {
yes_statement1;
yes_statement2;
......
} else {
no_statement1;
no_statement2;
....
}
Syntax
if(condition1) {
statement1;
......
} else if(condtion2){
statement2;
.....
} ........
............
else {
statement_for_other;
}
char gender;
cin << gender;
if(gender=='M' || gender=='m')
cout << "You are a male.\n";
else
cout << "You are a female.\n";
Meaning
Examples
/*
Enter your score (within 0 to 100).
The program will find out your grade.
10/12/2015 3:50 PM
Pic10A Lecture4
4 of 4
http://www.charlesli.org/pic10a/lectures/lecture4/index.html
A: 90-100
B: 80-90
C: 65-80
D: 50-65
*/
#include<iostream>
using namespace std;
int main() {
double score;
cout << "Please enter your score: ";
cin >> score;
10/12/2015 3:50 PM