CSC 107 – Programming For Science. Announcements.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
New Mexico Computer Science For All Booleans and Logic Maureen Psaila-Dombrowski.
3. DIGITAL ELECTRONICS..
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
CSC 107 – Programming For Science. Follow-Up From Last Lecture.
CSC 107 – Programming For Science. Announcements  Tutors available MTWR in WTC206/WTC208  Special lab (with Macs) & not in the Tutoring Center  Can.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC Intro. to Computing Lecture 5: Boolean Logic, Gates, & Circuits.
CSC 107 – Programming For Science. Spacing in a Program  C++ ignores spaces in a program  This also means where newlines placed ignored  #define &
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CPS120: Introduction to Computer Science Decision Making in Programs.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
CSC Intro. to Computing Lecture 5: Boolean Logic, Gates, & Circuits.
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  Based upon B (& other) earlier languages  Since its creation,
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Problem of the Day  Why are manhole covers round?
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CPS120: Introduction to Computer Science Operations Lecture 9.
CSC 107 – Programming For Science. The Week’s Goal.
CSC 107 – Programming For Science. George Boole  Mathematician from English middle-class  Lived from 1815 – 1864  Started work at age 16 as a teaching.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
CSC 107 – Programming For Science. Announcements.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
CSC 107 – Programming For Science. Today’s Goal  Know how to write selections besides if-else  When each of the options makes sense  When each selection.
PHY 107 – Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
LOGIC CIRCUITLOGIC CIRCUIT. Goal To understand how digital a computer can work, at the lowest level. To understand what is possible and the limitations.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Rational Expressions relational operators logical operators order of precedence.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Introduction to Decision Structures and Boolean Variables
Basic concepts of C++ Presented by Prof. Satyajit De
Data Types and Expressions
Computer Science 210 Computer Organization
A mechanism for deciding whether an action should be taken
Computer Science 210 Computer Organization
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Understanding Conditions
Life is Full of Alternatives
Chapter 3: Selection Structures: Making Decisions
Life is Full of Alternatives
COMS 261 Computer Science I
Conditionals.
Presentation transcript:

CSC 107 – Programming For Science

Announcements

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

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…

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 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

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

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

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

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

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?

Relational Operators  < ( less than)  > ( greater than)  <= ( less than of equal to)  >= ( greater than of equal to)  != ( inequality ≠)  == ( equality – if two things have same value)

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 (=)

Assignment vs. Comparison

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;

Code Structures in Programming  Sequence  Selection yesno

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?"

if (…) statement

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; }

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

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

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

What Does This Compute? char _ [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;*_ =__31415=2,_ [0][_ ]=1[__3141]=5;__3_1415=1;do{_3_14159=_314=0,__ ;for( _31415 =0;_31415<(3,14-4)*__31415;_ )_31415[_3141]=_314159[_31415]= - 1;_3141[*_314159=_3_14159]=_314;_3_141=_ __3_1415;_3_1415= __3_1415 +__3141;for(_31415 = __3_1415 ;_31415;_ ,_3_141 ++,_3_1415++){_314 +=_314<<2 ;_314<<=1;_314+= *_3_1415;_31 =_ _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_ ,++_3_14159)) ; } for ( _31415 = 1; _31415< ;_ )write( 31415% 314-( 3,14),_ [ _31415 ] + " ","314" [ 3]+1)-_314; puts((*_ =0,_ )) ;_314= *" ";}

Indentation  Traditionally we indent code within braces ( {} )  Use consistent size to indent (I use 2 spaces)

Indentation  Traditionally we indent code within braces ( {} )  Use consistent size to indent (I use 2 spaces)  Nothing is

Indentation  Traditionally we indent code within braces ( {} )  Use consistent size to indent (I use 2 spaces)  Nothing is more

Indentation  Traditionally we indent code within braces ( {} )  Use consistent size to indent (I use 2 spaces)  Nothing is more annoying

Indentation  Traditionally we indent code within braces ( {} )  Use consistent size to indent (I use 2 spaces)  Nothing is more annoying than

Indentation  Traditionally we indent code within braces ( {} )  Use consistent size to indent (I use 2 spaces)  Nothing is more annoying than looking

Indentation  Traditionally we indent code within braces ( {} )  Use consistent size to indent (I use 2 spaces)  Nothing is more annoying than looking for

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

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.

Your Turn  Get in groups & work on following activity

For Next Lecture  Read sections – 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!