Download presentation
Presentation is loading. Please wait.
Published byMargaret Welch Modified over 9 years ago
1
CSC 107 - Programming for Science Lecture 10: Boolean Expressions & More If Else Statements
2
Today’s Goal After today, should have better understanding of how to write if-else statements Will finish this discussion on Friday
3
Goal When Writing Code Good programs work Focus must be creating working algorithm No code can overcome incorrect algorithm Always design solution on paper first Start coding only after design is complete Quick poll: How many did this in lab?
4
Boolean Expression In C, any expression can be a boolean expression Simply a matter of how expression is used If value is 0, C treats it as false If value is not zero, C treats it as true What would the following expressions be if x = 3? x == 3 (x + 3) != ((5 * 2) – 4) !x x = 0 x = 4
5
Operators Most functions and all operators compute value Result can be used, ignored, or assigned (saved) Is not saved unless variable assigned its value Numeric operators are familiar & comfortable +, -, /, *, %, =, --, ++, … Depending on inputs compute a char, short, int, long, float, double, or long double Relational operators just another operator ==, !=,, = Result of these always smallest possible integer type
6
Operators Result Result of numeric operation well defined 6 + 4 6.0 / 4 10 / 4 x = 4 sqrt(x) Result of relational operator not as defined Follows simple rules, but true not just 1 value 7 <= 5 (10 / 4) == 2 pow(9, 0.5) != 0.5
7
Boolean Operators Like relational operators, compute integer Operands do not have to be boolean Boolean is NOT a datatype in C! But operand handled with C rules of boolean
8
Boolean Operators && AND Result is “true” if both operands is “true” || OR Result is “true” if either operand is “true” ! NOT Result is “true” when operand is “false” So if operand is zero, result is not zero And if operand is not-zero, result is zero
9
Spacing in a Program C ignores almost all spaces in a program Includes where newlines are included The few exceptions: pre-processor statements on own line text in quotes must be on one line This leads to some very… interesting code
10
Any Guesses? 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";}
11
Indentation Traditionally indent code within set of braces ({}) Use consistent indentation size (I use 3 space) Nothing is more annoying than looking for the next line.
12
If statement First evaluates expression in parenthesis If value of expression is “true”, executes next statement If expression “false”, execution continues after statement Very common bug ; is statement (a boring statement, but…) if (a == b) ; printf(“A is equal to B\n”);
13
Longer Statements What if more than 1 statement needed? Add opening brace (“{“) after expression Indent all statements you want to include Add closing brace (“}”) after statements to be run when expression is true if (sqrt(x) == 3.0) { printf(“The value of x is 9.0 or %lf\n”, x); printf(“Square root of x is 3.0 or %lf\n”, sqrt(x)); }
14
A Modest Proposal if (a == b); Hanging semi-colon is a common bug Also very easy to miss when debugging Adding (unneeded) braces does no harm Worst-case scenario: a little extra typing Use braces on all if – else statement Prevent hanging semi-colon bug Name more variables stewardesses to make up for extra typing
15
else statement May want to perform one of two actions Add else clause after if Value of expression is “true”, execute if statements Otherwise, execute statements in else if (boolean expression) { statement;... } else { statement;... } Execution returns as normal once finished
16
Handling Multiple Options Sometimes want more than two options Add “else if” after if & before else if (a > 3) { printf(“a = %d & greater than 3\n”, a); } else if (b > 3) { printf(“b = %d & greater than 3\n”, b); } else if (c > 3) { printf(“c = %d & greater than 3\n”, c); } else { printf(“Is everything less than 3?\n”); }
17
if/else if/else Statements First evaluate if expression and when “true” execute if statements Otherwise evaluate first else if expression and when “true” execute its statements Otherwise evaluate next else if expression and when “true” execute statements If no expression is “true” execute else statements Include as many else if as you want else is optional
18
Your Turn Get into groups and complete daily activity
19
For Next Lecture Read Sections 3.1 – 3.3 of book Do not need to understand all the details But important knowing what is not understood Continue week #4 weekly assignment Get lab #3 up and running First programming assignment on web Due 2 weeks from this Friday
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.