phy 107 programming for science. announcements no magic formulas exist need to learn concepts: no...

28
LECTURE 09: if STATEMENTS PHY 107 – Programming For Science

Upload: abraham-chambers

Post on 18-Jan-2018

248 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

LECTURE 09:if STATEMENTS

PHY 107 – Programming For Science

Page 2: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Announcements

Need to learn concepts: no magic formulas exist Single solution not useful; will not see

problem again Examples from slides/web cannot just be

copied What does scanf do? When should it be

used? How to print out things? What does %lf

mean? Project #1 available later tonight; due

in 3 weeks You will complete 3 larger projects over term Tie everything together; more realistic

problems solved DO NOT LEAVE FOR LAST MOMENT

Page 3: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Today’s Goals

At end of lecture, be able to use if statements Choose what is executed so not just

calculator Understand source of many bugs & how to

avoid Correctly use braces ({ }) and make code

pretty Be able to explain when & why ifs

used

Page 4: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Boolean Values

3 boolean functions can combine into computer Billions of gates combined by engineers to

make CPU 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?

Page 5: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Boolean Operators

&& (“AND”: true when both operands true)

|| (“OR”: true when either operand true)

^ (“XOR”: true when only one operand true)

! (“NOT”: true when operand false)

Page 6: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

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

Page 7: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

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

Page 8: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

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

= assigns value== checks equalityC/C++ will NOT

correct your misuse

Page 9: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Code Structures in Programming Sequence

Page 10: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Code Structures in Programming Sequence

Decisiontrue

false

Page 11: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

if (…) statement

1st evaluates boolean expression in parenthesis Executes next statement if expression is true

When expression is false, skips past statement(s)int num = -32;if (num < 0) { printf("Sorry Dave, I can't do that.\n");}if ((num < 100) && (num >= 0)) { printf(" ");}if (num >= 0) { printf("%lf=square root of %d", sqrt(num), num); }

Page 12: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

if (…) statement

1st evaluates boolean expression in parenthesis

Executes next statement if expression is true When expression is false, skips over the

statement This leads to one very common bug

; is statement (a boring statement, but…)

if (dc > 20);printf("This line is always run\n");

Page 13: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

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) { printf("Yarrrr! Avast, matey!”); ninja = false;}

Page 14: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

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

Page 15: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

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

Page 16: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Spacing in a Program

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

Page 17: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

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

Page 18: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Indentation

Traditionally we indent code within braces ({})

Use consistent size to indent (I use 2 spaces)

Page 19: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Indentation

Traditionally we indent code within braces ({})

Use consistent size to indent (I use 2 spaces)

Nothing is

Page 20: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Indentation

Traditionally we indent code within braces ({})

Use consistent size to indent (I use 2 spaces)

Nothing ismore

Page 21: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Indentation

Traditionally we indent code within braces ({})

Use consistent size to indent (I use 2 spaces)

Nothing ismore

annoying

Page 22: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Indentation

Traditionally we indent code within braces ({})

Use consistent size to indent (I use 2 spaces)

Nothing ismore

annoyingthan

Page 23: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Indentation

Traditionally we indent code within braces ({})

Use consistent size to indent (I use 2 spaces)

Nothing ismore

annoyingthan

looking

Page 24: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Indentation

Traditionally we indent code within braces ({})

Use consistent size to indent (I use 2 spaces)

Nothing ismore

annoyingthan

looking

for

Page 25: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Indentation

Traditionally we indent code within braces ({})

Use consistent size to indent (I use 2 spaces)

Nothing ismore

annoyingthan

looking

forthe next

Page 26: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Indentation

Traditionally we indent code within braces ({})

Use consistent size to indent (I use 2 spaces)

Nothing ismore

annoyingthan

looking

forthe next

line.

Page 27: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

Your Turn

Get in groups & work on following activity

Page 28: PHY 107  Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;

For Next Lecture

Read pages 99 – 104 for Friday What if we want to have multiple possible

options? How do else & else if statements work?

We will have lab on Thursday

Week #3 weekly assignment due tomorrow If problem takes more than 10 minutes,

TALK TO ME!