CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.

Slides:



Advertisements
Similar presentations
More on Algorithms and Problem Solving
Advertisements

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Lecture 2 Introduction to C Programming
Introduction to C Programming
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,
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
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 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.
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  Based upon B (& other) earlier languages  Since its creation,
Problem of the Day  Why are manhole covers round?
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
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. History of C  Dennis Ritchie developed C from 1969 – 1973  Based upon B (& other) earlier languages  Since its creation,
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
CSC 107 – Programming For Science. Announcements.
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.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
CSC 107 – Programming For Science. The Week’s Goal.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
CSC 107 – Programming For Science. George Boole  Mathematician from English middle-class  Lived from 1815 – 1864  Started work at age 16 as a teaching.
CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  While at Bell Labs, created language to develop Unix 
CSC 107 – Programming For Science. Announcement Today’s Goal  Know how to write selections besides if-else  Why we use select or if - else and how.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CSC 212 – Data Structures Prof. Matthew Hertz WTC 207D /
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Week 8: Decisions Bryan Burlingame 21 October 2015.
CSC 107 – Programming For Science. Announcements.
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
COMP Loop Statements Yi Hong May 21, 2015.
PHY 107 – Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;
CSC 107 – Programming For Science. Final Exams Dec. 16, 8AM – 10AM in OM221  Exam for CSC107: Dec. 16, 8AM – 10AM in OM221  Will be done using paper.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Basic concepts of C++ Presented by Prof. Satyajit De
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 3: Selection Structures: Making Decisions
Understanding Conditions
Relational Operators.
Life is Full of Alternatives
Unit 3: Variables in Java
An Overview of C.
Introduction to C Programming
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

CSC 107 – Programming For Science

Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus  Read before lecture so can know what to ask no magic formulas  Need to know concepts since no magic formulas  Single solution not useful; will not see problem again  Examples in book good, but cannot just copy them  What does using do? When should it be used?  How to print out things? What does printf mean?

Today’s Goals  At end of lecture, be able to use if statements  More than just calculator, choose what is executed  Understand source of many bugs & how to avoid  Correctly use braces ( { } ) and make code pretty  Be able to explain when & why if s used

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)  NOT the same as assignment (=)

Code Structures in Programming  Sequence  Selection yesno

if (…) statement  1 st evaluates boolean expression in parenthesis  Executes next statement if expression is true  When expression is false, skips over the statement int num = -32; if (num = 0)) cout = 0) cout << sqrt(num) << " is root of " << num;

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 (month == 9 && day == 19) { cout << "Yarrrr! Avast, matey!"; ninja = false; }

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 Wednesday  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!