FOR LOOP: ======== Loop Entry Control Statement break statement: ================ the break without the condition: ================================ class BreakStatement{ public static void main(String[] args) { for(int i = 1;i <= 10;++i) { System.out.print(i+"\t"); break;// will give immediate termination // System.out.print(i+"\t"); } System.out.println("Hi"); } } if the statement immediate to the break: ======================================== class BreakStatement{ public static void main(String[] args) { for(int i = 1;i <= 10;++i) { if(i == 4) { break; System.out.println("Hi"); } System.out.print(i+"\t"); } System.out.println("Hi"); } } output:(Run time error) ======= ERROR! /tmp/UttK7ar1Jv/BreakStatement.java:9: error: unreachable statement System.out.println("Hi"); ^ 1 error ==================================================== 97 ==> 2,3,4,5,6,7,.....48 ==> Prime number 99 ==> 2,3,4,5,....49 ==> break gaming applications: dice roll games ludo king dice == 6: coin ==> released dice == 6:(at the beginning) break banking application: transfer if funds ==> amount: break =========================================== What happened if the break in conditional statements without loops: ==================================================================== class BreakStatement{ public static void main(String[] args) { int n = 22; if(n%2 == 0) { break; } } } Output: ====== ERROR! /tmp/malDpkpfrC/BreakStatement.java:7: error: break outside switch or loop break; ^ 1 error === Code Exited With Errors === ======================================================================= continue Statement: =================== banking application: transaction: sending of amount loop for 10times: if(amount > funds) { break; } else if(min_fund >= 5000) { continue; } else if(server is running slow) { skip; } ==> continue is the keyword we can always use in loop statements only. ==> will stop the current iteration and continue with remaining Syntax: continue; class ContinueStatement{ public static void main(String[] args) { int i = 1; while(i <= 10) { if(i == 5) { ++i; continue; } System.out.println(i); ++i; } System.out.println("Hi"); } } ========================================== if any statement immediate to the "continue": ============================================= Run time error: class ContinueStatement{ public static void main(String[] args) { int i = 1; while(i <= 10) { if(i == 5) { // ++i; continue; ++i; } System.out.println(i); ++i; } System.out.println("Hi"); } } Output: ====== ERROR! /tmp/CkNNVmO1K9/ContinueStatement.java:11: error: unreachable statement ++i; ^ 1 error === Code Exited With Errors === =========================================== if the "continue" inside the conditional statements (without loops): =================================================================== class ContinueStatement{ public static void main(String[] args) { int n = 22; if(n % 2 == 0) { continue; } } } Output: ======= ERROR! /tmp/LQ7CQtG3Kq/ContinueStatement.java:7: error: continue outside of loop continue; ^ 1 error === Code Exited With Errors === ============================================= Break Vs Continue: =================== Break: ===== 1) will give the immediate termination from the loops. 2) if the break inside the loops without conditions: break will stop the all iterations after first iteration only. continue: ========= 1) will stop the current iteration and continue with remaining iterations. 2) if the break inside the loops without conditions: continue will make the loop as normal. ===================================================== Switch Statement: ================= ==> switch ==> keyword ==> Selection statement class Calculator{ public static void main(String[] args) { int n1,n2,result = 0; char ch; n1 = 12; n2 = 13; ch = '*'; if(ch == '+') { result = n1+n2; } else if(ch == '-') { result = n1-n2; } else if(ch == '*') { result = n1*n2; } else if(ch == '/') { result = n1/n2; } else if(ch == '%') { result = n1%n2; } else { System.out.println("Invalid Operator Symbol."); } System.out.println("The Result of the given operation is = "+result); } } ========================================= Syntax for switch statement: ============================ switch(expression(integer/character/string)) { case label1: statement-1; statement-2; break; case label2: statement-1; statement-2; break; case label3: statement-1; statement-2; break; default: statement-1; statement-2; } // WAP FOR SIMPLE CALCUALTOR import java.util.Scanner; class SimpleCalculator{ public static void main(String[] args) { Scanner obj = new Scanner(System.in); int n1,n2,result = 0; char opr = ' '; System.out.println("Enter two values:"); n1 = obj.nextInt(); n2 = obj.nextInt(); switch(opr) { case '+': result = n1 + n2; break; case '-': result = n1-n2; break; case '*': result = n1*n2; break; case '/': result = n1/n2; break; case '%': result = n1%n2; break; default: System.out.println("Invalid operator choice"); } System.out.println("The Given Operation Result = "+result); } } ================================================ // WAP TO PRINT THE DAY NAME BY ACCEPTING THE DAY NUMBER. /* DAY NUM = 0 ==> SUNDAY DAY NUM = 1 ==> MONDAY*/ import java.util.Scanner; class DayNamePrinting{ public static void main(String[] args) { Scanner obj = new Scanner(System.in); System.out.println("Enter the day number (0 to 6:)"); int dayNum = obj.nextInt(); String dayName = "";// empty string System.out.println("The Name of the day = "); switch(dayNum) { case 0: dayName = "Sunday"; System.out.println(dayName); break; case 1: dayName = "Monday"; System.out.println(dayName); break; case 2: dayName = "Tuesday"; System.out.println(dayName); break; case 3: dayName = "Wednesday"; System.out.println(dayName); break; case 4: dayName = "Thirsday"; System.out.println(dayName); break; case 5: dayName = "Friday"; System.out.println(dayName); break; case 6: dayName = "Saturday"; System.out.println(dayName); break; default: System.out.println("Invalid Day Number."); } } } ============================================================ Assignment: =========== 1) WAP TO ACCEPT STUDENT MARKS OF 5 SUBJECTS. CALCULATE THE STUDENT PERCENTAGE AND PRINT THE GRADE OF THE STUDENT BASED ON PERCENTAGE. PERCENTAGE >= 85 ==> 'A1'; PERCENTAGE >= 80 BUT BELOW 85 ==> 'A2'; PERCENTAGE >= 75 BUT BELOW 80 ==> 'A'; PERCENTAGE >= 70 BUT BELOW 75 ==> 'B1'; PERCENTAGE >= 65 BUT BELOW 70 ==> 'B2'; PERCNETAGE >= 60 BUT BELOW 65 ==> 'C'; PERCENTAGE >= 50 BUT BELOW 60 ==> 'D'; PERCENTAGE >= 40 BUT BELOW 50 ==> 'E'; OTHERWISE ==> FAIL 2) WAP TO ACCEPT THE MONTH NAME AND PRINT ITS NUMBER OF DAYS. HINT: JANUARY ==> 31-DAYS FEBRUARY ==> 28 OR 29-DAYS MARCH ==> 31 DAYS. ETC.