Presentation is loading. Please wait.

Presentation is loading. Please wait.

SELECTIONS STATEMENTS

Similar presentations


Presentation on theme: "SELECTIONS STATEMENTS"— Presentation transcript:

1 SELECTIONS STATEMENTS
lab3 PROGRAMMING 1 SELECTIONS STATEMENTS

2 BEFORE WE START Your Cell Phone Silent Please Keep Quite
Find Your Computer and Switch On Ins.Ebtesam AL-Etowi

3 content Boolean data types values. comparison operators .
selection statement . conditional operator Formatting Output . Ins.Ebtesam AL-etowi

4 Boolean Data Type Boolean value: true or false. Example:
boolean open = true; How do you compare two values, such as whether is greater than 0, equal to 0, or less than 0? The result of the comparison is a Boolean value. java provides six comparison operators (also known as relational operators), show in Table Ins.Ebtesam AL-etowi

5 comparison AND logic operators
Ins.Ebtesam AL-etowi

6 Selection statements Selection statements use conditions.
Conditions are Boolean expressions . Java has several types of selection statements: one-way if statements. two-way if statements. nested if statements. switch statements. Ins.Ebtesam AL-etowi

7 One-Way if Statements A one-way if statement executes an action if and only if the condition is true. The syntax for a one-way if statement is shown below: if (boolean-expression) { statement(s); } Ins.Ebtesam AL-etowi

8 Two-Way if Statements The actions that a two-way if statement specifies differ based on whether the condition is true or false. The syntax for a two-way if statement is shown below: if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; Ins.Ebtesam AL-etowi

9 Two-Way if Statements(cont..)
Example : Ins.Ebtesam AL-etowi

10 Nested if Statements The inner if statement is said to be nested inside the outer if statement. multiple alternative if statements. Ins.Ebtesam AL-etowi

11 Switch Statements Java provides a switch statement to handle multiple conditions efficiently. Ins.Ebtesam AL-etowi

12 Example :Switch statements
int month = 8; switch (month) { case 1: System.out.print("January"); break; case 2: System.out.print("February"); break; case 3: System.out.print("March"); break; case 4: System.out.print("April"); break; case 5: System.out.print("May"); break; case 6: System.out.print("June"); break; case 7: System.out.print("July"); break; case 8: System.out.print("August"); break; case 9: System.out.print("September"); break; case 10: System.out.print("October"); break; case 11: System.out.print("November"); break; case 12: System.out.print("December"); break; default: } Ins.Ebtesam AL-etowi

13 conditional operator The syntax is shown below:
boolean-expression ? expression1 : expression2; The result of this conditional expression is expression1 if boolean-expression is true; otherwise the result is expression2. For example: if (num % 2 == 0) System.out.println(num + “is even”); else System.out.println(num + “is odd”); System.out.println((num % 2 == 0)? num + “is even” :num + “is odd”); Ins.Ebtesam AL-etowi

14 Formatting Output Use the printf statement.
System.out.printf(format, items); Where format is a string that may consist of substrings and format specifiers. A format specifier specifies how an item should be displayed. An item may be a numeric value, character, boolean value, or a string. Each specifier begins with a percent sign. Ins.Ebtesam AL-etowi

15 Specifier Output Example
%b a boolean value true or false %c a character 'a' %d a decimal integer %f a floating-point number %e a number in standard scientific notation e+01 %s a string "Java is cool" Ins.Ebtesam AL-etowi

16 Exercise1 Write a program that prompts the user to enter an integer and checks whether the number is divisible by both 5 and 6, or neither of them, or just one of them. 1 2 3 4 Ins.Ebtesam AL-etowi

17 OUTPUT Ins.Ebtesam AL-etowi

18 Exercise2 Write a program to make simple calculator , Let the user Enter the numbers then ,choose the Arithmetic operation. 1] Addition 2]Subtraction 3]Multiplication 4]Division 1 2 3 Ins.Ebtesam AL-etowi

19 Exercise2 Add Sub Mul Div Ins.Ebtesam AL-etowi

20 OUTPUT Ins.Ebtesam AL-etowi

21 Thank You !


Download ppt "SELECTIONS STATEMENTS"

Similar presentations


Ads by Google