SELECTIONS STATEMENTS lab3 PROGRAMMING 1 SELECTIONS STATEMENTS
BEFORE WE START Your Cell Phone Silent Please Keep Quite Find Your Computer and Switch On Ins.Ebtesam AL-Etowi
content Boolean data types values. comparison operators . selection statement . conditional operator Formatting Output . Ins.Ebtesam AL-etowi
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
comparison AND logic operators Ins.Ebtesam AL-etowi
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
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
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
Two-Way if Statements(cont..) Example : Ins.Ebtesam AL-etowi
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
Switch Statements Java provides a switch statement to handle multiple conditions efficiently. Ins.Ebtesam AL-etowi
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
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
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
Specifier Output Example %b a boolean value true or false %c a character 'a' %d a decimal integer 200 %f a floating-point number 45.460000 %e a number in standard scientific notation 4.556000e+01 %s a string "Java is cool" Ins.Ebtesam AL-etowi
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
OUTPUT Ins.Ebtesam AL-etowi
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
Exercise2 Add Sub Mul Div Ins.Ebtesam AL-etowi
OUTPUT Ins.Ebtesam AL-etowi
Thank You !