CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.

Slides:



Advertisements
Similar presentations
COMP 14 Introduction to Programming Miguel A. Otaduy May 19, 2004.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005.
Logical Operators and Conditional statements
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Introduction to Java Java Translation Program Structure
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Control statements Mostafa Abdallah
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
COMP 14 Introduction to Programming
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
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 3: Program Statements
CS139 October 11, 2004.
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Control Structure.
Presentation transcript:

CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009

Basic Review Terms you need to know –variable, type, value, expression, statement, declaration, assignment, initialization, package, class, method, parameter, return type What is the return type of Integer.parseInt(String) double x = *Math.pow(rad,2); You need to learn what every line of code you write does.

Review Relational Operators Less than < Greater than > Equal to == –not assignment ‘ = ‘ Not equal to != Less than or equal to <= Greater than or equal to >=

Review Boolean Operators NOT ! (unary) !(2+2==5) AND && (binary) (2+2==5) && (1+1==2) OR || (binary) (2+2==5) || (1+1==2) true false true

Comparing Strings Strings are compared on a character-by- character basis –first character not in common determines how the strings compare –ex: "Airplane" is less than "Airport" If two strings have different lengths, and one is a substring of the other, the shorter one is evaluated as less –ex: "Air" is less than "Airplane"

Comparing Strings Don't use relational operators (==, ) for Strings str1.compareTo (str2) Returns an integer value: –< 0 if str1 is less than str2 –0 if str1 is equal to str2 –>0 if str1 is greater than str2 str1.equals (str2) Returns a boolean value of true or false method parameter

Review Questions Given String str = "Homer"; str.compareTo("Marge") str.equals("homer") str.compareTo("Bart") negative value false positive value

Today Flow of Execution

Today: Selection if statements if-else statements Nested if statements switch statements Textbook Reference: Chapter 4 up to 4.5

Conditional Statements Let us choose which statement will be executed next –also called selection statements Java's conditional statements: –the if statement –the if-else statement –the switch statement

One-Way Selection Syntax:if (expression) statement

The if Statement if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.

The if Statement if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); First, the condition is evaluated. The value of sum is either greater than the value of MAX, or it is not. If the condition is true, the assignment statement is executed. If it is not, the assignment statement is skipped. Either way, the call to println is executed next.

Block Statements Syntax: { statement1 statement2. statementn } We use curly braces to group a set of individual statements. This way we can have multiple statements execute based on a decision. If we don't use curly braces, only a single statement will be executed based on the result of a decision.

The if Statement if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); A if (sum > MAX) { delta = sum - MAX; } System.out.println ("The sum is " + sum); B if (sum > MAX) { delta = sum - MAX; System.out.println ("The sum is " + sum); } C

Curly Braces int num = 87, max = 25; if (num >= max*2) { System.out.println ("apple"); } System.out.println ("orange"); System.out.println ("pear"); int num = 87, max = 25; if (num >= max*2) System.out.println ("apple"); System.out.println ("orange"); System.out.println ("pear");

if Statement Gotcha if (score >= 90); grade = "A"; No matter the result of the condition, grade will be assigned "A". The semicolon after the if statement is a semantic error.

In-Class Exercises What is printed by the following code fragment? int num = 87, max = 25; if (num >= max*2) System.out.println ("apple"); System.out.println ("orange"); System.out.println ("pear"); Write a code fragment that will print the value of val if val is less than MAX. apple orange pear if (val < MAX) { System.out.println (val); }

Two-Way Selection

The if-else Statement if ( condition ) statement1; else statement2; If the condition is true, statement1 is executed; if the condition is false, statement2 is executed One or the other will be executed, but not both

The if-else Statement if (height <= MAX) { adjustment = 0; } else { adjustment = MAX-height; }

In-Class Exercise What is the value of adjustment given the following values of height and MAX ? if (height <= MAX) { adjustment = 0; } else { adjustment = MAX-height; } height = 60, MAX = 80 height = 100, MAX = 75 height = 45, MAX =

Nested if Statements The statement (or block) executed as a result of an if statement can be another if statement (nested if). An else clause is always matched to the closest unmatched if. Closing curly brace signals the end of an if statement.

Nested if Statements The general syntax of a nested if statement is: if (condition1) { block1 } else if (condition2) { block2 } else { block3 }

Nested if Statements if (hasFourLegs) { if (playsFetch) { System.out.println ("DOG"); } else { System.out.println ("CAT"); } else if (hasWings) { System.out.println ("BIRD"); } else { System.out.println ("FISH"); } has four legs does not have four legs plays fetch doesn't play fetch has wings doesn't have wings

Min.java Example

The switch Statement

Provides another means to decide which statement to execute next Evaluates an expression, then attempts to match the result to one of several possible cases Each case contains a value and a list of statements The flow of control transfers to statement associated with the first value that matches

The switch Statement The general syntax of a switch statement is: switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... } switch and case are reserved words If expression matches value2, control jumps to here

The switch Statement Expression evaluated must be integral type –integer or character, not boolean or floating point Case values must be constant (literal), not variable Can be implemented with nested if statements, but is clearer with switch statements

The switch Statement default –no case value matches the expression –if no default exists and no case value matches the expression, no statements in the switch statement are executed break –processing jumps to statement following the switch statement –usually at the end of each case

The switch Statement System.out.print ("Enter prize code: "); int prize = Integer.parseInt(keyboard.readLine()); switch (prize) { case 1: System.out.println (``A Brand New Car!’’); break; case 2: System.out.println (``A Trip to Hawaii!’’); break; default: System.out.println (``Sorry, Try Again’’); break; }

GradeReport.java Example

Questions Assume movieRating is an int and movieName is a String switch (movieRating) { case 1: System.out.println ("Run away!"); if (movieName.equals("Gigli")) { System.out.println ("Quickly!"); } case 2: System.out.println ("Save your money"); break; case 3: System.out.println ("It's OK"); break; } Run away! Quickly! Save your money [Nothing] 1. What is printed if movieRating is 1 and movieName is "Gigli"? 2. What is printed if movieRating is 5 ?

Questions if ((x > 0) && (y < 0)) { z = x+y; System.out.println (“z = ” + z); } System.out.print (“The sum is “); if (x+y < 0) { System.out.println (“negative.”) } else { System.out.println (“positive.”); } z=1 The sum is positive. The sum is negative. The sum is positive. What is printed with the following values of x and y: 1.x = 5, y = -4 2.x = -9, y = 5 3.x = 5, y = 5

Next Time in CSCI 51 Iteration or Loops: for loops, while loops. Reading Assignment: Chapter 6