Week 4 - Wednesday.  What did we talk about last time?  if statements  else statements  Nested selection statements.

Slides:



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

The if-else Statements
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
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.
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
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.
DNA Bases. Adenine: Adenine: (A) pairs with Thymine (T) only.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
C++ crash course Class 8 statements, sort, flight times program.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
CSC 107 – Programming For Science. Announcement Today’s Goal  Know how to write selections besides if-else  Why we use select or if - else and how.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Week 15 - Monday.  What did we talk about last time?  File I/O.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
CSC 107 – Programming For Science. Today’s Goal  Know how to write selections besides if-else  When each of the options makes sense  When each selection.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
PHY 107 – Programming For Science. Today’s Goal  Know how to write decisions besides if-else select  Why we use select or if - else and how to choose.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
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.
Week 4 - Friday.  What did we talk about last time?  Examples  switch statements.
Week 3 Part 2 Kyle Dewey.
COMP 14 Introduction to Programming
Week 3 - Friday CS222.
The switch Statement, and Introduction to Looping
Decisions Chapter 4.
Week 4 - Wednesday CS 121.
Chapter 4: Making Decisions.
Week 4 - Monday CS 121.
Week 15 – Wednesday CS 121.
Chapter 4: Making Decisions.
Expression Review what is the result and type of these expressions?
Selection CSCE 121 J. Michael Moore.
Chapter 7 Conditional Statements
Flow Control Statements
Lecture Notes – Week 2 Lecture-2
Decisions, decisions, decisions
Week 5 - Friday CS 121.
Presentation transcript:

Week 4 - Wednesday

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

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

Two different outcomes if( condition ) statement1; else statement2;

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

if( condition1 ) { statement1; if( condition2 ) { if( condition3 ) statement2; … }

 The simplest answer:  What if we want to add a message if the speed is legal? if( speed > 3.0e8 ) System.out.println("Not so fast!"); if( speed > 3.0e8 ) System.out.println("Not so fast!"); if( speed > 3.0e8 ) System.out.println("Not so fast!"); else System.out.println("That speed's fine."); if( speed > 3.0e8 ) System.out.println("Not so fast!"); else System.out.println("That speed's fine.");

 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

 What if you want to take care of upper and lower cases? 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"); 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");

 Is there a simpler way? 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"); 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");

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

 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

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

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

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

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

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

 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

 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

 Write a program that reads in wedding anniversaries and gives the traditional gift for that anniversary

 Write a program that will read in a positive integer and print out the corresponding ordinal number NumberOrdinalNumberOrdinal 11st1111th 22nd1212th 33rd1313th 44th2121st 55th2222nd 66th2323rd

 Review  Lab 4

 Keep reading Chapter 4 of the textbook  Keep working on Project 1  Due this Friday!  Exam 1 next Monday  Office hours this Friday from 3:30-5pm are canceled due to travel