SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.

Slides:



Advertisements
Similar presentations
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
Advertisements

CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
CSCI1402: Lecture 1 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Introduction to Computers and Programming Lecture 5 New York University.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
ITERATION CSC 171 FALL 2004 LECTURE 10. Simple Branching If (test){ A;} start end A; test.
Outline Relational Operators Boolean Operators Truth Tables Precedence Table Selection and Algorithms The if – else Variations of if The switch.
Applets & Applications CSC 171 FALL 2001 LECTURE 15.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Control structures: if-else statements and switch statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Java Exception Handling Handling errors using Java’s exception handling mechanism.
JAVA PROGRAMMING PART II.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
1 Course Lectures Available on line:
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
CS1101: Programming Methodology Recitation 3 – Control Structures.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
From C++ to Java A whirlwind tour of Java for C++ programmers.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 5: Decisions 1 Chapter 5 Decisions.
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
Java Exception Handling Handling errors using Java’s exception handling mechanism.
1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
State Design Pattern. Behavioral Pattern Allows object to alter its behavior when internal state changes Uses Polymorphism to define different behaviors.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
A: A: double “4” A: “34” 4.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
Programming Principles Operators and Expressions.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Exercise 1 Review OOP tirgul No Outline Polymorphism Exceptions A design example Summary.
CompSci 230 S Programming Techniques
Sum of natural numbers class SumOfNaturalNumbers {
Sequence, Selection, Iteration The IF Statement
Lecture 2: Data Types, Variables, Operators, and Expressions
Introduction to programming in java
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Java 24th sep /19/2018 CFILT.
SELECTION STATEMENTS (1)
مفاهیم اولیه زبان جاوا Java Basic Concepts
Control Statement Examples
Review Operation Bingo
Operators Laboratory /11/16.
null, true, and false are also reserved.
Self study.
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Question 1a) What is printed by the following Java program? int s;
LCC 6310 Computation as an Expressive Medium
Presentation transcript:

SELECTION CSC 171 FALL 2004 LECTURE 8

Sequences start end

Example method public static void main(String[] args) { int a; a = 1 ; System.out.println(“a == “ + a); a = a + a + a; System.out.println(“a == “ + a); a = a * a * a; System.out.println(“a == “ + a); }

Same statements? public static void main(String[] args) { int a; a = 1 ; System.out.println(“a == “ + a); a = a + a + a; System.out.println(“a == “ + a); a = a * a * a; System.out.println(“a == “ + a); }

Sequence Statements public static void main(String[] args) { int a; a = 1 ; System.out.println(“a == “ + a);//1 a = a + a + a; System.out.println(“a == “ + a); //3 a = a * a * a; System.out.println(“a == “ + a);// 27 }

start main() a=1; end main() a=a+a+a; a=a*a*a; System.out.println(“a == “ + a);

start main() a=1; System.out.println(“a == “ + a); a=a+a+a; System.out.println(“a == “ + a); a=a*a*a; System.out.println(“a == “ + a); end main() 1 a 327

Sequences start end

Sequence branching start end

Sequence branching start end

Sequence branching start end

Sequence branching start a=1; end a=a+a; a=a*a System.out.println(“a == “ + a); a<3 true false

Sequence branching start a=1; end a=a+a; System.out.println(“a == “ + a); a<3 true

Sequence branching start a=6; end a=a*a System.out.println(“a == “ + a); a<3 false

Sequence Branching public static void main(String[] args) { int a; a = 1 ; if (a < 3) { a = a + a; } else { a = a * a; } System.out.println(“a == “ + a);//2 }

Sequence Branching public static void main(String[] args) { int a; a = 6 ; if (a < 3) { a = a + a; } else { a = a * a; } System.out.println(“a == “ + a);//36 }

Slightly more practical start READ INCOME end tax = income *.3; tax = income *.4 System.out.println(“take home == “ + (income-tax) ); income < true false

Review Reading Numbers static double myGetDouble() { String localInput = ""; InputStreamReader reader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(reader) try { localInput = console.readLine(); } catch (IOException e) { System.out.println(e + " bad read "); System.exit(0); } return Double.parseDouble(localInput); }

Sequence Branching public static void main(String[] args) { double income, tax; final double taxRate1 = 0.3, taxRate2 = 0.4; income = getMyDouble() ; if (income < 75000) { tax = income * taxRate1; } else { tax = income * taxRate2; } System.out.println(“take == “ + (income - tax)); }

Blocks On single statements brackets are optional if (x < 4) System.out.println(“x less than 4”); else System.out.println(“x is not less than 4”); But Beware: if (x < 4) System.out.println(“x less than 4”); System.out.println(“that’s a small num!”);

Else is optional if (income > ) tax = tax + (income * extraTaxRate);

“simple” if branching start end true false ?

What about the conditions? if (income > ) tax = tax + (income * extraTaxRate); These are “boolean” expressions They have a value of “true” or “false”

Relational Operators < // less than <= // less than or equal to > // greater than >= // greater than or equal to == // equal to != // not equal to

Can be used in clauses double income = ; if (income > ) System.out.println(“Pay up!”);

Can be abstracted boolean b1; double income = ; b1 = ( income > ); if (b1) System.out.println(“Pay up!”);

Using “AND” double inc = ; int numChild = 5; if ((inc 3)) System.out.println(“Pay less”);

Using “OR” double inc = ; int numChild = 5; if ((inc 3)) System.out.println(“Pay less”);

Using “NOT” double inc = ; int numChild = 5; if !(!(inc 3)) System.out.println(“Pay less”);