if (condition1) {// Code to be executed if condition1 is true} elseif (condition2) {// Code to be executed if condition2 is true} elseif (condition3) {// Code to be executed if condition3 is true} else {// Code to be executed if all conditions are false}
for (initialization; condition; increment/decrement){// Body}
Example 1:
publicclassForLoop {publicstaticvoidmain(String[] args) {for (int num =1; num <=5; num +=1){System.out.println(num); } }}
Output: 1, 2, 3, 4, 5
Example 2:
importjava.util.Scanner;publicclassForLoop {publicstaticvoidmain(String[] args) {Scanner in =newScanner(System.in);int n =in.nextInt();for (int num =1; num <= n; num +=1){System.out.print(num +" "); } }}
Input: 6
Output: 1 2 3 4 5 6
While Loop:
Used when the number of iterations is not known.
Syntax:
while (condition){// Code to be executed// Increment/Decrement}
Example:
publicclassWhileLoop {publicstaticvoidmain(String[] args) {int num =1;while (num <=5){System.out.println(num); num +=1; } }}
Output: 1, 2, 3, 4, 5
Do-While Loop:
Used when the loop needs to execute at least once.
Exit-controlled loop.
Syntax:
do {// Code to be executed// Update statement (increment/decrement)} while (condition);
Example:
publicclassDoWhileLoop {publicstaticvoidmain(String[] args) {int n =1;do {System.out.println(n); n++; } while (n <=5); }}
Output: 1, 2, 3, 4, 5
Comparison between While Loop and Do-While Loop:
Programs
🎯 Program: Largest of Three Numbers
Problem Statement: "Find largest among three numbers."
Approach 1:
importjava.util.Scanner;publicclassLargestOfThree {publicstaticvoidmain(String[] args) {Scanner in =newScanner(System.in);int a =in.nextInt();int b =in.nextInt();int c =in.nextInt();int max = a;if (b > max) { max = b; }if (c > max) { max = c; }System.out.println(max); }}
Approach 2:
importjava.util.Scanner;publicclassLargestOfThree {publicstaticvoidmain(String[] args) {Scanner in =newScanner(System.in);int a =in.nextInt();int b =in.nextInt();int c =in.nextInt();int max =0;if (a > b) { max = a; } else { max = b; }if (c > max) { max = c; }System.out.println(max); }}
Approach 3 (Using Math.max):
importjava.util.Scanner;publicclassLargestOfThree {publicstaticvoidmain(String[] args) {Scanner in =newScanner(System.in);int a =in.nextInt();int b =in.nextInt();int c =in.nextInt();int max =Math.max(c,Math.max(a, b));System.out.println(max); }}
Input: 3 6 5
Output: 6
🎯 Program: Alphabet Case Check
Problem Statement: "Take an input character from the keyboard and check whether it is an uppercase alphabet or lowercase alphabet."
Problem Statement: "Find the nth Fibonacci number."
Example:
importjava.util.Scanner;publicclassFibonacciNumbers {publicstaticvoidmain(String[] args) {Scanner in =newScanner(System.in);int n =in.nextInt();int a =0, b =1, count =2;while (count <= n) {int temp = b; b = b + a; a = temp; count++; }System.out.println(b); }}
Input: 7
Output: 13
🎯 Program: Counting Occurrence
Problem Statement: "Input two numbers, find how many times the second number's digit is present in the first number."
Problem Statement: "Input a number from the keyboard and show the output as the reverse of that number."
Example:
importjava.util.Scanner;publicclassReverseANumber {publicstaticvoidmain(String[] args) {Scanner in =newScanner(System.in);int num =in.nextInt();int reversedNum =0;while (num >0) {int rem = num %10; num /=10; reversedNum = reversedNum *10+ rem; }System.out.println(reversedNum); }}
Input: 458792
Output: 297854
🎯 Program: Calculator Program
Example of a simple calculator program.
Input and output are managed until the user presses 'X' or 'x'.
importjava.util.Scanner;publicclassCalculator {publicstaticvoidmain(String[] args) {Scanner in =newScanner(System.in);int ans =0;while (true) {System.out.print("Enter the operator: ");char op =in.next().trim().charAt(0);if (op =='+'|| op =='-'|| op =='*'|| op =='/'|| op =='%') {System.out.print("Enter two numbers: ");int num1 =in.nextInt();int num2 =in.nextInt();switch (op) {case'+': ans = num1 + num2;break;case'-': ans = num1 - num2;break;case'*': ans = num1 * num2;break;case'/':if (num2 !=0) { ans = num1 / num2; }break;case'%': ans = num1 % num2;break; } } elseif (op =='x'|| op =='X') {break; } else {System.out.println("Invalid operation!!"); }System.out.println(ans); } }}
Conclusion
To wrap up, diving into Conditional Statements and Loops equips you with essential tools for effective programming. The covered topics, from basic If-Else statements to versatile loops, offer practical solutions for various scenarios. The hands-on examples, like finding the largest number or building a basic calculator, reinforce your skills and prepare you for real-world coding challenges. In a nutshell, this learning journey sets a strong foundation for making informed decisions and handling repetitive tasks in Java programming.