Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus."— Presentation transcript:

1 CSC 107 – Programming For Science

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

3 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

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

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

6 Code Structures in Programming  Sequence  Selection yesno

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

8 if (…) statement

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

10 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

11 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

12 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

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

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

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

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

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

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

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

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

21 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

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

23 Your Turn  Get in groups & work on following activity

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


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

Similar presentations


Ads by Google