1 / 18

Control Flow Statements

Control Flow Statements.

calla
Download Presentation

Control Flow 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. Control Flow Statements • The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.

  2. Selection Statements • Selection statements are control statements that allow alternative actions based upon conditions that are evaluated at run time. • Such statements include if and switch statements.

  3. THE if STATEMENT The if statement allows for conditional execution. The statement that is included within it will be executed only if its condition is true. The syntax for the if statement is: if (condition) statement; where condition is a boolean expression and statement is the statement that will execute if the value of the boolean expression is true.

  4. Example 1: class IfStatements {      public static void main(String[] args) { int user = 17; if (user < 18) { System.out.println(“User is less than 18”);      } } }

  5. A variable by the name user was created and assigned a value of 17. The if statement evaluates the condition (user < 18). If it is true, the string “user is less than 18” is printed.

  6. Exercise 1Replace your "less than" symbol with the "less than or equal to" symbols. Change your message to suit, something like "user is less than or equal to 18". Run your programme again. Do you see the message? ExerciseChange the user value to 20. Run your programme again. Do you still see the message?

  7. Java Operators • Simple Assignment Operator = Simple assignment operator • Arithmetic Operators + Additive operator (also used for String concatenation) - Subtraction operator • Multiplication operator / Division operator • % Remainder operator

  8. Equality and Relational Operators == Equal to != Not equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to

  9. Conditional Operators && Conditional-AND || Conditional-OR ?: Ternary (shorthand for if-then-else statement)

  10. You can have more than one IF Statement in your code.      public static void main(String[ ] args) { int user = 18; if (user < = 18) { System.out.println(“User is 18 or younger”);      } if (user > 18) {System.out.println (“User is older than 18”); } }

  11. This time, we have two IF Statements. The first tests for values less than or equal to 18. The second tests for values greater than 18. Exercise Change the value of user to 20 and run the code again.

  12. THE if . . . else STATEMENT The if . . . else statement is the if statement with an added else clause. It works the same way as the if statement except that, when the condition is false, the statement within the else clause executes.

  13. Its syntax is as follows: if ( condition ) statement1; else ( condition ) statement2; Either statement 1 or statement 2 executes (but not both), depending on whether the condition is true or false.

  14. Example:      public static void main(String[ ] args) { int user = 17; if (user < = 18) { System.out.println(“User is 18 or younger”);      } else {System.out.println (“User is older than 18”); } } Exercise: Change the value of user to 30 and run the codes again.

  15. THE if . . . else if STATEMENT The if . . .else. . . statement allows for conditional execution based upon two alternatives. If you have more than two possible alternatives, you can link together a sequence of if . . .else. . . Statements.

  16. if ( condition_one )statement1; { }else if ( condition_two ) statement2; { }else statement3; { }

  17.      public static void main(String[ ] args) { int user = 21; if (user < = 18) { System.out.println(“User is 18 or younger”);      } elseif (user > 18 && user < 40) { System.out.prinntln (“user is between 19 and 39”); } else {System.out.println (“User is older than 40”); } } Exercise: • Change the value of user to 45 • Try to use other conditional operators in your code.

  18. NESTED if Statements Nesting an IF Statement just means putting one IF Statement inside of another. In your spare time, look up Nested if Statements.

More Related