CS 180 Recitation 9/20/07 - 9/21/07. Announcements Exam 1 is Tuesday, September 25 th  Rooms: Sec 0101-0401 WTHR 104 Sec 0501-0801 MATH 175  Time: 7:00.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
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)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Lecture 4: Booleans and if-else Statements Yoni Fridman 7/3/01 7/3/01.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
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.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 2. Program Construction in Java. 2.4 Selection (decisions)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
ICT Introduction to Programming Chapter 4 – Control Structures I.
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.
Control Statements: Part1  if, if…else, switch 1.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Lecture 3 Selection Statements
Branching statements.
Chapter 4: Control Structures I
Selections Java.
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.
Lecture 3- Decision Structures
Flow of Control.
Chapter 3 Branching Statements
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Control Structures I
Selection CSCE 121 J. Michael Moore.
Chapter 4: Control Structures I (Selection)
The System.exit() Method
Control Structure Chapter 3.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Branching statements Kingdom of Saudi Arabia
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Announcements Program 1 due noon Lab 1 due noon
Control Structure.
Presentation transcript:

CS 180 Recitation 9/20/07 - 9/21/07

Announcements Exam 1 is Tuesday, September 25 th  Rooms: Sec WTHR 104 Sec MATH 175  Time: 7:00 pm If you have a conflict with the exam, contact Dr. Van Zandt Sign the Academic Integrity Policy!! Mentoring with Armand Navabi  Mondays 7-9 pm in LWSN B134

Boolean Expressions boolean is a primitive data type Two values: true or false Compares two values using a relational operator , ==, =, != Examples  boolean isOK = true;  boolean isLarge = num >= 100;

Boolean Operators Boolean expressions can be combined using boolean operators  And&&  Or||  Not!

Boolean Operators bool1 && bool2  true if bool1 AND bool2 are true  false if either bool1 or bool2 is false bool1 || bool 2  true if either bool1 OR bool2 is true  false if both bool1 AND bool2 are false !bool1  true if bool1 is false  false if bool1 is true

Boolean Operator Examples What are the results of the above expressions? int x = 20; boolean isValid = x > 0 && x < 100; int x = 5; boolean isValid = x > 0 || x < -100; int x = 5; int y = 10; boolean isDouble = (y == (x * 2));

Operator Precedence Parenthesis should be used to indicate the order of operations When there are no parenthesis, operator precedence is followed  Higher precedence preformed before lower precedence  Equal precedence performed left-to-right, except for unary operations which are performed right-to- left

Operator Precedence Rules

Operator Precedence Examples What are the values of x, y, w, and z? int x = * 10; int y = (20 + 5) * 10; boolean w = true && true || false && false; boolean z = true && (true || false) && false;

Selection Statements Selection statements change the flow of control in a program Use when the action to be preformed is dependent on the answer to a question  E.g. If the value of x is less than zero, print an error message. Otherwise add 1

If-else Statement A branching statement used to choose between two actions if ( ) else The else branch is executed only when the if condition evaluates to false It is optional

If-else Statement To enclose multiple statements in a branch, enclose the branch in braces if ( ) { line 1; line 2; line 3; } else { line 4; line 5; }

If-else Statement Single line blocks are not required to be enclosed in braces, but it is a good idea if ( ) else Is equivalent to if ( ) { } else { }

Multibranch If-else Statement if ( ) statement 1 else if ( ) statement 2 else if ( ) statement 3 else if ( ) statement 4 else Default Statement

If-else Statement Examples int grade = 11; if (grade 10) grade++; else grade--; int count = 70; if (count >= 90 && count < 100) System.out.println(“A”); else if(count >= 80) System.out.println(“B”); else if(count >= 70) System.out.println(“C”); else if(count >= 60) System.out.println(“D”); else System.out.println(“F”);

If-else Statement Examples What is the value of grade in each statement? int grade = 5; if(grade < 5) grade++; grade = 10; int grade = 5; if(grade < 5) { grade++ grade = 10; }

If-else Statement Examples What is the value of grade in each statement? int grade = 5; if(grade < 5) ; grade = 10; int grade = 5; if(grade > 0) grade++; if(grade >= 5) grade++; else grade = 0;

Dangling else Which if does the else match? Design decision. Java matches it to the most recent if. if ( ) if ( ) else

Switch Statements Switch statements are a multiway branch which makes its decision based on an integer expression  char, byte, short, or int A list of cases is searched until a match is found If no match is found, the default case is executed  Optional

Switch Statement Syntax The breaks and the default case label above are optional What happens if we take the breaks out? switch(Controlling_Expression) { case Label1: statement(s); case Label2: statement(s); statement(s); }

Switch Statement Examples What are the results of the above statements? int section = 7; int room = 0; switch(section) { case 1: room = 101; break; case 7: room = 102; break; case 5: room = 103; break; default: System.out.println(“invalid”); } int section = 7; int room = 0; switch(section) { case 1: room = 101; break; case 7: room = 102; case 5: room = 103; default: System.out.println(“invalid”); }

Switch Statement Examples What is the result of the above statement?  Notice the empty case bodies What if gender = ‘x’ ? char gender = ‘f’ switch(gender) { case ‘f’: case ‘F’: System.out.println(“female”); break; case ‘m’: case ‘M’: System.out.println(“male”); break; }

Unicode Encoding The Unicode Worldwide Character Standard (Unicode) supports the interchange, processing, and display of the written texts of diverse languages. A UNICODE character takes up two bytes. ASCII characters take up one byte. char ch1 = ‘X’; System.out.println(ch1); /* output X */ System.out.println((int) ch1); /* output 88 */

Character Processing char ch1, ch2 = ‘X’; System.out.println(“ASCII code of character X is “ + (int) ‘X’); System.out.println(“Character with ASCII code 88 is “ + (char) 88); ‘A’ < ‘c’ if ( ch1 < ‘A’ && ch2 == 99 ) System.out.println(“Done”); Declaration and Initialization Type conversion between int and char. Comparison returns true because ASCII value of ‘A’ is 65 while that of ‘c’ is 99. Can compare characters and numbers.

compareTo method for 2D points private double myx, myy; public static int compareTo(double x, double y) { double mydistance = Math.sqrt(Math.pow(myx, 2) + Math.pow(myy, 2)); double distance = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); if(mydistance < distance) { return -1; } else if(mydistance == distance) { return 0; } else { return 1; }