Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.

Slides:



Advertisements
Similar presentations
The if-else Statements
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Selection (decision) control structure Learning objective
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
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 Selection Structures: Making Decisions.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
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.
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.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
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.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 4: Control Structures II
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
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.
COMP Loop Statements Yi Hong May 21, 2015.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Control Statements: Part1  if, if…else, switch 1.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Java Fundamentals 4.
REPETITION CONTROL STRUCTURE
The switch Statement, and Introduction to Looping
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.
Introduction to programming in java
CSC113: Computer Programming (Theory = 03, Lab = 01)
JavaScript: Control Statements I
Structured Program
Pages:51-59 Section: Control1 : decisions
2.6 The if/else Selection Structure
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 6 Selection Statements
Additional control structures
Pages:51-59 Section: Control1 : decisions
Presentation transcript:

Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship between the two operators. –Relational operators are evaluated after all arithmetic operators.

Relational Operators

4 > / 2 == / 2 Assume a = 4 and x = 9, a <= b

Logical Operators Logical Operators are used to compare boolean values and create a boolean result. –Logical operators are usually used to compare the results of relational operators. –Logical operators are evaluated after all relational operators. –Logical operators are && (logical And), & (boolean And), || (logical Or), | (boolean Or), ^ (boolean Exclusive Or), and ! (logical Not).

Logical Operators && (logical And): Expression1 Expression2 Expression1 && Expression2 True True True True False False False True False False False False

Logical Operators II (logical Or): Expression1 Expression2 Expression1 || Expression2 True True True True False True False True True False False False

Logical Operators && vs. & and || vs. | –Both && and || will only evaluate the expressions until it knows a result while the & and | operators will evaluate all the expressions before they return the result. –Gender == 1 || age >= 65 –Gender == 1 | age >= 65

Logical Operators ^ (logical Exclusive Or): Expression1 Expression2 Expression1 ^ Expression2 True True False True False True False True True False False False

Logical Operators ! (logical Not): Expression1 ! Expression1 True False False True

Logical Operators (4 >= 7) && (3 + 4 == 7) (4 >= 7) && (3 + 4 == 7) || (4 < 7) (4 >= 7) && (3 + 4 == 7) || (4 < 7) && true

Control Structures Control structures are used to organize actions (statements). Examples: –Sequence Structure –Selection Structure –Repetition Structure

Sequence Structures public static void main(String[] args) { MainWindow mainWindow = new MainWindow; OutputBox outputBox = new OutputBox(mainWindow); outputBox.printLine( “Welcome to”); outputBox.printLine( "Java Programming!”); } outputBox.printLine(“ Welcome to”); outputBox.printLine( “Java Programming!”); MainWindow mainWindow = new MainWindow(); OutputBox outputBox = new OutputBox( mainwindow);

Selection Structures Selection Structures allow you to write code that will select and execute specific code statements instead of other code statements.

If Selection Structure The basic structure of an if statement in Java is: if (boolean_expression) { statement; }

If Selection Structure If the boolean_expression is true then the statement is executed. If the boolean_expression is false then the statement is skipped and the next statement in the sequence is executed.

If Selection Structure If (hungry == true) { outputBox.printLine(“find some food”); } outputBox.printLine (“find some food”); hungry == true False True

If Selection Structure Let’s create the statements for the following problem: We want to categorize grades such that we will print out the corresponding letter grade. Assume 90 = A.

If/else Selection Structure The if/else statement allows you to write code that executes one statement if the boolean_expression is true and different statement if the boolean_expression is false.

If/else Selection Structure The basic structure of an if/else statement in Java is: if (boolean_expression) } statement 1; } else { statement 2; }

If/else Selection Structure If (hungry == true) { outputBox.printLine(“find some food”); } else { outputBox.printLine(“get more work done”); } outputBox.printLine(“ find some food”); Hungry == true False True outputBox.printLine( “get more work done);

If/else Selection Structure The statement inside an if/else structure may be another if/else statement. In Java it looks like: if (boolean_expression_1) { statement1; } else { if (boolean_expression_2) { statement2; } else { statement 3; }

If/else Selection Structure It may also be written as: if (boolean_expression_1) { statement1; } else if (boolean_expression_2) { statement2; } else { statement3; }

If/else Selection Structure Let’s look at our grade program again and rewrite it using the if/else structure.

If/else Selection Structure The if/else and if statements we have worked with so far execute only one statement. We can also execute multiple statements using compound statements. –Compound statements are actually multiple statements enclosed in brackets { }

If/else Selection Structures The format for an if/else statement which includes a compound statement in Java is: if (boolean_expression) { statement1; statement2; } else { statement3; statement4; statement5; statement6; }

Nesting If Statements Nesting if statements makes your program more powerful because it can handle many different situations. –Nesting occurs when one or more if structures are inside of another if statement. –The else statement is always associated with the previous if statement unless { } are used to change the associativity. Dangling else

Nested If Statements What is printed when the following is evaluated: if (y == 8) if (x == 5) outputBox.printLine(“1”); else outputBox.printLine(“2”); outputBox.printLine(“3”); outputBox.printLine(“4”);

The Conditional Operator The conditional operator is basically a short cut to the if/else structure. –It is called a ternary operator because it has three arguments that form the conditional expression. (boolean_expression? true_operation : false_operation)

The Conditional Operator If (grade > 70) { outputBox.printLine (“C”); } else { outputBox.printLine(“F”); } outputBox.printLine(grade > 70 ? “C” : “F”);

The Conditional Operator Write the conditional operator for the following if/else statement. boolean a = true, b = false, c = true; if ((a && c) && c || b) { out putBox.printLine(“true”); } else { out putBox.printLine(“false”); }

The Conditional Operator Write the conditional operator for the following if/else statement. If (grade >= 90) { outputBox.printLine(“The grade is an A”); } else if (grade >= 80) { outputBox.printLine(“The grade is an B”); } else if (grade >= 70) { outputBox.printLine(“The grade is an C”); } else { outputBox.printLine(“The grade is an F”); }

Switch Selection Structure The Switch selection structure is basically short hand for multiple if/else statements. It allows you to perform statements for a specific value of a variable when there are multiple possibilities. –The switch expression must be an integer or char result.

Switch Selection Structure switch (switch_expr) { case item_1: statement1; statement2; … break; case item_2: statement1; … break; default: statement 1; … break; }

Switch Selection Structure switch ( grade ) { case 'A': ++aCount; break; case 'B': ++bCount; break;... default: outputBox.printLine( "Incorrect grade. Enter new grade." ); break; } ++aCount Case A’ False True break ++bCount Case B’ False True break... outputBox. printLine... default False True break

Switch Selection Structure The switch should always use the break statement for each case or the structure will not work properly. Your switch statements should always have a default case for completeness purposes. Anything you can represent with a switch you can represent as a nested if/else statement.

Switch Selection Structure Write a program to determine the cost of products sold in the last week at a mail order house. They have five products whose retail prices are: product 1 - $2.98; product 2 - $4.50; Product 3 - $9.98; Product 4 - $4.49; Product 5 - $6.87 The program should ask the user to enter the product number and the quantity sold for one day. The program should use a switch statement to determine the total retail price for each product.

Switch Selection Structure What if you have multiple values you want to test for and have them execute the same “case”?

Switch Selection Structure switch (switch_expr) { case item_1: item_2: statement1; statement2; … break; case item_3: statement1; … break; default: statement 1; … break; }

Switch Selection Structure Write a program to read in letter grades and calculate how many of each letter are entered.

ListBox Class A ListBox allows a program to present multiple alternative inputs to the user in a well-defined manner. –A ListBox must be associated with a MainWindow. MainWindow mainWindow = new MainWindow(); ListBox listBox = new ListBox(mainWindow);

ListBox Class When a ListBox is created it has nothing in it. The program must add items to the list. listBox.addItem(“Item One”); –As many items as needed can be added to the list.

ListBox Class To retrieve the user’s input use the getSelectedIndex() method. int selection = listBox.getSelectedIndex(); –The first item in the list has an index of zero (0). –If the user makes no selection, then the result is ListBox.NO_SELECTION. –If the user cancels the input, then the result is ListBox.CANCEL.

Color Class Java represents colors using individual red, green, and blue components combined to create a color. –Each component is a value between 0 and 255. Black = (0,0,0) White = (255, 255, 255)

Color Class The color class also provides some predefined colors. (see page 276) –To use the predefined colors you reference them by their name: Color.magenta –To change the color of an object: objectName.setColor(Color.pink);

DrawingBoard Class The DrawingBoard class can be used to draw shapes. –To draw a line: objectName.drawLine( x1, y1, x2, y2); –To draw a circle: objectName.drawCircle(centerX, centerY, radius); –To draw a rectangle: objectName.drawRectangle(x, y, width, height);