phy 107 programming for science. announcements no magic formulas exist need to learn concepts: no...
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 usedTRANSCRIPT
LECTURE 09:if STATEMENTS
PHY 107 – Programming For Science
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
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
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?
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)
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 (=)
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 (=)
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
Code Structures in Programming Sequence
Code Structures in Programming Sequence
Decisiontrue
false
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); }
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");
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;}
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/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 _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";}
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 ismore
Indentation
Traditionally we indent code within braces ({})
Use consistent size to indent (I use 2 spaces)
Nothing ismore
annoying
Indentation
Traditionally we indent code within braces ({})
Use consistent size to indent (I use 2 spaces)
Nothing ismore
annoyingthan
Indentation
Traditionally we indent code within braces ({})
Use consistent size to indent (I use 2 spaces)
Nothing ismore
annoyingthan
looking
Indentation
Traditionally we indent code within braces ({})
Use consistent size to indent (I use 2 spaces)
Nothing ismore
annoyingthan
looking
for
Indentation
Traditionally we indent code within braces ({})
Use consistent size to indent (I use 2 spaces)
Nothing ismore
annoyingthan
looking
forthe next
Indentation
Traditionally we indent code within braces ({})
Use consistent size to indent (I use 2 spaces)
Nothing ismore
annoyingthan
looking
forthe next
line.
Your Turn
Get in groups & work on following activity
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!