Download presentation
Presentation is loading. Please wait.
Published byAngela Perry Modified over 9 years ago
1
CSC 107 – Programming For Science
2
Announcements
3
George Boole Mathematician from English middle-class Lived from 1815 – 1864 Started work at age 16 as a teaching assistant Held two assistantships to support family Opened own school after many years of work In 1847 wrote Mathematical Analysis of Logic
4
Mathematical Analysis of Logic Boole’s book proposed new logical system World began with 2 values– though more created Devised rules to add, subtract, & multiply Work ignored during Boole’s lifetime System only had 2 values, so what was the point? What is done with developer of pointless knowledge? Basis for most technology in the modern age All it took was a simple little discovery…
5
Gate Combines input(s) to generate output signal Like most electronics, uses “on-off” state Input is "off", if line drops below 2 volts From 2 - 5 volts, an input is considered on Gate is deep fried silicon if line goes above 5 volts, Like Boole’s logic, electronics have 2 values Simple gates combine to make modern circuitry All initially part of Boolean algebra Basis of programming at the lowest, rawest level
6
Truth Table Normal way that Boolean functions presented All combinations of inputs shown in this table This is really easy, inputs must be true or false Output shown for each of the possible inputs Given how it sounds, not at all complicated Very simple rules to follow to construct Does requires you count up to 2
7
NOT Gate Simplest gate: computes opposite of input Output false when input true; Output true when input false; !a Written in C++ as !a a is gate’s input x is gate’s output ax !a!a!a!a true false a x
8
OR Gate Equivalent to addition in Boolean algebra If either input is true is going to be checked true when either a OR b are true; false otherwise a || b Written in C++ as a || b a & b are inputs; x is output abx a || b false true false true a b x
9
AND Gate Equivalent to multiplication in Boolean algebra If both inputs are true is going to be checked True when a AND b are true; false otherwise a && b Written in C++ as a && b a & b are inputs; x is output abx a && b false true false true a b x
10
Boolean Values 3 boolean functions can combine into computer Billions of gates combined by engineers to make CPU (a && b) || (a && !c) Could use, for example: (a && b) || (a && !c) But how to generate inputs for these functions First need boolean values of true and false Easy in hardware (true is any signal > 3.3V) But how could we do get values within our program?
11
Relational Operators < ( less than) > ( greater than) <= ( less than of equal to) >= ( greater than of equal to) != ( inequality ≠) == ( equality – if two things have same value)
12
Relational Operators < ( less than) > ( greater than) <= ( less than of equal to) >= ( greater than of equal to) != ( inequality ≠) == ( equality – if two things have same value) NOT the same as assignment (=)
13
Assignment vs. Comparison
14
Relational Operators Relational operators compute bool Like any expression, can be used in any statements int nfl = 32; bool team = 0 > 6; bool group = 45 <= nfl; bool gang = nfl == 32; team = sqrt(144) == 12.0; group = (133 == pow(12, 2)); gang = (group == team); cout << "Gang says: " << gang << endl;
15
Code Structures in Programming Sequence Selection yesno
16
if (…) statement First evaluates expression in parenthesis If expression is true, executes next statement Skips over the statement, when expression is false int nyc = 32, dc = 1; bool gang = (nyc > 12) && (dc == 1); if (nyc == dc) cout 20); cout << "Huh?"
17
if (…) statement
18
I Want More! Add opening brace ( { ) after closing parenthesis Can now write all statements to execute Add closing brace ( } ) to show where if ends If expression false, execution restarts at that point if (sqrt(x) == 3.0) { cout << "root of x = 3" << endl; cout << "So, x = 9" << endl; }
19
A Modest Proposal ; if (a == b); Hanging semi-colon is a common bug Also easy to miss when writing & debugging No harm is done by adding (unneeded) braces A little extra typing is worst-case scenario Use braces everywhere Prevents hanging semi-colon bug
20
A Modest Proposal ; if (a == b); Hanging semi-colon is a common bug Also easy to miss when writing & debugging No harm is done by adding (unneeded) braces A little extra typing is worst-case scenario Use braces everywhere Prevents hanging semi-colon bug Name variables stewardesses to balance typing
21
Spacing in a Program C++ ignores almost all spaces in a program This also means where most newlines placed ignored Symbolic constants & text in quotes are exceptions This can lead to some very… interesting code
22
What Does This Compute? char _3141592654[3141 ],__3141[3141];_314159[31415],_3141[31415];main(){register char* _3_141,*_3_1415, *_3__1415; register int _314,_31415,__31415,*_31, _3_14159,__3_1415;*_3141592654=__31415=2,_3141592654[0][_3141592654 -1]=1[__3141]=5;__3_1415=1;do{_3_14159=_314=0,__31415++;for( _31415 =0;_31415<(3,14-4)*__31415;_31415++)_31415[_3141]=_314159[_31415]= - 1;_3141[*_314159=_3_14159]=_314;_3_141=_3141592654+__3_1415;_3_1415= __3_1415 +__3141;for(_31415 = 3141- __3_1415 ;_31415;_31415--,_3_141 ++,_3_1415++){_314 +=_314<<2 ;_314<<=1;_314+= *_3_1415;_31 =_314159+_314; if(!(*_31+1) )* _31 =_314 / __31415,_314 [_3141]=_314 % __31415 ;* ( _3__1415=_3_141 )+= *_3_1415 = *_31;while(* _3__1415 >= 31415/3141 ) * _3__1415+= - 10,(*--_3__1415 )++;_314=_314 [_3141]; if ( ! _3_14159 && * _3_1415)_3_14159 =1,__3_1415 = 3141-_31415;}if( _314+(__31415 >>1)>=__31415 ) while ( ++ * _3_141==3141/314 )*_3_141--=0 ;}while(_3_14159 ) ; { char * __3_14= "3.1415"; write((3,1) (--*__3_14,__3_14 ),(_3_14159 ++,++_3_14159))+ 3.1415926; } for ( _31415 = 1; _31415<3141- 1;_31415++)write( 31415% 314-( 3,14),_3141592654[ _31415 ] + "0123456789","314" [ 3]+1)-_314; puts((*_3141592654=0,_3141592654)) ;_314= *"3.141592";}
23
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces)
24
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is
25
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more
26
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying
27
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying than
28
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying than looking
29
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying than looking for
30
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying than looking for the next
31
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying than looking for the next line.
32
Your Turn Get in groups & work on following activity
33
For Next Lecture Read sections 7.2.2 – 7.3 for Tuesday What if we want to have multiple possible options? What do else & else if statements do? Are there any rules about how to use them? Week #3 weekly assignment due Tuesday Problems available on Angel If problem takes more than 10 minutes, TALK TO ME!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.