CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.

Slides:



Advertisements
Similar presentations
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Advertisements

3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
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.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue 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.
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.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
DAT602 Database Application Development Lecture 5 JAVA Review.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Final Exam Review Closed book Closed laptop One sheet of notes permitted SE-0010 Dr. Mark L. Hornick 1.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Control statements Mostafa Abdallah
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Application development with Java Lecture 6 Rina Zviel-Girshin.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
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.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Relational Operator and Operations
CiS 260: App Dev I Chapter 4: Control Structures II.
Looping and Repetition
The University of Texas – Pan American
Chapter 8 JavaScript: Control Statements, Part 2
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
3 Control Statements:.
Computer Science Core Concepts
Flow of Control.
CprE 185: Intro to Problem Solving (using C)
Looping and Repetition
Presentation transcript:

CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions

CS-1010 Dr. Mark L. Hornick 2 Instructions in a Java program are executed in sequence Starting with the first instruction in your main() method and continuing to the end. To alter the control flow, decision-making instructions can be added to a program An instruction that alters the control flow is called a control statement or selection statement. public static void main(…) … }

CS-1010 Dr. Mark L. Hornick 3 The Java if statement specifies whether a block of code should execute …depending on the result of evaluating a test condition called a boolean expression public static void main(…) { if( ){ // block of code that executes when // the boolean expression is true } }

CS-1010 Dr. Mark L. Hornick 4 What is a boolean expression? First recall: A numerical expression uses arithmetic operators and is evaluated to a numerical value Some numerical expressions using binary arithmetic operators: x+y x-y x*y x/y Arithmetic operators operate on numerical datatypes byte, short, int, long, float, double Exception: (+ operator works for the String datatype)

CS-1010 Dr. Mark L. Hornick 5 The boolean datatype is another primitive datatype Variables declared as boolean datatypes can only assume one of the two boolean values: true false boolean isOK = false; boolean insufficientFunds = true; boolean, true, and false are Java reserved words

CS-1010 Dr. Mark L. Hornick 6 A boolean expression uses relational operators between numerical values and is evaluated to a boolean value: either true or false. x < y// less than operator; evaluates to true if x is less than y; otherwise false x <= y// less than or equal to; note =< is not valid x > y// greater than x >= y// greater than or equal to x == y// equal to; be careful w.r.t. x=y x != y// not equal to

CS-1010 Dr. Mark L. Hornick 7 Variables of data type boolean can be declared and assigned to the result of a boolean expression: int score = 86; boolean result; result = 70 < score; if ( result ){ … }

CS-1010 Dr. Mark L. Hornick 8 The { } braces are optional, but this means that the block of code is the single instruction following the if statement if ( ) // executed if true // executed always This means that whatever Java statement following the if statement will be executed when the boolean expression is true And only the single instruction following the if(…)

CS-1010 Dr. Mark L. Hornick 9 Methods can be declared to return a boolean value The String class has a contains() method that returns a boolean result: String message = “Welcome to MSOE”; boolean isPresent = message.contains(“to”); if( isPresent ) // contains “to” JOptionPane.showInputDialog(…);

CS-1010 Dr. Mark L. Hornick 10 A more complex if statement if ( ){ // executed when exp. is true } else { }// executed when exp. is false The else clause is optional When present, the instructions in the else block are executed only when the boolean expression is false

CS-1010 Dr. Mark L. Hornick 11 The Boolean operators Repeat: relational operators take numerical operands and return a boolean value: either true or false. Example: (x<y) Exception: == and != operators can be used between boolean datatypes A boolean operator takes boolean values as its operands and returns a boolean value. The three boolean operators are &&// means “and” ||// means “or” !// means “not”

CS-1010 Dr. Mark L. Hornick 12 Boolean operators and their meanings: PQP && QP || Q!P false true falsetruefalsetrue false truefalse true false

CS-1010 Dr. Mark L. Hornick 13 Complex boolean expressions int x = 100, t=50; boolean passed, record; // initialized to false passed = (x >= 70); // or !(x < 70) record = (t = 60) if( pass && record ) msg = “You passed in record time!”;

CS-1010 Dr. Mark L. Hornick 14 Complex boolean expressions int x = 100, t=50; boolean pass, record; // initialized to false pass = (x >= 70); // evaluates to true record = (t < 60); // evaluates to true boolean both = pass && record; if( both ){ msg = “You passed in record time!”; } // always safer to include { }

SE-1010 Dr. Mark L. Hornick 15 Looping and Repetition Statements control a block of code to be executed for: a fixed number of times 1, 10, 10000… until a certain condition is met a value remains positive (or negative) a value remains below (or above) some limit …

SE-1010 Dr. Mark L. Hornick 16 The two (three) main loop types: while() and it’s cousin do…while() Sentinel-controlled loops, where the loop body is executed repeatedly until a designated condition, called a sentinel, is encountered. for() A count-controlled loop, where the loop body is executed a fixed number of times.

SE-1010 Dr. Mark L. Hornick 17 The do-while() Statement Format: do { } while ( ); The part is known as the loop body The loop body is executed until the becomes false And is always executed at least once! Even if is false

SE-1010 Dr. Mark L. Hornick 18 The do-while() statement Demo

SE-1010 Dr. Mark L. Hornick 19 The while Statement Format: while ( ){ // 0 or more } As long as the is true, the loop body is executed

SE-1010 Dr. Mark L. Hornick 20 JOptionPane Confirmation dialog Created by using the showConfirmDialog method of the JOptionPane class: int answer = JOptionPane.showConfirmDialog(null, “Play another game?", “Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); // answer will contain the values corresponding // to JOptionPane.YES_OPTION or JOptionPane.NO_OPTION