🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

RiskFreeVigor

Uploaded by RiskFreeVigor

Tags

java programming conditional statements loop statements

Full Transcript

Object Oriented Programming and Design WEEK 2 – Conditional Statements,Loop Statement,Jump Statement Revision Topic Variables Data Types Variables Declaration Expressions Errors Comments Operators 2 LET’S GET STARTED WITH...

Object Oriented Programming and Design WEEK 2 – Conditional Statements,Loop Statement,Jump Statement Revision Topic Variables Data Types Variables Declaration Expressions Errors Comments Operators 2 LET’S GET STARTED WITH LECTURE 2 3 Learning Outcomes To understand how to implement decision statements using the if-else statement in Java To understand how to implement jump statement in Java To understand how to implement loop statement in Java 4 Java Selection Control 1.Conditional Statements 5 1. Conditional Statements 1.1. Importance of Conditional Statements Conditional Statements 1.2. Types of Conditional Statements 2. Decision Making Statements 2.1. Simple if Statement Java provides statements that can be used to control 2.2. if-else Statement the flow of Java code. 2.3. if-else-if ladder Statement 2.4 Nested-if Statement It is one of the fundamental features of Java, which 3. Switch Statements 3.1. break Keyword provides a smooth flow of program. 3.2. Syntax for switch cases The statements in the code are executed according to 3.3. Switch Statement Example 3.4. default Keyword the order in which they appear. 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 6 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Importance of conditional Statements 1.2. Types of Conditional statements Statements 2. Decision Making Statements 2.1. Simple if Statement It helps to reduce the code size and result in saving the 2.2. if-else Statement time. 2.3. if-else-if ladder Statement 2.4 Nested-if Statement It improves the readability of the code. 3. Switch Statements 3.1. break Keyword It is useful for handling the large projects. 3.2. Syntax for switch cases 3.3. Switch Statement Example 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 7 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Types of Conditional Statements Statements 1. Decision Making statements 1.2. Types of Conditional Statements ○ if statements 2. Decision Making Statements ○ switch statement 2.1. Simple if Statement 2.2. if-else Statement 2. Loop statements 2.3. if-else-if ladder Statement 2.4 Nested-if Statement ○ do while loop 3. Switch Statements 3.1. break Keyword ○ while loop 3.2. Syntax for switch cases ○ for loop 3.3. Switch Statement Example 3.4. default Keyword ○ for-each loop 4. Loop Statements 4.1. Why we need loop? 3. Jump statements 4.2. Iterations ○ break statement 4.3. Loops in java 4.4. 'for' loop in java ○ continue statement 8 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional 1A.Decision Making Statements Statements As the name suggests, decision-making statements 1.2. Types of Conditional Statements decide which statement to execute and when. 2. Decision Making Statements 2.1. Simple if Statement 1. If Statement: In Java, the "if" statement is used to 2.2. if-else Statement evaluate a condition. 2.3. if-else-if ladder Statement 2.4 Nested-if Statement 3. Switch Statements There are four types of if-statements as given below. 3.1. break Keyword 3.2. Syntax for switch cases 1.1 Simple if statement 3.3. Switch Statement Example 3.4. default Keyword 1.2 if-else statement 4. Loop Statements 4.1. Why we need loop? 1.3 if-else-if ladder 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 1.4 Nested if-statement 9 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Statements 1.1 Simple if Statement 1.2. Types of Conditional Simple if is the most basic statement among all control Statements 2. Decision Making Statements flow statements in Java. 2.1. Simple if Statement 2.2. if-else Statement It evaluates a Boolean expression and enables the 2.3. if-else-if ladder Statement program to enter a block of code if the expression 2.4 Nested-if Statement 3. Switch Statements evaluates to true. 3.1. break Keyword 3.2. Syntax for switch cases Syntax: 3.3. Switch Statement Example 3.4. default Keyword 1. if(condition) { 4. Loop Statements 4.1. Why we need loop? 2. statement 1; //executes when condition is true 4.2. Iterations 3. } 4.3. Loops in java 4.4. 'for' loop in java 10 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional 1.1 Simple if Statement Statements 1.2. Types of Conditional Flowchart Statements 2. Decision Making Statements 2.1. Simple if Statement 2.2. if-else Statement 2.3. if-else-if ladder Statement 2.4 Nested-if Statement 3. Switch Statements 3.1. break Keyword 3.2. Syntax for switch cases 3.3. Switch Statement Example 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 4.4.1. Simple 'for' loop Fig: Flowchart of simple if statement 11 1. Conditional Statements 1.1. Importance of Conditional 1.1 Simple if Statement Statements 1.2. Types of Conditional Example Statements 2. Decision Making Statements 2.1. Simple if Statement 1. public class IfExample { 2.2. if-else Statement 2.3. if-else-if ladder Statement 2. public static void main(String[] args) { 2.4 Nested-if Statement 3. //defining an 'age' variable 3. Switch Statements 3.1. break Keyword 4. int age=25; 3.2. Syntax for switch cases 5. //checking the age 3.3. Switch Statement Example 3.4. default Keyword 6. if(age>18){ 4. Loop Statements 7. System.out.print("Age is greater than 18"); 4.1. Why we need loop? 8. } OutPut: 4.2. Iterations 4.3. Loops in java 9. } Age is greater than 18 4.4. 'for' loop in java 10. } 12 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Statements 1.2 if-else Statement 1.2. Types of Conditional The if-else statement is an extension to the if-statement, Statements 2. Decision Making Statements which uses another block of code, i.e., else block. 2.1. Simple if Statement 2.2. if-else Statement The else block is executed if the condition of the 2.3. if-else-if ladder Statement 2.4 Nested-if Statement if-block is evaluated as false. 3. Switch Statements 3.1. break Keyword 3.2. Syntax for switch cases 3.3. Switch Statement Example 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 13 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional 1.2 if-else Statement Statements 1.2. Types of Conditional Statements 2. Decision Making Statements Syntax: 2.1. Simple if Statement 2.2. if-else Statement 2.3. if-else-if ladder Statement 1. if(condition) { 2.4 Nested-if Statement 2. statement 1; //executes when condition is true 3. Switch Statements 3.1. break Keyword 3. } 3.2. Syntax for switch cases 3.3. Switch Statement Example 4. else{ 3.4. default Keyword 4. Loop Statements 5. statement 2; //executes when condition is 4.1. Why we need loop? 4.2. Iterations false 4.3. Loops in java 6. } 4.4. 'for' loop in java 14 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional 1.2 if-else Statement Statements 1.2. Types of Conditional Flowchart Statements 2. Decision Making Statements 2.1. Simple if Statement 2.2. if-else Statement 2.3. if-else-if ladder Statement 2.4 Nested-if Statement 3. Switch Statements 3.1. break Keyword 3.2. Syntax for switch cases 3.3. Switch Statement Example 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 4.4.1. Simple 'for' loop Fig: Flowchart of if-else statement 15 1. Conditional Statements 1.1. Importance of Conditional 1.2 if-else Statement Statements 1.2. Types of Conditional Flowchart Statements 1. public class IfElseExample { 2. Decision Making Statements 2. public static void main(String[] args) { 2.1. Simple if Statement 2.2. if-else Statement 3. //defining a variable 2.3. if-else-if ladder Statement 4. int number=13; 2.4 Nested-if Statement 3. Switch Statements 5. //Check if the number is divisible by 2 or not 3.1. break Keyword 6. if(number%2==0){ 3.2. Syntax for switch cases 3.3. Switch Statement Example 7. System.out.println("even number"); 3.4. default Keyword 8. }else{ 4. Loop Statements 9. System.out.println("odd number"); 4.1. Why we need loop? 4.2. Iterations 10. } OutPut: 4.3. Loops in java 4.4. 'for' loop in java 11. } odd number 16 4.4.1. Simple 'for' loop 12. } 1. Conditional Statements 1.1. Importance of Conditional Statements 1.3 if-else-if ladder Statement 1.2. Types of Conditional Statements The if-else-if ladder statement contains the if-statement 2. Decision Making Statements 2.1. Simple if Statement followed by multiple else-if statements. 2.2. if-else Statement In other words, we can say that it is the chain of if-else 2.3. if-else-if ladder Statement 2.4 Nested-if Statement statements that create a decision tree where the 3. Switch Statements 3.1. break Keyword program may enter in the block of code where the 3.2. Syntax for switch cases condition is true. 3.3. Switch Statement Example 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 17 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Statements 1.3 if-else-if ladder Statement 1.2. Types of Conditional Syntax: Statements 2. Decision Making Statements 1. if(condition 1) { 2.1. Simple if Statement 2.2. if-else Statement 2. statement 1; //executes when condition 1 is true 2.3. if-else-if ladder Statement 2.4 Nested-if Statement 3. } 3. Switch Statements 4. else if(condition 2) { 3.1. break Keyword 3.2. Syntax for switch cases 5. statement 2; //executes when condition 2 is true 3.3. Switch Statement Example 3.4. default Keyword 6. } 4. Loop Statements 4.1. Why we need loop? 7. else { 4.2. Iterations 8. statement 2; //executes when all the conditions are false 4.3. Loops in java 4.4. 'for' loop in java 9. } 18 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional 1.3 if-else-if ladder Statement Statements 1.2. Types of Conditional Flowchart Statements 2. Decision Making Statements 2.1. Simple if Statement 2.2. if-else Statement 2.3. if-else-if ladder Statement 2.4 Nested-if Statement 3. Switch Statements 3.1. break Keyword 3.2. Syntax for switch cases 3.3. Switch Statement Example 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 4.4.1. Simple 'for' loop Fig: Flowchart of if-else-if ladder statement 19 1. Conditional Statements 1.1. Importance of Conditional 1.3 if-else-if ladder Statement Statements 1.2. Types of Conditional Example Statements 1. public class PositiveNegativeExample { 2. Decision Making Statements 2. public static void main(String[] args) { 2.1. Simple if Statement 2.2. if-else Statement 3. int number=-13; 2.3. if-else-if ladder Statement 4. if(number>0){ 2.4 Nested-if Statement 3. Switch Statements 5. System.out.println("POSITIVE"); 3.1. break Keyword 6. }else if(number=18){ 3. Switch Statements 6. if(weight>50){ 3.1. break Keyword 7. System.out.println("You are eligible to donate blood"); 3.2. Syntax for switch cases 3.3. Switch Statement Example 8. } else{ 3.4. default Keyword 9. System.out.println("You are not eligible to donate blood"); 4. Loop Statements 10. } 4.1. Why we need loop? 11. } else{ 4.2. Iterations 4.3. Loops in java 12. System.out.println("Age must be greater than 18"); 4.4. 'for' loop in java 13. } OutPut: 24 4.4.1. Simple 'for' loop 14. } } You are eligible to donate blood 1. Conditional Statements 1.1. Importance of Conditional Statements 1B. Switch Statements 1.2. Types of Conditional Statements The Switch statements contains multiple blocks of code 2. Decision Making Statements 2.1. Simple if Statement called cases and a single case is executed based on 2.2. if-else Statement 2.3. if-else-if ladder Statement the variable which is being switched. 2.4 Nested-if Statement 3. Switch Statements In Java, Switch statements are similar to if-else-if 3.1. break Keyword statements. 3.2. Syntax for switch cases 3.3. Switch Statement Example 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 25 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional 1B. Switch Statements Statements 1.2. Types of Conditional Points to be remembered: Statements ❖ Cases cannot be duplicate 2. Decision Making Statements 2.1. Simple if Statement ❖ The case variables can be int, short, byte, char, or 2.2. if-else Statement 2.3. if-else-if ladder Statement enumeration. 2.4 Nested-if Statement 3. Switch Statements ❖ Default statement is executed when any of the case 3.1. break Keyword doesn't match the value of expression. It is optional. 3.2. Syntax for switch cases 3.3. Switch Statement Example ❖ Break statement terminates the switch block when the 3.4. default Keyword 4. Loop Statements condition is satisfied. 4.1. Why we need loop? It is optional, if not used, next case is executed. 4.2. Iterations 4.3. Loops in java ❖ The case expression will be of the same type as the 4.4. 'for' loop in java 4.4.1. Simple 'for' loop variable. However, it will also be a constant value. 26 1. Conditional Statements 1.1. Importance of Conditional Statements 1B.The break Keyword 1.2. Types of Conditional Statements 2. Decision Making Statements When Java reaches a break keyword, it breaks out of 2.1. Simple if Statement the switch block. 2.2. if-else Statement 2.3. if-else-if ladder Statement This will stop the execution of more code and case 2.4 Nested-if Statement 3. Switch Statements testing inside the block. 3.1. break Keyword When a match is found, and the job is done, it's time for 3.2. Syntax for switch cases 3.3. Switch Statement Example a break. There is no need for more testing. 3.4. default Keyword 4. Loop Statements Note:A break can save a lot of execution time because it 4.1. Why we need loop? 4.2. Iterations "ignores" the execution of all the rest of the code in the 4.3. Loops in java switch block. 4.4. 'for' loop in java 27 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Statements 1.2. Types of Conditional Syntax: Statements 2. Decision Making Statements switch(expression) { 2.1. Simple if Statement 2.2. if-else Statement case x: // code block 2.3. if-else-if ladder Statement break ; 2.4 Nested-if Statement case y: // code block 3. Switch Statements break; 3.1. break Keyword default: // code block 3.2. Syntax for switch cases 3.3. Switch Statement Example } 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 28 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional 1B.Switch Statement Example Statements public class Main { 1.2. Types of Conditional public static void main(String[] args) { int day = 4; Statements switch (day) { 2. Decision Making Statements case 1: System.out.println("Monday"); 2.1. Simple if Statement break; 2.2. if-else Statement case 2: System.out.println("Tuesday"); 2.3. if-else-if ladder Statement break; 2.4 Nested-if Statement case 3: Output System.out.println("Wednesday"); 3. Switch Statements break; Thursday 3.1. break Keyword case 4: System.out.println("Thursday"); 3.2. Syntax for switch cases break; 3.3. Switch Statement Example case 5: System.out.println("Friday"); 3.4. default Keyword break; 4. Loop Statements case 6: System.out.println("Saturday"); 4.1. Why we need loop? break; 4.2. Iterations case 7: System.out.println("Sunday"); 4.3. Loops in java break; 4.4. 'for' loop in java } } 29 4.4.1. Simple 'for' loop } 1. Conditional Statements 1.1. Importance of Conditional Statements 1B.The default Keyword 1.2. Types of Conditional Statements The default keyword specifies some code to run if there 2. Decision Making Statements 2.1. Simple if Statement is no case match: 2.2. if-else Statement int day = 4; 2.3. if-else-if ladder Statement switch (day) { 2.4 Nested-if Statement case 6: Output 3. Switch Statements System.out.println("Today is Saturday"); break; "Looking forward to 3.1. break Keyword case 7: the Weekend" 3.2. Syntax for switch cases System.out.println("Today is Sunday"); 3.3. Switch Statement Example break; 3.4. default Keyword default: 4. Loop Statements System.out.println("Looking forward to the 4.1. Why we need loop? Weekend"); 4.2. Iterations } 4.3. Loops in java Note:Note that if the default statement is used as the last 4.4. 'for' loop in java 4.4.1. Simple 'for' loop statement in a switch block, it does not need a break. 30 Java Selection Control 2.Loop Statements 31 1. Conditional Statements 1.1. Importance of Conditional Statements 1.2. Types of Conditional Multiple Messages Statements 2. Decision Making Statements 2.1. Simple if Statement public class Main{ 2.2. if-else Statement public static void main(String[] args) { 2.3. if-else-if ladder Statement 2.4 Nested-if Statement System.out.println("Hello Class!"); 3. Switch Statements System.out.println("I am a teacher."); 3.1. break Keyword System.out.println("How are you doing?"); 3.2. Syntax for switch cases } 3.3. Switch Statement Example } 3.4. default Keyword Output: 4. Loop Statements Hello Class! 4.1. Why we need loop? I am a teacher. 4.2. Iterations How are you doing? 4.3. Loops in java 4.4. 'for' loop in java 32 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Statements 1.2. Types of Conditional Statements Correct Way? 2. Decision Making Statements What to do if we want to print it from 30 times? 2.1. Simple if Statement 2.2. if-else Statement 2.3. if-else-if ladder Statement public class Main{ 2.4 Nested-if Statement public static void main(String[] args) { 3. Switch Statements System.out.println("Hello World"); 3.1. break Keyword System.out.println("Hello World"); 3.2. Syntax for switch cases 3.3. Switch Statement Example System.out.println("Hello World"); 3.4. default Keyword System.out.println("Hello World"); 4. Loop Statements System.out.println("Hello World"); 4.1. Why we need loop? 4.2. Iterations System.out.println("Hello World"); 4.3. Loops in java System.out.println("Hello World"); 4.4. 'for' loop in java } 4.4.1. Simple 'for' loop } Correct Way? 33 1. Conditional Statements 1.1. Importance of Conditional Statements 1.2. Types of Conditional Statements Nothing To Worry!! 2. Decision Making Statements 2.1. Simple if Statement Don’t worry to code long codes 2.2. if-else Statement Because 2.3. if-else-if ladder Statement 2.4 Nested-if Statement Iteration (or looping) is there for you to reduce code line 3. Switch Statements 3.1. break Keyword 3.2. Syntax for switch cases 3.3. Switch Statement Example 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 34 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Statements Iterations 1.2. Types of Conditional 1. Iterative Control Statement: Statements 2. Decision Making Statements ○ A control statement that enables the repeated execution of a set of 2.1. Simple if Statement instructions. 2.2. if-else Statement 2. Iterative Control Structure: 2.3. if-else-if ladder Statement ○ A set of instructions along with the iterative control statement(s) that 2.4 Nested-if Statement govern their execution. 3. Switch Statements 3.1. break Keyword ○ Often referred to as "loops" due to their repetitive nature. 3.2. Syntax for switch cases 3. Types of Loops: 3.3. Switch Statement Example ○ There are various types of loops in programming, including for, while, 3.4. default Keyword and do-while loops. 4. Loop Statements 4. Execution Time: 4.1. Why we need loop? 4.2. Iterations ○ Loops are crucial in programming for executing tasks repeatedly. 4.3. Loops in java ○ Understanding loop execution time is essential for optimizing code 4.4. 'for' loop in java performance. 35 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Statements 1.2. Types of Conditional Loops in java Statements 2. Decision Making Statements The Java for loop is used to iterate a part of the program 2.1. Simple if Statement several times. If the number of iteration is fixed, it is 2.2. if-else Statement recommended to use for loop. 2.3. if-else-if ladder Statement 2.4 Nested-if Statement There are three types of for loops in Java. 3. Switch Statements 3.1. break Keyword 3.2. Syntax for switch cases 3.3. Switch Statement Example 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 36 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Statements 1.2. Types of Conditional Loops in java Statements 2. Decision Making Statements 2.1. Simple if Statement 2.2. if-else Statement 2.3. if-else-if ladder Statement 2.4 Nested-if Statement 3. Switch Statements 3.1. break Keyword 3.2. Syntax for switch cases 3.3. Switch Statement Example 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 37 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Statements 1.2. Types of Conditional for loop in java Statements 2. Decision Making Statements Types of for loop in java 2.1. Simple if Statement Simple for loop 2.2. if-else Statement For-each or Enhanced for loop 2.3. if-else-if ladder Statement 2.4 Nested-if Statement Simple for loop:We can initialize the variable, check 3. Switch Statements condition and increment/decrement value. It consists of four 3.1. break Keyword parts: 3.2. Syntax for switch cases 1. Initialization: It is the initial condition which is executed once 3.3. Switch Statement Example when the loop starts. 3.4. default Keyword 2. Condition: It is the second condition which is executed each 4. Loop Statements time to test the condition of the loop. 4.1. Why we need loop? 3. Increment/Decrement: It increments or decrements the 4.2. Iterations variable value. 4.3. Loops in java 4. Statement: The statement of the loop is executed each time 4.4. 'for' loop in java until the second condition is false. 38 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Statements 1.2. Types of Conditional Statements Simple for Loop 2. Decision Making Statements 2.1. Simple if Statement 2.2. if-else Statement 2.3. if-else-if ladder Statement 2.4 Nested-if Statement 3. Switch Statements 3.1. break Keyword 3.2. Syntax for switch cases 3.3. Switch Statement Example 3.4. default Keyword 4. Loop Statements 4.1. Why we need loop? 4.2. Iterations 4.3. Loops in java 4.4. 'for' loop in java 39 4.4.1. Simple 'for' loop 1. Conditional Statements 1.1. Importance of Conditional Statements 1.2. Types of Conditional Statements Simple for Loop example 2. Decision Making Statements 2.1. Simple if Statement 1. public class ForExample { 2.2. if-else Statement 2.3. if-else-if ladder Statement 2. public static void main(String[] args) 2.4 Nested-if Statement { 3. Switch Statements 3.1. break Keyword 3. //Code of Java for loop 3.2. Syntax for switch cases 3.3. Switch Statement Example 4. for(int i=1;i

Use Quizgecko on...
Browser
Browser