Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional Branching

Similar presentations


Presentation on theme: "Conditional Branching"— Presentation transcript:

1 Conditional Branching

2 We often need to branch our program depending on a particular result
We often need to branch our program depending on a particular result. Today we will take a look at our conditional statements: if, elseif, else First, the logical operations in Java: == equal to != not equal to > greater than < less than >= greater than or equal to <= less than or equal to

3 Consider the following program: double n1 = Math. random()
Consider the following program: double n1 = Math.random()*100; if(n1 > 50) { // execute this code } else // execute some other code The program above will generate a random number between 0 – 100 and execute the code inside the ‘if’ statement if n1 greater than 50. Otherwise, it will execute code inside the ‘else’

4 Structure: if( some condition is true) // the ‘if’ must have a condition to test { // the condition must be a logical operations // code } else // the ‘else’ has no condition to test. It is the default. {

5 Structure: The ‘if’ statements can stand on their own
Structure: The ‘if’ statements can stand on their own. However, the ‘else’ statements must be linked with an ‘if’. You cannot have stand alone ‘else’ statement. Every ‘else’ statement must respect an ‘if’ statement.

6 The following is legal: Scanner key = new Scanner(System
The following is legal: Scanner key = new Scanner(System.in); int n1 = key.nextInt(); if(n1 == 50) { // execute code } There is not else statement here. It is not necessarily needed.

7 The following is not legal: Scanner key = new Scanner(System
The following is not legal: Scanner key = new Scanner(System.in); int n1 = key.nextInt(); else { // execute code } There is no ‘if’ statement for the ‘else’ statement to respect. This will be an error. You cannot have standalone ‘else’ statements.

8 The elseif statement: The “elseif” chain together a series of statements that are all linked. Consider the following: if(some condition) {//do this code} elseif(some condition) else {// do this code} The else statement will respect the entire chain. It will only execute when ALL of the conditions fail. Notice that the ‘elseif’ requires a condition to test. Only the ‘else’ is default and requires no condition.

9 An ‘elseif’ must always have an ‘if’ statement before it
An ‘elseif’ must always have an ‘if’ statement before it. It cannot stand alone. It will respect the ‘if’ statement or other ‘elseif’ statements that are preceding it. Remember, the ‘elseif’ chains things together. If you introduce an ‘if’ between two ‘elseif’ statements, the chain is broken. Only ‘if’ statements can stand alone without anything else. You can only have one ‘else’ statement per chain. It is the default and requires no conditions.

10 Exercise 1 Write a program that will take three user inputs to the sides of a triangle. Use this information to determine if the numbers represent a scalene, isosceles, or equilateral triangle.

11 Exercise 2 Write a program that will generate 3 random integers between Determine which is the biggest and which one is the smallest and out put them.

12 Exercise 3 Ask the user to input 2 strings. Determine which is string is the smaller length. Now generate a random number between 0 to the size of the smaller string. Get the character of both strings at position of that random number and see if they are equal. Output “equal” if they are and “not equal” if they are not.

13 Exercise 4 Generate 4 random integers between Determine if the three numbers generated are a Pythagorean triplet.


Download ppt "Conditional Branching"

Similar presentations


Ads by Google