CONDITIONAL
STATEMENTS
IN JAVA
BY,
MRS.V.SANGEETHA.,
IF STATEMENT
if (condition) {
// block of code
}
Example:-
IF-ELSE STATEMENT
if (condition) {
// true block
} else {
// false block
}
IF-ELSE IF LADDER
if (condition1) {
// block1
} else if (condition2) {
// block2
} else {
// default block
}
SWITCH STATEMENT
switch (expression) {
case value1:
// block
break;
default:
// default block
}
CHECK EVEN OR ODD USING IF-
ELSE
import java.util.Scanner;
public class EvenOddScanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = sc.nextInt();
if (number % 2 == 0)
System.out.println(number + " is Even");
else
System.out.println(number + " is Odd");
}
}
PROGRAM WITH BUFFEREDREADER
– GRADING WITH IF-ELSE IF
import java.io.*;
public class GradeChecker {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your marks: ");
int marks = Integer.parseInt(br.readLine());
if (marks >= 90)
System.out.println("Grade A");
else if (marks >= 75)
System.out.println("Grade B");
else if (marks >= 60)
System.out.println("Grade C");
else
System.out.println("Fail");
}
}
LOOPING STATEMENTS IN JAVA
FOR LOOP
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
WHILE LOOP
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
DO-WHILE LOOP
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
PRINT MULTIPLICATION TABLE
USING FOR LOOP
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
for (int i = 1; i <= 10; i++) {
System.out.println(num + " x " + i + " = " + (num * i));
}
}
}
SUM OF NUMBERS USING WHILE
LOOP
import java.io.*;
public class SumUsingWhile {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the limit: ");
int n = Integer.parseInt(br.readLine());
int sum = 0, i = 1;
while (i <= n) {
sum += i;
i++;
}
System.out.println("Sum = " + sum);
}
}

Conditional Statements in Java. Here we can discuss about if, if..else,if..elseif..else, switch and looping statements like for, while and do..while along with programs.

  • 1.
  • 2.
    IF STATEMENT if (condition){ // block of code } Example:-
  • 3.
    IF-ELSE STATEMENT if (condition){ // true block } else { // false block }
  • 4.
    IF-ELSE IF LADDER if(condition1) { // block1 } else if (condition2) { // block2 } else { // default block }
  • 5.
    SWITCH STATEMENT switch (expression){ case value1: // block break; default: // default block }
  • 6.
    CHECK EVEN ORODD USING IF- ELSE import java.util.Scanner; public class EvenOddScanner { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter an integer: "); int number = sc.nextInt(); if (number % 2 == 0) System.out.println(number + " is Even"); else System.out.println(number + " is Odd"); } }
  • 7.
    PROGRAM WITH BUFFEREDREADER –GRADING WITH IF-ELSE IF import java.io.*; public class GradeChecker { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter your marks: "); int marks = Integer.parseInt(br.readLine()); if (marks >= 90) System.out.println("Grade A"); else if (marks >= 75) System.out.println("Grade B"); else if (marks >= 60) System.out.println("Grade C"); else System.out.println("Fail"); } }
  • 8.
  • 9.
    FOR LOOP for (inti = 1; i <= 5; i++) { System.out.println(i); }
  • 10.
    WHILE LOOP int i= 1; while (i <= 5) { System.out.println(i); i++; }
  • 11.
    DO-WHILE LOOP int i= 1; do { System.out.println(i); i++; } while (i <= 5);
  • 12.
    PRINT MULTIPLICATION TABLE USINGFOR LOOP import java.util.Scanner; public class MultiplicationTable { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter number: "); int num = sc.nextInt(); for (int i = 1; i <= 10; i++) { System.out.println(num + " x " + i + " = " + (num * i)); } } }
  • 13.
    SUM OF NUMBERSUSING WHILE LOOP import java.io.*; public class SumUsingWhile { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the limit: "); int n = Integer.parseInt(br.readLine()); int sum = 0, i = 1; while (i <= n) { sum += i; i++; } System.out.println("Sum = " + sum); } }