Week 4 - Wednesday CS 121.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
Week 5 - Friday.  What did we talk about last time?  Repetition  while loops.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
Week 4: Conditional Execution 1.  So far we have only considered Java programs that do one thing after another, in sequence  Our programs have not had.
COMP 14 Introduction to Programming Miguel A. Otaduy May 19, 2004.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4 Making Decisions
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
The switch Statement, DecimalFormat, and Introduction to Looping
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Week 4 - Wednesday.  What did we talk about last time?  if statements  else statements  Nested selection statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
DNA Bases. Adenine: Adenine: (A) pairs with Thymine (T) only.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
Discussion 4 eecs 183 Hannah Westra.
Week 2 - Wednesday CS 121.
Assignment 201 – task B ANNIVERSARIES
Week 4 - Friday CS 121.
Week 3 Part 2 Kyle Dewey.
COMP 14 Introduction to Programming
Week 3 - Friday CS222.
The switch Statement, and Introduction to Looping
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
2. Java language basics (2)
Decisions Chapter 4.
Chapter 4: Making Decisions.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
EGR 2261 Unit 4 Control Structures I: Selection
Week 4 - Monday CS 121.
Week 15 – Wednesday CS 121.
DKT121: Fundamental of Computer Programming
Chapter 4: Making Decisions.
Chapter 6: Conditional Statements and Loops
Chapter 8: Control Structures
Week 4 - Monday CS222.
Chapter 4: Making Decisions.
Selection (if-then-else)
Selection CSCE 121 J. Michael Moore.
Chapter 7 Conditional Statements
CS2011 Introduction to Programming I Loop Statements (II)
Chapter 4: Control Structures I (Selection)
Lecture Notes – Week 2 Lecture-2
Conditionals.
Boolean Expressions to Make Comparisons
CPS125 Week 5.
EECE.2160 ECE Application Programming
Decisions, decisions, decisions
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
EECE.2160 ECE Application Programming
Week 5 - Friday CS 121.
Presentation transcript:

Week 4 - Wednesday CS 121

Last time What did we talk about last time? if statements else statements Nested selection statements

Questions?

Project 1

if Review

if( condition ) statement; Anatomy of an if Any boolean expression The if part if( condition ) statement; Any single executable statement

Two different outcomes Anatomy of an if-else if( condition ) statement1; else statement2; Two different outcomes

An if with multiple statements if( condition ) { statement1; statement2; … statementn; } A whole bunch of statements

Nested ifs if( condition1 ) { statement1; if( condition2 ) … }

if Examples

DNA Assume that you have a variable called base of type char Let base contain one of: 'A', 'C', 'G', 'T' Write a series of if- and else-statements that will print out the chemical name of the base denoted by the corresponding character A -> Adenine C -> Cytosine G -> Guanine T -> Thymine

Printing DNA bases if( base == 'A' ) System.out.println("Adenine"); else if( base == 'C' ) System.out.println("Cytosine"); else if( base == 'G' ) System.out.println("Guanine"); else if( base == 'T' ) System.out.println("Thymine"); else System.out.println("Your base doesn't " + "belong to us"); What if you want to take care of upper and lower cases?

Upper and lower case bases using logic if( base == 'A' || base == 'a' ) System.out.println("Adenine"); else if( base == 'C' || base == 'c' ) System.out.println("Cytosine"); else if( base == 'G' || base == 'g' ) System.out.println("Guanine"); else if( base == 'T' || base == 't' ) System.out.println("Thymine"); else System.out.println("Your base doesn't " + "belong to us"); Is there a simpler way?

Upper and lower case bases using character conversion base = Character.toUpperCase( base ); if( base == 'A' ) System.out.println("Adenine"); else if( base == 'C' ) System.out.println("Cytosine"); else if( base == 'G' ) System.out.println("Guanine"); else if( base == 'T' ) System.out.println("Thymine"); else System.out.println("Your base doesn't " + "belong to us");

switch statements

if statements are okay… But, didn't that DNA example seem a little clunky? Surely, there is a cleaner way to express a list of possibilities Enter: the switch statement

Anatomy of a switch statement switch( data ) { case value1: statements 1; case value2: statements 2; … case valuen: statements n; default: default statements; }

DNA hittin' switches switch( base ) { case 'A': System.out.println("Adenine"); break; case 'C': System.out.println("Cytosine"); case 'G': System.out.println("Guanine"); case 'T': System.out.println("Thymine"); default: System.out.println("Your base" + "doesn't belong to us"); break; // unnecessary } Go to SwitchMonth

Peculiarities of switch Both "Three" and "Four" are printed int data = 3; switch( data ) { case 3: System.out.println("Three"); case 4: System.out.println("Four"); break; case 5: System.out.println("Five"); } Go to SwitchAge The break is optional The default is optional too

Rules for switch The data that you are performing your switch on must be an int, a char, or a String The value for each case must be a literal Execution will jump to the case that matches If no case matches, it will go to default If there is no default, it will skip the whole switch block Execution will continue until it hits a break

DNA with upper and lower case switch( base ) { case 'A': case 'a': System.out.println("Adenine"); break; case 'C': case 'c': System.out.println("Cytosine"); case 'G': case 'g': System.out.println("Guanine"); case 'T': case 't': System.out.println("Thymine"); default: System.out.println("Your base doesn't " + "belong to us"); break; // unnecessary } Go to SwitchDays

A caution about switch Using if-statements is usually safer if-statements are generally clearer and more flexible switch statements are only for long lists of specific cases Be careful about inconsistent use of break

Example 1 Write a program that reads in various ages and prints out any special abilities you gain at that age 16 Drive a car 17 Watch R-rated movies 18 Vote and smoke 21 Drink 25 Rent cars 30 Be a senator 35 Be president

Anniversary Traditional 1st Paper 2nd Cotton 3rd Leather 4th Fruit or Flowers 5th Wood 6th Candy or Iron 7th Wool or Copper 8th Pottery or Bronze 9th Willow or Pottery 10th Tin or Aluminum 20th China 25th Silver 30th Pearl 35th Coral 40th Ruby 45th Sapphire 50th Gold 60th Diamond Example 2 Write a program that reads in wedding anniversaries and gives the traditional gift for that anniversary

Example 3 Write a program that will read in a positive integer and print out the corresponding ordinal number Number Ordinal 1 1st 11 11th 2 2nd 12 12th 3 3rd 13 13th 4 4th 21 21st 5 5th 22 22nd 6 6th 23 23rd

Quiz

Upcoming

Next time… Review Lab 4

Reminders Keep reading Chapter 4 of the textbook Keep working on Project 1 Due this Friday! Exam 1 next Monday