Conditional Statements: ======================= 1) Simple If Statement 2) if else statement 3) nested if else ================= Syntax: if(condition1) { block of statements if(condition2) { block of statements } else { block of statements; } } else { if(condition3) { block of statements; } else { block of statements; } } // WAP TO FIND THE BIGGEST NUMBER AMONG THRE INTEGERS USING NESTED IF ELSE CONCEPT. import java.util.Scanner; class nestedIfElse{ public static void main(String x[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter three integers:"); int a = sc.nextInt();//91 int b = sc.nextInt(); // 101 int c = sc.nextInt();// 132 int big; // logic for finding the biggets among three if(a > b) // 111 > 101 { if(a > c) // 111 > 103 111 > 132 { big = a; //111 } else { big = c;//132 } } else { if(b > c) { big = b; } else { big = c; } } System.out.println("The Biggest number = "+big); } } if(a>b) if(a>c) if(a>d) if(a>e) if(a>f) else else else else else if(b>c) if(b>d) if(b>e) if(b>f) ============================== 4) if else if else ladder: ========================== Syntax: if(condition1) { block-1; } else if(condition2) { block-2; } else if(condition3) { block-3; } else { block-4; } // WAP to find the biggest number among 6-integers import java.util.Scanner; class ifElseIFElseLadder{ public static void main(String x[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter six values:"); int a = sc.nextInt(); // 19 int b = sc.nextInt(); // 11 21 int c = sc.nextInt(); // 13 31 int d = sc.nextInt(); // 15 51 int e = sc.nextInt(); // 12 int f = sc.nextInt(); // 14 int big; if(a > b && a > c && a > d && a > e && a > f) { big = a; } else if(b > c && b > d && b > e && b > f) { big = b; } else if(c > d && c > e && c > f) { big = c; } else if(d > e && d > f) { big = d; } else if(e > f) { big = e; } else { big = f; } System.out.println("The Biggest number = "+big); } } ============================= WAP for Student Grade System. ============================= four subject marks percentage percentage >= 85 ==> 'A1' 75 and 85 ==> 'A2' 70 and 75 ==> B 60 and 70 ==> C 50 and 60 ==> D 40 and 50 ==> E <40==> Fail ===================================== // WAP TO FIND THE TYPE OF TRIAGLE. /* all sides are equal ==> Equilateral any two sides are equal ==> Isosceles no side is equal ==> Scalene */ import java.util.Scanner; class ifElseIfElseLadder{ public static void main(String x[]) { Scanner obj = new Scanner(System.in); System.out.println("Enter the values for triangle:"); int a = obj.nextInt(); int b = obj.nextInt(); int c = obj.nextInt(); if(a == b && a == c && b == c) { System.out.println("It is an Equilateral Triangle."); } else if(a == b || a == c || b == c) { System.out.println("It is an Isosceles Triangle."); } else { System.out.println("It is a Scalene Triangle."); } } } ======================================================= Loop Statements: ================ ==> also called as "Iterative statements" ==> to make execute the selected (based on condition) block of code repeatedly, loops can be used. ==> Three types: 1) while loop 2) do-while loop 3) for loop Note: ===== we need three things: 1) Initialization 2) condition 3) Update 1) while loop ============= Syntax: ====== initialization for while loop while(condition) { statements;//while loop body update } // WAP TO PRINT NUMBERS FROM 1 TO 10 USING WHILE LOOP. /* int i = 1; initialization SOP(i); //1 i += 1; i = 2 SOP(i); i += 1 3 sop(i) i += 1 4 sop(i) */ class WhileLoop{ public static void main(String x[]) { int i = 1;// i is iterative variable/loop variable while(i < 11) { System.out.println(i); // 1 i++; // i = i + 1 i += 1 } System.out.println("Bye Bye."); } } ========================================== // WAP TO PRINT ALL NUMBERS FROM 100 TO 1 WITH THE DIFFERENCE OF 7. // 100 93 86 79 72.... class WhileLoop{ public static void main(String x[]) { int i = 100; // initialization while(i>=1) { System.out.print(i+"\t"); i -= 7; } System.out.println(); System.out.println("My program is terminated."); } } ============================= Nesting of conditions in loops: ============================== Syntax: initialization while(condition) { if(condition) { block } update; } // WAP TO FIND THE SUM OF ALL ODD NUMBERS FROM 100 TO 1. class WhileLoop{ public static void main(String[] x) { int i = 100; int s_odd = 0; while(i > 0) { if(i % 2 != 0) { s_odd = s_odd + i; } --i; } System.out.println("The Sum of odd numbers = "+s_odd); } } ======================================= // WAP TO COUNT THE NUMBER OF DIGITS IN A GIVEN NUMBER. import java.util.Scanner; class countDigits{ public static void main(String[] x) { Scanner obj = new Scanner(System.in); System.out.println("Enter a value:"); int num = obj.nextInt(); int n = num; // initialization for while loop int count = 0; while(n > 0) { n = n / 10; // n/=10 count++; } System.out.println("The Total Number of Digits = "+count); } }