Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CS 106 Introduction to Computer Science I 09 / 13 / 2006 Instructor: Michael Eckmann.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Section 5.1 Keyboard Input.
Program Statement Avoid Spaghetti Programming Program Statement.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Java Programming: From the Ground Up
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Java Programming, Second Edition Chapter Five Input and Selection.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Review of Math Class Methods abssqrt minmax powround ceilfloor.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
George Boole More than 150 years ago, there was a mathematician named George Boole, who took statements and wrote them in a precise format, such that.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
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.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
George Boole More than 150 years ago, there was a mathematician named George Boole, who took statements and wrote them in a precise format, such that.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 4: Control Structures SELECTION STATEMENTS.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
Visual Classes 1 Class: Bug 5 Objects: All Bug Objects.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Computer Programming1 Computer Science 1 Computer Programming.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
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.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
// Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 5: Control Structures II
PowerPoint Presentation Authors of Exposure Java
INPUT STATEMENTS GC 201.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Lec 4: while loop and do-while loop
Introduction to Java Programming
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Presentation transcript:

Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.

Conditional Statement Definition A conditional statement is a program expression that evaluates to true or false. Most conditional statements require a relational operator. All conditions must be placed inside (parentheses).

Relational Operators NameOperatorExpressionEvaluates Equals == 5 == 5 K == 10 true depends Not Equals != 50 != != 100 true false Less than < 100 < 200 P < Q true depends Greater than > 100 > 200 P > Q false depends Less than or equals <= 25 <= <= 25 true Greater than or equals >= 1000 >= 1000 K >= (P + Q) true depends

One-Way Selection Syntax One-Way selection general syntax: if (condition true) execute program statement if (counter > 100) System.out.println("Counter exceeds 100"); Use braces { } and block structure to control multiple program statements. if (cavings >= 10000) { System.out.println("It’s skiing time"); System.out.println("Let’s pack"); System.out.println("Remember your skis"); }

Indentation Rule: Java syntax uses freeform program style. Program statements may be placed on multiple lines with or without indentation. By convention, control structures and their conditional statements are placed on one line. The program statement that is executed, if the condition is true, is placed on the next line, and indented below the conditional statement. if(sales >= ) bonus = 1000; if(sales >=500000) bonus = 1000;

Two-Way Selection Syntax Two-Way selection general syntax: if (condition true) execute first program statement else // when condition is false execute second program statement if (gpa >= 90.0) System.out.println ( "You’re an honor graduate"); else System.out.println ("You’re not an honor graduate");

Multiple-Way Selection Program Statement Condition True False Program StatementCondition True False Program StatementCondition True False

Multiple-Way Selection Syntax General Syntax: switch(selectionVariable) { case selectionConstant: program statement; break; default : program statement; } The default statement is used to handle the situation when a proper match is not found. Frequently an error message is used to indicate that no match was found.

Multiple-Way Selection Block-Structure Syntax NO-NO Example The extra braces accomplish nothing and even prevent the break commands from working. switch(courseGrade) { case ’A’:{ points = 4; System.out.println(“Excellent!”); break; } case ’B’:{ points = 3; System.out.println(“Good”); break; } case ’C’:{ points = 2; System.out.println(“Fair”); break; } case ’D’:{ points = 1; System.out.println(“Poor”); break; } case ’F’:{ points = 0; System.out.println(“Bad”); break; } default : points = 0; }

Multiple-Way Selection Block-Structure Syntax Yes-Yes Example The switch statement is the only control structure that does not use braces for block structure. The break command at the end of each case is all it needs to keep each case separate. switch(courseGrade) { case ’A’:points = 4; System.out.println(“Excellent!”); break; case ’B’:points = 3; System.out.println(“Good”); break; case ’C’:points = 2; System.out.println(“Fair”); break; case ’D’:points = 1; System.out.println(“Poor”); break; case ’F’:points = 0; System.out.println(“Bad”); break; default : points = 0; }

// Java0501.java // This program (and the next few programs) demonstrate // user keyboard input during program execution. // This particular program demonstrates keyboard input of // a String in a text window using the Scanner class. public class Java0501 { public static void main (String args[]) { System.out.println("\nJAVA0501.JAVA\n"); System.out.print("Enter name ===>> "); // Line 1 Scanner input = new Scanner(System.in); // Line 2 String name = input.nextLine(); // Line 3 System.out.println("\nName Entered: " + name); System.out.println(); }//end of main }//end of program Line 1 is called the prompt. It asks or “prompts” the user for information. Line 2 is the line that actually scans the screen until the Enter key is hit. Line 3 is the line that stores the data entered on the screen in a variable.

// Java0502.java // This program demonstrates how to use nextLine( ) method of the Scanner class // for three separate String keyboard inputs. public class Java0502 { public static void main (String args[]) { System.out.print("Enter Line 1 ===>> "); Scanner input1 = new Scanner(System.in); String firstName = input1.nextLine(); System.out.print("Enter Line 2 ===>> "); Scanner input2 = new Scanner(System.in); String midName = input2.nextLine(); System.out.print("Enter Line 3 ===>> "); Scanner input3 = new Scanner(System.in); String lastName = input3.nextLine(); System.out.println(); System.out.println(firstName); System.out.println(midName); System.out.println(lastName); System.out.println(); }//end of main } //end of program

// Java0503.java // This program demonstrates objects concatenation with // keyboard entered data. public class Java0503 { public static void main (String args[]) { System.out.println("\nJAVA0503.JAVA\n"); System.out.print("Enter 1st Number ===>> "); Scanner input1 = new Scanner(System.in); String number1 = input1.nextLine(); System.out.print("Enter 2nd Number ===>> "); Scanner input2 = new Scanner(System.in); String number2 = input2.nextLine(); String sum = number1 + number2; System.out.println(); System.out.println(number1 + " + " + number2 + " = " + sum); System.out.println(); }//end of main }//end of program

Addition vs. Concatenation The problem with the previous program is that String data type was used, instead of int or double. When the plus sign ( + ) is used with a numerical value, like an int or a double, it performs addition. However, when the plus sign is used with two String variables, it joins the String s together. This is called String Concatenation.

// Java0504.java // This program uses the nextInt() method of the Scanner class to enter // integers from the keyboard. It is now possible to correctly add the two // numbers. public class Java0504 { public static void main (String args[]) { System.out.println("\nJAVA0504.JAVA\n"); System.out.print("Enter 1st Number ===>> "); Scanner input1 = new Scanner(System.in); int number1 = input1.nextInt(); System.out.print("Enter 2nd Number ===>> "); Scanner input2 = new Scanner(System.in); int number2 = input2.nextInt(); int sum = number1 + number2; System.out.println(); System.out.println(number1 + " + " + number2 + " = " + sum); System.out.println(); }//end of main }//end of program

// Java0505.java // This program demonstrates how to use nextDouble( ) method of the Scanner class // for three separate double keyboard inputs, which are used to display the mean. public class Java0505 { public static void main (String args[]) { System.out.println("\nJAVA0505.JAVA\n"); System.out.print("Enter Number 1 ===>> "); Scanner input1 = new Scanner(System.in); double n1 = input1.nextDouble(); System.out.print("Enter Number 2 ===>> "); Scanner input2 = new Scanner(System.in); double n2 = input2.nextDouble(); System.out.print("Enter Number 3 ===>> "); Scanner input3 = new Scanner(System.in); double n3 = input3.nextDouble(); System.out.println(); System.out.println(n1); System.out.println(n2); System.out.println(n3); double mean = (n1 + n2 + n3) / 3; System.out.println(); System.out.println("The mean is " + mean); System.out.println(); }//end of main }//end of program

// Java0507.java // This program demonstrates how to use GUI input methods. // It also shows a GUI display. public class Java0507 { public static void main (String args[]) { String firstName = JOptionPane.showInputDialog("Enter Student First Name."); String lastName = JOptionPane.showInputDialog("Enter Student Last Name."); String agePrompt = JOptionPane.showInputDialog(“Enter Student Age.”; int age = Integer.parseInt(agePrompt); String gpaPrompt = JOptionPane.showInputDialog(“Enter Student GPA.”; double gpa = Double.parseDouble(gpaPrompt); String sentence = firstName + " " + lastName +" is " + age + " years old, has a GPA of " + gpa; JOptionPane.showMessageDialog(null, sentence); }//end of main }//end of program

Keyboard Input Methods The following keyboard input methods are from the Scanner class: 1.nextInt() is used to enter an int from the text screen. 2.nextDouble() is used to enter a double from the text screen. 3.nextLine() is used to enter a String from the text screen. The following keyboard input methods are used in conjunction with the showInputDialog & showMessageDialog methods of the JOptionPane class, to enter data from a GUI (pop-up) window: 1.Double.parseDouble() is used to enter a double from a GUI window. 2.Integer.parseInt() is used to enter an int from a GUI window.

// Java0511.java // This program demonstrates two-way selection with. // Run the program twice: First with 1200, then with public class Java0511 { public static void main (String args[]) { System.out.println("\nJAVA0511.JAVA\n"); System.out.print("Enter SAT ===>> "); Scanner input = new Scanner(System.in); int sat = input.nextInt(); System.out.println(); if (sat >= 1100) System.out.println("You are admitted"); else System.out.println("You are not admitted"); System.out.println(); }//end of main } //end of program

// Java0512.java // This program demonstrates two-way selection with. // Multiple statements require the use of block structure. // Run the program twice: First with 1100, then with public class Java0512 { public static void main (String args[]) { System.out.println("\nJAVA0512.JAVA\n"); System.out.print("Enter SAT ===>> "); Scanner input = new Scanner(System.in); int sat = input.nextInt(); System.out.println(); if (sat >= 1100) { System.out.println("You are admitted"); System.out.println("Orientation will start in June"); } else { System.out.println("You are not admitted"); System.out.println("Please try again when your SAT improves."); } System.out.println(); }//end of main }//end of program

// Java0513.java // This program demonstrates multi-way selection with and. // This program compiles, but displays illogical output. public class Java0513 { public static void main (String args[]) { System.out.println("\nJAVA0513.JAVA\n"); System.out.print("Enter Letter Grade ===>> "); Scanner input = new Scanner(System.in); char grade = input.nextChar(); System.out.println(); switch (grade) { case 'A' : System.out.println(" Average"); case 'B' : System.out.println(" Average"); case 'C' : System.out.println(" Average"); case 'D' : System.out.println(" Average"); case 'F' : System.out.println("Below 60 Average"); } System.out.println(); }//end of main }//end of program

default is useful for input protection. Note that Java is case-sensitive.