The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.

Slides:



Advertisements
Similar presentations
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Advertisements

INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
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.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Introduction to Computer Programming Decisions If/Else Booleans.
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.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement.
The switch statement: an N-way selection statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
JAVA Control Statement.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Boolean expressions Conditional statements and expressions.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. CHAPTER 3: SELECTIONS 1.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Assignment statements using the same variable in LHS and RHS.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern.
Decisions Bush decision making.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
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" ©
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Decisions (If Statements) And Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
Introduction to programming in java
Web & Systems Developer, 30 credits
CS0007: Introduction to Computer Programming
Introduction to programming in java
Chapter 6 More Conditionals and Loops
Computer Programming Methodology Input and While Loop
TK1114 Computer Programming
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)
הרצאה 3 אלמנטים בסיסיים בשפה
Conditional Loops.
Chapter 6 More Conditionals and Loops
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
An Introduction to Java – Part I, language basics
The Boolean (logical) data type boolean
בתרגול הקודם אתר הקורס (הודעות, פרטי סגל הקורס, עבודות, פורום, מערכת הגשת תרגילים וכו') שימוש בחלון ה-command, פקודות בסיסות קוד Java: הידור (= קומפילציה)
Self study.
Lecture Notes – Week 2 Lecture-2
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Repetition Statements
The for-statement.
Presentation transcript:

The if-else statement

The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one of the two possible statements to be executed based on a given condition Example: if ( condition is true ) then execute this statement; otherwise execute the other statement;

Syntax and meaning of the if-else-statement Syntax of the if-else-statement : if ( CONDITION ) ONE-statement else ONE-statement The keyword if announces (to the Java compiler ) that we started an if- else-statement A conditional clause ( CONDITION ) follows the keyword if Following the condition clause, you can write (only) one statement Following the then-part, you must specify the keyword else followed by (only) one statement

Programming example: find maximum of 2 numbers (1)

Programming example: find maximum of 2 numbers (2) import java.util.Scanner; public class Max01 { public static void main(String[] args) { double a, b, max; Scanner in = new Scanner(System.in); // Construct Scanner object a = in.nextDouble(); // Read in next number into a b = in.nextDouble(); // Read in next number into b if ( a >= b ) max = a; else max = b; System.out.println( "max value = " + max ); }

Program example: find maximum of 3 numbers (1)

Program example: find maximum of 3 numbers (2) import java.util.Scanner; public class Max01 { public static void main(String[] args) { double a, b, max; Scanner in = new Scanner(System.in); // Construct Scanner object a = in.nextDouble(); // Read in next number into a b = in.nextDouble(); // Read in next number into b c = in.nextDouble(); // Read in next number into c if ( a >= b ) // Find max(a,b) max = a; else max = b; if ( c > max ) // Check c > max ? max = c; System.out.println( "max value = " + max ); }

Programming example: leap year (1) In the Gregorian calendar, the current standard calendar in most of the world, most years that are evenly divisible by 4 are leap years. Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years Year Leap year ? Reason ================= 1904 Yes Divisible by No Divisible by Yes Divisible by 400

Programming example: leap year (2)

Programming example: leap year (3) import java.util.Scanner; public class LeapYear01 { public static void main(String[] args) { int year; boolean leap; Scanner in = new Scanner(System.in); // Construct Scanner object year = in.nextInt(); // Read in year if ( year % 4 == 0 ) leap = true; else leap = false; if ( year % 100 == 0 ) leap = false; if ( year % 400 == 0 ) leap = true; System.out.println("Year is leap year ? " + leap); }

Programming example: leap year (4) import java.util.Scanner; public class LeapYear02 { public static void main(String[] args) { int year; boolean leap; Scanner in = new Scanner(System.in); // Construct Scanner object System.out.print("Enter year:"); year = in.nextInt(); // Read in year if ( (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0) ) { System.out.println("It is a leap year"); } else { System.out.println("It is NOT a leap year"); }

Comparing floating point values on equality (and inequality) (1) When are 2 values equal to each other: Two values are equal if they are equal in all digits Consequently: !=

Comparing floating point values on equality (and inequality) (2) public class FloatEq1 { public static void main(String[] args) { double a, b; int i; a = 4.0; b = 4.0/ / / / / / /7.0; System.out.println("a = " + a); System.out.println("b = " + b); if ( a == b ) { System.out.println("a is equal to b"); } else { System.out.println("a is NOT equal to b"); }

Testing equality within a given tolerance (1) When we want to test if 2 values a and b are approximately equal to each other, we use this test: if ( absoluteValue( b − a ) < some-very-small-value ) { a and b are equal } else { a and b are not equal (too far apart) }

Testing equality within a given tolerance (2) public class FloatEq2 { public static void main(String[] args) { double a, b; int i; a = 4.0; b = 4.0/ / / / / / /7.0; System.out.println("a = " + a); System.out.println("b = " + b); if ( Math.abs( b - a ) < ) { System.out.println("a is (approximately) equal to b"); } else { System.out.println("a is NOT (approximately) equal to b"); }

Nested conditional statements

A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement or an if-else-statement in the then- part (and in the else-part) of a conditional statement !!! Nested conditional statement = a conditional statement where the then-part and/or the else-part contains another conditional statement

Programming example: determine the price for a hair cut (1)

Programming example: determine the price for a hair cut (2)

Programming example: determine the price for a hair cut (3)