Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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.
Lecture 9. Today’s topic Let’s continue to do exercise (Lecture 8) Character data type –Char String class –String.
Introduction to Computer Programming Decisions If/Else Booleans.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 Tirgul no. 2 Topics covered: H Reading Input from the user. H Printing Output to the user. H if - else statement and switch. H Boolean Operators. H Checking.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Lecture 8. Review (Methods of Math class) int abs( int num ) double sqrt( double num ) double pow( double num, double power ) Method parametersData type.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
CONTROLLING PROGRAM FLOW
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Control statements Mostafa Abdallah
Application development with Java Lecture 6 Rina Zviel-Girshin.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Lesson thirteen Conditional Statement "if- else" ©
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
CompSci 230 S Programming Techniques
Lecture 3 Selection Statements
Building Java Programs
Introduction to programming in java
Factoring if/else code
Conditionals & Boolean Expressions
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
SELECTION STATEMENTS (1)
Topic 11 Scanner object, conditional execution
Building Java Programs
Control Statement Examples
Building Java Programs
Building Java Programs
Life is Full of Alternatives
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Building Java Programs
Building Java Programs
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
Building Java Programs
Presentation transcript:

Lecture 11

Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement

3 Review (Equality and Relational operator) Similar to arithmetic operators Equality operator == equal to != not equal to Relational operator < less than > greater than <= less than or equal to >= greater than or equal to

Boolean expression is –a condition that evaluates either true or false –It contains equality or relational operators –It returns boolean results Example int num = 5; boolean flag; flag = num >= 3; Review (boolean expressions) Equality operator Assignment operator true Boolean expression Ask like “is the value of num greater than or equal to 3 ?”

Examples boolean a; a = (15 / 3 < 5); System.out.println(“a contains a value “ + a); double num = 2.5; a = (2.5 >= num); System.out.println(“a contains a value “ + a); false true

6 if Statement The if statement has the following syntax: if ( condition ) { statement; } if is a Java key word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.

7 If-else Statement if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement1 is executed. If it is false, the statement2 is executed.

8 If-elseif Statement if ( condition1 ) { statement1; } else if ( condition2 ) { statement2; } else { statement3; } The condition1 and condition2 must be a boolean expression. It must evaluate to either true or false. If the condition1 is true, the statement1 is executed. If the condition1 is false and condition2 is true, the statement2 is executed. Otherwise statement3 is executed

int MAX = 10; int num = 15; if ( num < MAX ) { System.out.println(“num is smaller?”); } else { System.out.println(“num is bigger!!”); } Print as num is bigger!! Example

Exercise Write a class named MyConverter The converter will ask users to enter one of conversions they want to perform –1. Mile to Kilometer conversion –2. Fahrenheit to Celsius conversion Ask users to enter corresponding values Print out the result that users want

public class MyConverter { public static void main(String[] argv) { JFrame frame = new JFrame(“My Calculator"); IOConsole console = new IOConsole(); frame.getContentPane().add(BorderLayout.CENTER, console); frame.setSize(500, 300); frame.setVisible(true); console.println( “********** MENU ***********”); console.println( “ 1. Distance (Mile->Kilo)” ); console.println( “ 2. Temperature (F->C) ” ); console.println( “******************************”); next page

int option; for( int i=0; i<10; i++) { option = console.readInt( “Which conversion ? > “ ); if ( option == 1 ) { } else if ( option == 2 ) { } else { } Write statements to convert Mile to Kilometer Write statements to convert F to C Print out error messages for users