CSC 107 - Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Conditionals – if - else Dr. Sajib Datta
Conditional Statements Introduction to Computing Science and Programming I.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
CSC Programming for Science Lecture 11: Switch Statements & ?: Operator.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Logical Operators and Conditional statements
C++ for Engineers and Scientists Third Edition
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
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 &
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
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.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
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?
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Flow of Control Part 1: Selection
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. 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.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
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)
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
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.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
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 8: Character Functions.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
PHY 107 – Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC Programming for Science Lecture 12: while & do/while Loops.
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.
CSC Programming for Science Lecture 5: Actual Programming.
Basic concepts of C++ Presented by Prof. Satyajit De
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 4: Control Structures I (Selection)
CSC 1051 – Data Structures and Algorithms I
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
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 Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements

Today’s Goal After today, should have better understanding of how to write if-else statements  Will finish this discussion on Friday

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?

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

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

Operators Result Result of numeric operation well defined / 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

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

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

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

Any Guesses? 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 indent code within set of braces ({}) Use consistent indentation size (I use 3 space) Nothing is more annoying than looking for the next line.

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

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

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

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

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”); }

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

Your Turn Get into groups and complete daily activity

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