Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.

Slides:



Advertisements
Similar presentations
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Advertisements

5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C++ for Engineers and Scientists Third Edition
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
The Java Programming Language
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Introduction to Java Java Translation Program Structure
11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.
Chapter Making Decisions 4. Relational Operators 4.1.
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Branching statements.
Chapter 4: Control Structures I (Selection)
Chapter 5: Control Structures II (Repetition)
Selection (also known as Branching) Jumail Bin Taliba by
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.
Chapter 4: Making Decisions.
Methods Chapter 6.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Control Structures – Selection
Chapter 4: Control Structures I
Chapter 4: Control Structures I (Selection)
Control Structures: Selection Statement
C++ Statements and Functions
Chapter#3 Structured Program Development in C++
Chapter 7 Conditional Statements
Problem Solving, Object-Oriented Design and Java
Chapter 4 Selection.
Problem Solving, Object-Oriented Design and Java
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
B.Ramamurthy Copyright 1998 CSE Department
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Structured Program Development in C++
Decisions, decisions, decisions
Control Structures: Selection Statement
Presentation transcript:

Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR

Decision Statements (General) How to compare data values? Relational operators How to alter the sequence of program execution based on the result? If.. Else statements in non-event-driven programming Logical operators (&&, || and ! ) and their use. How to deal with multiple choices? Switch statement 4/23/2019 BR

Boolean expressions Any expression (variable) that evaluates to TRUE or FALSE is a Boolean expression (variable). example: boolean Raining; Boolean expression can be formed using logic operators (AND, OR , NOT) and/or relational operators used for comparison. 4/23/2019 BR

Logical expressions boolean OnProbation, OutOfSchool, BackInSchool; float GPA; /* if OnProbation and GPA < 2.0 then OutOf School is TRUE else OutOfSchool is FALSE*/ /* if OnProbation and GPA >= 2.0 then BackInSchool is TRUE else BackInSchool is FALSE*/ OutOfSchool = OnProbation && (GPA < 2.0); BackInSchool = OnProbation && ( GPA >= 2.0); /*Actually OutOfSchool is the negation of BackInSchool */ 4/23/2019 BR

if Statement An if statement allows a program to choose whether or not to execute a following statement. Syntax: if (condition) statement; Semantics: condition is a Boolean expression: Something that evaluates to True or False. If condition is true then execute the statement is executed. 4/23/2019 BR

If -else Statement An if-else statement allows a program to do one thing if a condition is true and a different thing if the condition is false. Syntax: if ( condition ) statement1 else statement2 Statements to be executed for if and else can be a single statement or multiple statements enclosed in { }. 4/23/2019 BR

Nested if Statement A body of an if statement could be another if statement. This situation is called nested-if. In this case, an else is matched with the most recent unmatched if. A nested-if allows a program to make decisions based on the results of the previous decision. 4/23/2019 BR

Nested if Let the integers be N1, N2 and N3. if ( N1 > N2) Largest = N1; else /* ASSERT : N3 > N1 and N1 > N2 */ Largest = N3; else /* ASSERT : N2 > N1 */ if (N2 > N3) Largest = N2; else /* ASSERT : N3 > N2 and N2 > N1 */ 4/23/2019 BR

Switch ... syntax switch (selector) { case label1: statements1; break; ..... case labeln : statementsn; default : statementsd; } 4/23/2019 BR

switch ... semantics Switch statement is used when there are more two alternatives to select from.. selector is an ordinal expression: That means that the values it can take should be finite, countable: integer, char, bool types are acceptable but not float. The selector type and case label type should be same. Each case label must be distinct. The selector expression is evaluated and compared to each of the case labels. If the value of the selector is one of the case labels, say label x, then the statementsx is executed. 4/23/2019 BR

switch ... semantics Execution continues on until a break statement is encountered. At this point, control is transferred to the next statement after the switch. If the selector value matches none of the labels, the default statements are executed, if one is provided. Though default provision is optional, it is a good programming practice to always have a default clause. 4/23/2019 BR

switch ... Example Problem: Write a switch statement to print out the grade points corresponding to the letter grades “A”, “B” , “C”, “D”, and “F”. switch (letter) { case ‘A’ : points = 4.0; break; case ‘B’ : points = 3.0; case ‘C’ : points = 2.0; break; case ‘D’ : points = 1.0; break; case ‘F’ : points = 0.0; break; default : cout << “error in grade” << endl; } cout << points << endl; 4/23/2019 BR

Control Structures Selection: if, if..else, switch Selection is used to implement “mutual exclusion”; when selective execution of a piece of code is required. Iteration: while, do..while, for Iteration is used when repeated execution of a piece of code is needed. 4/23/2019 BR

Reference Types (arrays) A.2.2 Examples of array usage: int primes1 [] = {2,3,5,7,11}; int primes2[ ] = new int[5]; int primes3[ ] = primes2; int primes3[4] = 11; double dip [] ; dip = new int [4][5]; 4/23/2019 BR

Strings A.3.3 Basic Strings are non-mutable objects. String s = “”; int i; for (I=1; I <= 10; I++) { s = s + “ “ + i; } System.out.println(s); 4/23/2019 BR

What is an Object? Everything around you is an object. How would you describe an object? Using its characteristics (has a ----?) and its behaviors (can do ----?) Object must have unique identity (name) : Basketball, Blue ball Consider a ball: Color and diameter are characteristics (Data Declarations) throw, bounce, roll are behaviors (Methods) 4/23/2019 BR

Classes are Blueprints A class defines the types of data associated with an object and the methods allowed on the data. The process creating an object from a class is called instantiation. Every object is an instance of a particular class. There can be many instances of objects from the same class possible with different values for data. 4/23/2019 BR

Instantiation : Examples class FordCar ---- defines a class name FordCar FordCar Windstar; ---- defines a Object reference WindStar Windstar = new FordCar(); ---- instantiates a Windstar Object class HousePlan1(color) HousePlan1 BlueHouse, GreenHouse; BlueHouse = new HousePlan1(Blue); GreenHouse = new housePlan1(Green); 4/23/2019 BR

Operator new and “dot” new operator creates a object and returns a reference to that object. After an object has been instantiated, you can use dot operator to access its methods and data declarations (if you access permissions). 4/23/2019 BR

Java API and class libraries JAVA Application Programming Interface (JAVA API) Package of related classes : java.awt Random java.util Date Dictionary Java.io, java.beans,.. Etc.. package class 4/23/2019 BR

Using Predefined Classes Java API contains a large number of predefined classes for use in your program (application). String class in one of them. It is defined in java.lang package. Instantiate an object MyName and initialize it to your name: 4/23/2019 BR

Problem Solving Using Classes From the problem statement decide the classes you need. Many of the classes you need may be predefined in Java API. Design the other classes by specifying the data and methods. Use the classes by instantiating objects from the classes invoking the methods 4/23/2019 BR

A complete example Problem Statement: You have been hired to assist in an secret encryption project. In this project each message (string) sent out is attached to a randomly generated secret code (integer) between 1 and 999. Design and develop an application program in Java to carry out this project. 4/23/2019 BR

Identify Objects There are two central objects: Message Secret code Is there any class predefined in JAVA API that can be associated with these objects? Yes , “string” of java.lang and “Random” of java.util 4/23/2019 BR

Design Class Random Class String An instance of Random number An instance of string Class Random An instance of Random number Input and fill up message. Generate Random integer Attach (concatenate) Output combined message. 4/23/2019 BR

Implementation On the overhead projector 4/23/2019 BR

Debugging and Testing Compile-time Errors : Usually typos or syntax errors (Solution: debugging) Run-time Errors : Occurs during execution. Example: divide by zero . These are known as exceptions. C++ and Java provide special exception handling mechanism to take care of these errors. (Solution: debugging) Logic Errors: Software will compile and execute with no problem, but will not produce expected results. (Solution: testing and correcting) 4/23/2019 BR

Summary Basic statements of Java Control Structures Reference types Using classes available in the Java API “structures” package available with the text provides an easier way to do IO. Next class we will start design of classes and methods. 4/23/2019 BR