90 likes | 717 Views
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.
E N D
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 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
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
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
Conditional Statements Example import java.util.Scanner; //needed for input stream classes publicclass ConditionalStatementExample { publicstaticvoid main (String [ ] args) { //receive an integer from the keyboard and print out whether it is //odd or even System.out.println(“Enter an integer”); Scanner console = new Scanner(System.in); int theInteger = console.nextInt( ); if (theInteger %2 == 0) System.out.println(“the integer “ + theInteger + “ is even”); else System.out.println(“the integer “ + theInteger + “ is odd”); } } If the integer is already positive, do nothing – else clause is not needed.
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