CSE 142 Programming I

Similar documents
Conditionals. CSE / ENGR 142 Programming I. Chapter 4. Conditional Execution. Conditional ("if ") Statement. Conditional Expressions

CSE 142 Programming I

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Introduction to Programming

Text Input and Conditionals

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

UEE1302(1102) F10: Introduction to Computers and Programming

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Lecture 10: Boolean Expressions

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

Topics. Chapter 5. Equality Operators

CS Programming I: Arrays

QUIZ: What value is stored in a after this

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Chapter 4 - Notes Control Structures I (Selection)

CS 117 Fall Compound boolean expressions. Control Statements, Part 2. Using boolean operators. Boolean operators

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

BRANCHING if-else statements

Conditional Statement

Chapter 5 Selection Statements. Mr. Dave Clausen La Cañada High School

Selections. CSE 114, Computer Science 1 Stony Brook University

Relational operators (1)

Boolean Expressions. Is Equal and Is Not Equal

CS16 Exam #1 7/17/ Minutes 100 Points total

5. Selection: If and Switch Controls

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Selection Statements. Pseudocode

Lecture 4 CSE July 1992

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

CSE / ENGR 142 Programming I

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

Flow Control. CSC215 Lecture

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Control Structures in Java if-else and switch

Chapter 4: Making Decisions

Case Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Boolean Expressions. Is Equal and Is Not Equal

Decisions (If Statements) And Boolean Expressions

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Lecture 6. Drinking. Nested if. Nested if s reprise. The boolean data type. More complex selection statements: switch. Examples.

DCS/100: Procedural Programming

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Conditional Statement

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

5. Control Statements

CS Programming I: Branches

C: How to Program. Week /Mar/05

Chapter Goals. 3.1 The if Statement. Contents 1/30/2013 DECISIONS

LECTURE 04 MAKING DECISIONS

Making Decisions In Python

Programming for Electrical and Computer Engineers. Loops

8. Decision-Making Statements. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS Programming I: Branches

Decisions. Arizona State University 1

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Selection Structures II

Decision Making -Branching. Class Incharge: S. Sasirekha

More Complex Versions of the if Statement. Class 13

Selection the If Statement Try Catch and Validation

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

LESSON 3 CONTROL STRUCTURES

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Chapter 2 - Introduction to C Programming

Lecture 8 CSE11 Fall 2013 While Loops, Switch Statement, Simplifying Conditionals

Following is the general form of a typical decision making structure found in most of the programming languages:

SELECTION. (Chapter 2)

Control Statements. If Statement if statement tests a particular condition

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Programming for Engineers Iteration

typedef int Array[10]; String name; Array ages;

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Relational Operators and if. Class 10

Control Structures in Java if-else and switch

Introduction to Programming II W4260. Lecture 2

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

CS1 Lecture 3 Jan. 22, 2018

Formatting & Style Examples

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

AIMMS Function Reference - Date Time Related Identifiers

Programming Logic and Design Sixth Edition

Chapter 4. Flow of Control

Test Results Schedule Miscellanea Control Structs. Add l Oper s Break Hands on Q & A Conclusion References Files

Computer Science & Engineering 150A Problem Solving Using Computers

bool bool - either true or false

CSE413 Midterm. Question Max Points Total 100

Transcription:

CSE 142 Programming I Conditionals Chapter 4 lread Sections 4.1 4.5, 4.7 4.9 lthe book assumes that you ve read chapter 3 on functions Read it anyway, you should do fine Their order is a little screwy... 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-1 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-2 Preview of Things to Come lcontrol flow is the order in which statements are executed luntil now, control flow has been sequential: Preview Prologue lwe ll look at ways to change the flow: Conditionals Functions Loops 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-3 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-4 Conditionals lconditionals let the computer choose an execution path depending on the value of an expression Print an error if the withdrawal amount is more then what is in the account Add one to my age if it is my birthday If my grade is greater than 3.5, then celebrate Conditional ( if ) Statement if (condition) statement; lthe statement is executed only if the condition is true. if (age >= 21) printf( Have a beer!\n ); 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-5 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-6 1

Conditional Flow Chart if (x < 100) x = x + 1; y = y + 1; yes X < 100? x = x + 1 no y = y + 1; Conditional Expressions lalso called logical, or Boolean expressions lmake use of relational operators la relational operator compares two values lexamples: (x < 30) (12 > y) 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-7 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-8 Relational Operators In Math In C In English < < Less Than > > Greater Than = == Equal To <= Less Than or Equal To >= Greater Than or Equal To!= Not Equal Some Conditional Expressions air_temperature > 80 98.6 == body_temperature marital status == M weight >= 8000 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-9 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-10 Multiple Actions lwhat if we want to do more things at once? Use a compound statement! lreplace the statement with several statements surrounded by braces: { Sometimes called a block Indent each block! Example: Checking for Error printf( Enter divisor: ); scanf( %lf, &divisor); if (0 == divisor){ printf( Can t divide by 0!\n ); exit(0); No ; 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-11 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-12 2

Announcement! lnow you know enough to do homework 1 Yay! Value of Boolean Expressions lremember, expressions are things in C that have values lwhat s the value of a conditional expression? 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-13 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-14 Values of Boolean Expressions lconditional expressions are either true or false lc doesn t have a Boolean Type lc fakes it using integers! 0 means false non-zero (usually 1) means true Examples: ldo these examples make sense? lif so, what are the values assigned? foo = 7 < 0; bar = 8!= 3; 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-15 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-16 More Strangeness! lin fact, this is an expression: x = 6 + 7 lif it is an expression, then it has a value lthe value of an assignment is the value assigned The Value of an Assignment lwhat does this do? int foo, bar; foo = (bar = 6); 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-17 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-18 3

Why Am I Telling You This?! lit s easy to confuse = and == lif you confuse them, the program will compile and run, but it won t do what you want it to Strange Example I lwhat does this do? if (x = 7) printf( X is equal to 7\n ); lwatch out for this error! 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-19 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-20 Strange Example II lwhat does this do? x == PI * radius * radius; Lesson: lc is very picky Getting one character wrong will often make your program work incorrectly! lyou ll have to get very good at finding these stupid little errors Practice practice practice! 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-21 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-22 One More Strange Point ldo not use == or!= with doubles doubles may not have exact values, only approximations == and!= are only make sense if the values are exact <, >, etc. are fine for use with doubles Complex Conditionals lwe d like more expressive power If I have at least $15 or you have at least $15, then we can go to the movies. If you re in Guggenheim 224 and it s 12:00 on Friday, then you re in CSE 142. If you re in CSE 142 and your name is Isaac, then you re the lecturer. 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-23 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-24 4

Boolean Operators lboolean operators act on Boolean values to produce new Boolean values C English && And Or (sort-of )! Not Complex Conditionals in C if ((mymoney > 15.0) (yourmoney > 15.0)){ if ((location == 224) && (time == 12 )){ if (!(initial == I )){ 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-25 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-26 Truth Tables for &&, la truth table lists all possible combinations of values and their result P Q P && Q P Q T T T T T F F T F T F T F F F F 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-27 Or isn t Always Like in English! lin English if this fruit is an apple or it is an orange, then it cannot be both apple and orange lin C, (fruit == A ) (fruit == O ) is true if either half is true! 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-28 Truth Table for! l&& and are binary operators l! is a unary operator that inverts the value P!P T F F T An Example Expression lfoo is an integer that is not 5, 6, or between 10 and 20!((foo == 5) (foo == 6) ((foo > 10) && (foo < 20))) 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-29 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-30 5

DeMorgans Laws lconvert between and expressions and or expressions lexample:!((age < 25) && (sex == M )) DeMorgan s Laws ldemorgan s Laws tell us some legal conversions:!(p && Q) (!P!Q) is equivalent to ((age >= 25) (sex!= M ))!(P Q) (!P &&!Q) 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-31 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-32 Proof of DeMorgan s Laws lproof using a truth table: P Q (P && Q)!(P && Q)!P!Q (!P!Q) T T T F F T F F 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-33 Else: The Other Half of If lelse lets you do something if the condition was false if (balance >= withdrawal){ balance = balance - withdrawal; else { printf ( Insufficient Funds!\n ); 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-34 if-else Control Flow Nested ifs #define BILL_SIZE 20 no Insufficient Funds! balance >= withdraw? arrive here whether condition is TRUE or FALSE yes adjust balance dispense funds if (balance < withdrawal) { printf( Insufficient funds!\n ); else { if (withdrawal < BILL_SIZE) printf ( Try a larger amount. \n ); else balance = balance - withdrawal; 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-35 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-36 6

Tax Example lprint the tax based on income: income < 15,000 >= 15,000, < 30,000 >= 30,000, < 50,000 >= 50,000, < 100,000 >= 100,000 tax 0% 18% 22% 28% 31% 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-37 Simple Solution if ( income < 15000 ) { printf( No tax. ); if ( income >= 15000 && income < 30000 ) { printf( 18%% tax. ); if ( income >= 30000 && income < 50000 ) { printf( 22%% tax. ); if ( income >= 50000 && income < 100000 ) { printf( 28%% tax. ); if ( income >=100000) { printf( 31%% tax. ); 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-38 Cascaded ifs Another Type of Conditional! if ( income < 15000 ) { if ( income < 15000 ) { printf( No tax ); printf( No tax ); else { else if ( income < 30000 ) { if ( income < 30000 ) { printf( 18%% tax. ); printf( 18%% tax. ); else if ( income < 50000 ) { else { printf( 22%% tax. ); if ( income < 50000 ) { else if ( income < 100000 ) { printf( 22%% tax. ); printf( 28%% tax. ); else { else if ( income < 100000 ) { printf( 31%% tax. ); printf( 28%% tax. ); else { printf( 31%% tax. ); 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-39 lthe Switch Statement: 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-40 Longwinded if Better Code: /* How many days in a month? */ if ( month == 1 ) { /* Jan */ days = 31 ; else if ( month == 2 ) { /* Feb */ days = 28 ; else if ( month == 3 ) { /* Mar */ days = 31 ; else if ( month == 4 ) /* Apr */ days = 30 ;... /* need 12 of these */ if ( month==9 month==4 /* Sep, Apr */ month==6 month==11 ){/* Jun, Nov */ days = 30; else if ( month == 2 ) { /* Feb */ days = 28 ; else { days = 31; /* All the rest */ 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-41 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-42 7

Even Better Code: switch switch ( month ) { case 2: /* February */ days = 28 ; break ; case 9: /* September */ case 4: case 6: /* April /* June */ */ case 11: /* November */ days = 30 ; break ; default: /* All the rest have 31 */ days = 31 ; 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-43 switch (control expression){ case-list-1 statements 1 break; case-list-2 statements 2 break; default: statements 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-44 switch Pitfalls lthe type of the control expression must be int or char lthe cases must be constant lthe switch statement falls through 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-45 Falling Through switch (month) { case 2: days = 28 ; /* break missing */ case 9: case 4: case 6: case 11: days = 30; /* break missing */ default: days = 31 ; 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-46 Conditionals Summary lif lets the execution branch complex conditions are put together with &&,, and! lelse does something if the condition was false lswitch can be used in some situations to do a multiple branch control-expression must be int or char cases must be constants 28. June, 2000 CSE 142 Summer 2000 Isaac Kunen F-47 8