1 / 13

Conditional Statements

Conditional Statements. Contents . If – else statement Relational operators Examples using if – else statements Programming style Nested if statements. Expression that evaluates to true or false. Block of statements that are executed if the condition is true. Statements executed otherwise.

gambhiri
Download Presentation

Conditional Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Conditional Statements Contents • If – else statement • Relational operators • Examples using if – else statements • Programming style • Nested if statements

  2. Expression that evaluates to true or false Block of statements that are executed if the condition is true Statements executed otherwise Conditional Statements The if – else statement Form of the if – else statement if ( boolean condition ) { //statement sequence } else { //alternative statement sequence } The else clause is optional, and may not be needed in every situation in which a segment of code is executed conditionally

  3. Conditional Statements Relational operators The boolean condition in an if – else statement is often expressed as a test of a relationship between two variables. Is x equal to y? Is x greater than or equal to 0? Is x less than size? Is x not equal to 0? (x == y) (x >= 0) (x < size) (x != 0) These tests are expressed in java in terms of the relational operators == tests whether the expressions on the left and right are equivalent <= Is expression on left less than or equal to exp. on right? < Is expression on left less than expression on right? > Is expression on left greater than expression on right? >= Is expression on left greater than or equal to exp. on right? != tests whether the expressions on the left and right are not equal

  4. Conditional Statements Relational operators Be sure to distinguish between the relational operator == and the assignment operator = Tests if the contents of variable x are the same as the contents of variable y x == y Assigns the value stored in variable y to variable x (overwriting what is in x and leaving y unchanged) x = y; Several of the relational operators use two characters for their symbol. There must NOT be a space between these two characters. x == y x != y x <= y x >= y

  5. Conditional Statements Example import java.util.Scanner; //needed for input stream classes publicclass ConditionalStatementExample { publicstaticvoid main (String [ ] args) { //convert a possibly negative integer received from the keyboard to //its positive (absolute) value System.out.println(“Enter an integer”); Scanner console = new Scanner(System.in); BufferedReader br = new BufferedReader(isr); int theInteger = console.nextInt( ); if (theInteger < 0) theInteger = - theInteger; System.out.println(“The absolute value is: ” + theInteger); } } If the integer is already positive, do nothing – else clause is not needed.

  6. 2 to 4 spaces Conditional Statements Programming style publicstaticvoid main(String [ ] args) { //list of statements indented 2 to 4 spaces int num1, num2, num3 = 5; num1 = 3 * num3 % 7; num2 = 5 * num3 % 11; //which number is larger if (num1 >= num2) { num1 += num3; System.out.println(“The larger pair is num1, num3”); } else { num2 += num3; System.out.println(“The larger pair is num2, num3”); } } Main block Block of stmts. contained in if -- block Block of stmts. contained in else -- block

  7. Conditional Statements Programming style Note that if there is only a single statement in the if or else block, curly brackets are not needed. If there is more than one statement in one of these blocks, the curly brackets are required. if (boolean condition) statement; else statement; if (boolean condition) { statement; statement; } else { statement; statement; } Curly brackets optional Curly brackets required

  8. If the condition is satisfied, the program returns from this function call. Only scores less than 90 are evaluated further inside this function. Conditional Statements Nested if statements Consider a function that assigns a letter grade to a numerical test score. (The test score is supplied as a parameter – writing functions will be explained fully later, right now we only wish to illustrate a nested if statement. publicchar gradeGiver(int testScore) { if (testScore >= 90) return ‘A’; else if (testScore >= 80) return ‘B’; else if (testScore >= 70) return ‘C’; else //testScore < 70 return ‘F’: } By convention, each if and else– block is indented 2 to 4 spaces.

  9. Conditional Statements Nested if statements The preceding form is somewhat difficult to follow, therefore the preferred way of writing nested if statements is to use an else if construction. publicchar gradeGiver (int testScore) { if (testScore >= 90) return ‘A’; elseif (testScore >= 80) return ‘B’; elseif (testScore >= 70) return ‘C’; else return ‘F’; }

  10. Introduction to Java Streams

  11. Needed when using readLine( ) Conditional Statements Example import java.io.*; //needed for input stream classes publicclass ConditionalStatementExample { publicstaticvoid main (String [ ] args) throws java.io.IOException { //convert a possibly negative integer received from the keyboard to //its positive (absolute) value System.out.println(“Enter an integer”); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String inputString = br.readLine( ); int theInteger = Integer.parseInt(inputString); if (theInteger < 0) theInteger = - theInteger; System.out.println(“The absolute value is: ” + theInteger); } } If the integer is already positive, do nothing – else clause is not needed.

  12. Conditional Statements Example import java.io.*; //needed for keyboard input import java.util.*; //needed for String Tokenizer publicclass ConditionalStatementExample2 { publicstaticvoid main (String [ ] args) throws java.io.IOException { //Enter two numbers and print the larger of the two System.out.println(“Enter two integer numbers: ”); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String inputString, s1, s2; inputString = br.readLine( ); StringTokenizer st = new StringTokenizer(inputString); s1 = st.nextToken( ); s2 = st.nextToken( ); int num1, num2; num1 = Integer.parseInt(s1); num2 = Integer.parseInt(s2); if (num1 >= num2) System.out.println(“The larger number is: ” + num1); else System.out.println(“The larger number is: ” + num2); } }

More Related