Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
CMT Programming Software Applications
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
C++ for Engineers and Scientists Third Edition
Java Basics (continued)
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
2440: 211 Interactive Web Programming Expressions & Operators.
Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
CSC Programming I Lecture 5 August 30, 2002.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
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.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Warm-Up: Monday, March 24 List as many commands in Java as you can remember (at least 10)
4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the arithmetic and concatenation operators to provide.
Chapter 05 (Part III) Control Statements: Part II.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
The Math Class Methods Utilizing the Important Math Operations of Java!
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CPS120: Introduction to Computer Science Decision Making in Programs.
Application development with Java Lecture 6 Rina Zviel-Girshin.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Computing with C# and the.NET Framework Chapter 4 More Control Structures and Types ©2003, 2011 Art Gittleman.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
COMP Loop Statements Yi Hong May 21, 2015.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
Java Basics (continued) Ms. Pack AP Computer Science A.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
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.
Java Fundamentals 4.
Chapter 4: Control Structures I
C# and the .NET Framework
Multiple variables can be created in one declaration
JavaScript: Control Statements I
SELECTION STATEMENTS (1)
Arithmetic operations, decisions and looping
Chapter 4: Mathematical Functions, Characters, and Strings
Selection (if-then-else)
Basics of ‘C’.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Control Structure.
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

Lesson 6 Selection Structures

Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class Grades { public static void main (String args []) { //Variables double grade1,grade2, grade3, total; //End of Variables //Input KeyboardReader reader = new KeyboardReader(); System.out.print ("Please enter in the first grade: "); grade1 = reader.readDouble(); System.out.print ("Please enter in the second grade: "); grade2 = reader.readDouble(); System.out.print ("Please enter in the third grade: "); grade3 = reader.readDouble(); //End of Input //Calculations total = (grade1 + grade2 + grade3) / 3.0; System.out.println ("The average is: "+total); //End of Calculations //If Statements if (total >= 94) System.out.println ("The letter grade is: A"); if (total >= 84 && total <94) System.out.println ("The letter grade is: B"); if (total >= 76 && total <84) System.out.println ("The letter grade is: C"); if (total >= 68 && total <76) System.out.println ("The letter grade is: D"); if (total <68) System.out.println ("The letter grade is: F"); }

Compound Assignment Operations Simple Assignment Compound Addition Compound Subtraction Compound Multiplication Compound Division Compound Remainder = += -= *= /= %= (integers only)

Compound Assignment Equalities x = x + 10 x = x – 10 x = x * 10 x = x / 10 x = x % 10 x += 10 x –= 10 x *= 10 x /= 10 x %= 10

Compound Assignments Translate the following statements to equivalent statements that use extended assignment operators: a. X *=2a. X = X *2; b. Y = Y % 2; b. Y %= 2

Math Class The math class is quite extensive but we will concentrate a just a few of it’s properties: static int abs(int x)Returns the absolute value of an integer x static double abs (double x)Returns the absolute value of a double x static double pow(double base, double exponent) Returns the base raised to the exponent. static long round (double x)Returns x rounded to the nearest whole number. static int max (int a, int b)Returns the greater of a and b static int min(int a, int b)Returns the lesser of a and b static double sqrt (double x)Returns the square root of x.

Examples of Math Class Methods int m; double x; m = Math.abs(-7) // m equals 7 x = Math.abs(-7.5)// x equals 7.5 x = Math.pow(3.0,2.0)// x equals 3.0^2.0 = 9.0 x = Math.pow(16.0,.25)// x equals 16.0 ^.25 = 2.0 m = Math.max(20,40)// m equals 40 m = Math.min(20,40)// m equals 20 m = (int) Math.round(4.27)// m equals 4

Round to two decimal places (double) Math.round(answer*100)/100

Math Class //Given the area of a circle, compute its radius. double area = 10.0, radius; radius = Math.sqrt(area / Math.PI); Math.PI is accurate to about 17 decimal places

Random Class The Random class has two methods that will generate a random integer or double. MethodWhat is Does int nextInt(int n)Returns a integer chosen at random from among 0,1,2,3,…,n-1 double nextDouble()Returns a double chosen at random between 0.0 and 1.0, inclusive.

Example of Random Class Methods Import java.util.Random; Random generator = new Random(); int i; double j; i = generator.nextInt(3);// would give a // random number 0,1, or 2. j = generator.nextDouble();// would give a // random number // between 0 and 1

Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a structured programming language are sequence, selection, and iteration. Sequence control structure is what you have been doing up until now. The second two is what we are going to take a look at next.

Selection/Iteration Structure Selection and Iteration allow the flow of the program to be altered, depending on one or more conditions. Selection is implemented by using a if, if/else, and switch statements. Iteration is implemented by using the while, do/while, and for statements.

The IF statement The if statement works much the same as a if/then statement in Visual Basic. It uses relational operators to test if something is true or false. If it is true, the program will execute the statement(s) within the if statement. If it is false, the program will bypass the statements and continue with the statements following the if statement.

Syntax of the If Statement if (test expression) { statement1; statement2; statementn; } //this is an example of a compound statement

IF/ELSE Statement The IF/ELSE statement works in the same manner as the if/then/else in Visual Basic. It is considered a double-alternative statement. If the expression evaluates as true, then the statements inside the if are executed. If the expression evaluates as false, then the statements inside the else are executed.

Syntax for if/else if (test expression) { statement1; statement2; statementn; } else { statement1; statement2; statementn; } //example of compound statements

Relational Operators Relational operators allow two quantities to be compared. = =Equal to ! =Not equal to <Less than < =Less than or equal >Greater than > =Greater than or equal

Logical Operators

Switch Statement The switch statement works in the same manner as the case select statement in Visual Basic. A selector variable is first evaluated to produce a value. The selector is then compared to a series of cases. If the selector value matches one of the case values, the corresponding case statements are executed.

Syntax for a Switch Statement switch (selector variable) //must be int or char { case case1value :case1statements; break; case case2value :case2statements; break; case casenvalue : case_N_statements; break; default : case exception statements; }

Escape Sequences \b \t \n \” \’ \\ Backspace Tab Newline or line feed Double Quote Single Quote Backslash

Bottoms Up On The Fourth Cup