Week 3 Part 2 Kyle Dewey.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
Week #10 Kyle Dewey. Overview Meeting times Typos Project #3 tidbits Exam Review.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 4 Making Decisions
Selection. Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Conditional Statement
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Week 8: Decisions Bryan Burlingame 21 October 2015.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Current Assignments Homework 2 is available and is due tomorrow (June 19th). Boolean expressions, if statements and the while loop. Project 1 due in 5.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Week 5 Part 2 Kyle Dewey. Overview Scope Lifetime Testing Exam #1 overview.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Discussion 4 eecs 183 Hannah Westra.
Lecture 3 Selection Statements
CprE 185: Intro to Problem Solving (using C)
COMP 14 Introduction to Programming
Week 3 - Friday CS222.
Making Choices with if Statements
Decisions Chapter 4.
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
Week 7 Part 2 Kyle Dewey.
Chapter 4: Making Decisions.
Control Structures – Selection
Introduction to Programming
Chapter 4: Control Structures I (Selection)
Selection (if-then-else)
Conditions and Boolean Expressions
Program Breakdown, Variables, Types, Control Flow, and Input/Output
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Controlling Program Flow
Arrays Introduction to Arrays Reading for this Lecture:
Presentation transcript:

Week 3 Part 2 Kyle Dewey

Overview Lab announcement if / else switch Project 1 Exam 1 Review

turnin lab by Friday at 11:59

Conditionals

Motivation int min( int x, int y ) { // return smaller of // x and y } int max( int x, int y ) { // return larger of

Min / Max Need a way to say “pick this one if it’s larger, else pick the other one” if / else does exactly this

if if ( condition ) itIsTrue(); if ( condition ) { } somethingElse();

Condition The condition is a Boolean expression These can use relational operators int x = 0; int y = 1; x < y 0 <= y 1 > 5 2 >= x

Equality Not equal: != ( x != y ) Equality: == (NOT =)

Examples if ( 1 < 2 ) { printf( “foo” ); } if ( 2 == 3) {

Question What does this print? int x = 0; if ( x = 1 ) { printf( “foobar” ); }

Answer “foobar” Assignment instead of equality int x = 0; if ( x = 1 ) { printf( “foobar” ); }

if / else if ( condition ) { itIsTrue(); } else { itIsFalse(); }

Examples if ( 1 < 2 ) { printf( “foo” ); } else { printf( “bar” ); printf( “baz” ); printf( “moo” );

if / else if / else Picks the first one that’s true if ( condition ) { itIsTrue(); } else if ( otherCondition ){ otherIsTrue(); } else { neitherAreTrue(); }

Min / Max Revisited int min( int x, int y ) { // return smaller of // x and y } int max( int x, int y ) { // return larger of

Example #1 if ( 2 < 2 ) { printf( “foo” ); } else if ( 2 < 3 ) { printf( “bar” ); } else { printf( “baz” ); }

Example #2 if ( 2 < 3 ) { printf( “foo” ); } else if ( 3 < 4 ) { printf( “bar” ); } else { printf( “baz” ); }

Nesting Conditionals can be nested Heavy nesting usually means it should be split into more functions

Nesting Example if ( 1 < 2 ) { if ( 5 >= 4 ) { printf( “fizz” ); } else { printf( “buzz” ); } } else if ( 2 < 3 ) { printf( “bar” ); printf( “baz” );

Boolean Operations And: && Or: ||

Examples if ( 1 < 2 && 2 > 3 ) if ( 1 == 2 || 2 >= 2 )

Precedences And (&&) has higher precedence than or (||) if ( 1 < 2 && 2 > 3 || 5 == 5 ) if ( ( 1 < 2 && 2 > 3 ) || 5 == 5 )

More on Conditions In C, 0 (zero) is considered false, and everything else is considered true This refers only to the binary representation

Examples if ( 5 ) printf( “foo" ); if ( 0 ) printf( “bar” ); printf( “baz” );

Math vs. C Say we have an integer variable “x” Mathematically, we can test if 0 < x < 1000 However, C does not behave quite like this All relational operators are binary in nature

Math vs. C int amount = 0; 0 < amount < 1000 // true in C (1) 0 < 1000 1

Math vs. C int amount = 1; 0 < amount < 1000 // true in C (1) 1 < 1000 1

Math vs. C int amount = 1000; 0 < amount < 1000 // true in C (1) 1 < 1000 1

Boolean Operators And (&&) and or (||) work in the same way 1 && 2 // returns 1 1 || 2 // returns 1 0 && 1 // returns 0 0 || 0 // returns 0

Short-Circuiting C stops looking at an expression once its truth value can be determined This is called short-circuiting 1 == 1 || 2 == 3 || 3 == 5 2 == 2 && 3 < 2 && 5 > 7

Question What does x equal after this code runs? int x = 1; if ( x > 1 && x = 0 ) { x = 2; }

Answer x = 1 int x = 1; if ( x > 1 && x = 0 ) { x = 2; }

Question What does x equal after this code runs? int x = 1; if ( x > 0 && x = 0 ) { x = 2; }

Answer x = 0 int x = 1; if ( x > 0 && x = 0 ) { x = 2; }

switch

switch Can be seen as a specialized version of if Can look nicer than if for select cases

switch switch ( integral expression ) { case constant integral expression: statements; break; default: }

Integral Expression Something that returns an integer switch ( 5.5 ) // non-integer

Constant Integral Something that returns an integer constant int x = 1; int y = 10; switch( y ) { case x: // non-constant ...

Semantics Find first matching case Execute statements until we hit a break If no cases match, execute the default

Example #1 int x = 5; switch ( x ) { case 4: printf( “four” ); break; printf( “five” ); default: printf( “Do not know” ); }

Example #2 int x = 4; switch ( x ) { case 4: printf( “four” ); break; printf( “five” ); default: printf( “Do not know” ); }

Example #3 int x = 4; switch ( x ) { case 4: printf( “four” ); case 5: printf( “five” ); break; default: printf( “Do not know” ); }

Example #4 int x = 4; switch ( x ) { case 4: case 5: printf( “five” ); break; default: printf( “Do not know” ); }

Example #5 int x = 4; switch ( x ) { case 4: case 5: printf( “five” ); default: printf( “Do not know” ); }

Example #6 int x = 6; switch ( x ) { case 4: case 5: printf( “five” ); break; default: printf( “Do not know” ); }

Useful Example int vowel = 0; switch ( getchar() ) { case ‘a’: case ‘e’: case ‘i’: case ‘o’: case ‘u’: vowel = 1; }

Useful Example int input = getchar(); int vowel = 0; if ( input == ‘a’ || input == ‘e’ || input == ‘i’ || input == ‘o’ || input == ‘u’ ) vowel = 1;

Project 1

Exam Structure 75 Minutes 20% of grade Short answer style Questions of variable length

Don’t worry about... Specific ASCII character codes (‘a’ = 97, etc.) Specific scanf / printf placeholders

Do worry about... Hint hint: I ask a lot of questions in the slides Understanding what code does Being able to write code to do certain things, perhaps with functions Different kinds of errors