0% found this document useful (0 votes)
213 views88 pages

Conditional Statements in Java

The document discusses various control statements in Java including if, if-else, nested if, if-else-if ladder, switch-case statements, and jump statements like break, continue, and return. It provides syntax examples and sample code to illustrate how each control statement works in Java programs. Key points covered include the basic syntax for if, if-else, and switch-case statements, as well as rules for break statements in switch cases, nested switches, fall through cases, using enums and strings in switches, and how break, continue, and return statements affect program flow.

Uploaded by

RAAGA VEDA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
213 views88 pages

Conditional Statements in Java

The document discusses various control statements in Java including if, if-else, nested if, if-else-if ladder, switch-case statements, and jump statements like break, continue, and return. It provides syntax examples and sample code to illustrate how each control statement works in Java programs. Key points covered include the basic syntax for if, if-else, and switch-case statements, as well as rules for break statements in switch cases, nested switches, fall through cases, using enums and strings in switches, and how break, continue, and return statements affect program flow.

Uploaded by

RAAGA VEDA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 88

Control Statements in Java

if:
Syntax: if(condition)
if(condition) statement1;
{ statement2;
// Statements to execute if
// condition is true // Here if the condition is true, if
} block
// will consider only statement1 to
be inside
// its block.
// Java program to illustrate If statement
class IfDemo
{
public static void main(String args[])
{
int i = 10;

if (i > 15)
System.out.println("10 is less than 15");

// This statement will be executed


// as if considers one statement by default
System.out.println("I am Not in if");
}
}
Output:
I am Not in if
if-else:
Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
// Java program to illustrate if-else statement
class IfElseDemo
{
public static void main(String args[])
{
int i = 10;

if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Output:
i is smaller than 15
public class Main
{
public static void main(String args[])
{
int a = 15;
if (a > 20)
System.out.println("a is greater than 10");
else
System.out.println("a is less than 10");
System.out.println("Hello World!");
}
}
}
Output:
a is less than 10
Hello World!
nested-if:
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
// Java program to illustrate nested-if statement
class NestedIfDemo
{
public static void main(String args[])
{
int i = 10;

if (i == 10)
{
// First if statement
if (i < 15)
System.out.println("i is smaller than 15");

// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
System.out.println("i is smaller than 12 too");
else
System.out.println("i is greater than 15");
}
}
}
Output:
i is smaller than 15
i is smaller than 12 too
public class Main
{
public static void main(String args[])
{
int s = 18;
if (s > 10)
{
if (s%2==0)
System.out.println("s is an even number and greater than 10!");
else
System.out.println("s is a odd number and greater than 10!");
}
else
{
System.out.println("s is less than 10");
}
System.out.println("Hello World!");
}
}
Output:
s is an even number and greater than 10!
Hello World!
if-else-if ladder: 
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
// Java program to illustrate if-else-if ladder
class ifelseifDemo
{
public static void main(String args[])
{
int i = 20;

if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
Output:
i is 20
switch-case
Syntax:
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
// Java program to illustrate switch-case
class SwitchCaseDemo
{
public static void main(String args[])
{
int i = 9;
switch (i)
{
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
default:
System.out.println("i is greater than 2.");
}
}
}
Output:
i is greater than 2.
public class Music {
public static void main(String[] args) case 6:
{ musicInstrument = "Violin";
int instrument = 4;
String musicInstrument; break;
// switch statement with int data type case 7:
switch (instrument) {
case 1: musicInstrument = "Trumpet";
musicInstrument = "Guitar"; break;
break;
case 2: default:
musicInstrument = "Piano"; musicInstrument = "Invalid";
break;
case 3: break;
musicInstrument = "Drums"; }
break;
case 4: System.out.println(musicInstrument);
musicInstrument = "Flute"; }
break;
case 5: }
musicInstrument = "Ukelele";
break;
Output:
Flute
Rules To Remember

There are a certain rules one must keep in mind while declaring a switch case in
java. Following are a certain points to remember while writing a switch case in
java.
1.We cannot declare duplicate values in a switch case.
2.The values in the case and the data type of the variable in a switch case must be
same.
3.Variables are not allowed in a case, it must be a constant or a literal.
4.The break statement fulfills the purpose of terminating the sequence during
execution.
5.It is not necessary to include the break statement, the execution will move to
the next statement if the break statement is missing.
6.The default statement is optional as well, it can appear anywhere in the block.
Break Statement In Switch Case
case 7:
      System.out.println("july");
      break;
public class Example{
case 8:
public static void main(String args[]){
int month= 7;
      System.out.println("august");
        break;
switch(month){ case 9:
case 1 :
      System.out.println("january");
      System.out.println("september");
      break;       break;
case 2:
      System.out.println("february");
case 10:
      break;       System.out.println("October");
case 3:       break;
      System.out.println("march");
      break; case 11:
case 4:      System.out.println("november");
      System.out.println("april");      break;
      break;
case 5: case 12:
      System.out.println("may");      System.out.println("december");
      break;
     break;
case 6:
      System.out.println("june"); default:
      break;      System.out.println("not valid");
}
}
}
Output: july
Nested Switch Case
public class Example{
public static void main(String args[]){
int tech = 2;
int course = 2;

switch(tech){
case 1:
System.out.println("python");
break;
case 2:
switch(course){
case 1:
System.out.println("J2EE");
break;
case 2:
System.out.println("advance java");
}
}
}
}
Output: advance java
Fall Through Switch Case
public class Example{
public static void main( String args[])
{
int courses = 2;
 
switch(courses){
case 1:
      System.out.println("java");
case 2:
      System.out.println("python");
case 3:
      System.out.println("Devops");
case 4:
      System.out.println("Automation testing");
case 5:
      System.out.println("Hadoop");
case 6:
      System.out.println("AWS");
default:
      System.out.println("check out www.google.com for more");
}
}
}
Output: java
python
Devops
Automation testing
Hadoop
AWS
check out www.google.com for more
Enum In Switch Case
public class Example{
public enum day { s , m , t , w , th, fr, sa };
public static void main(String args[]){
course[] c = day.values();
 for(day today : c)
{
  switch (today){
 case s :
        System.out.println("Sunday");
        break;
case m:
        System.out.println("Monday");
        break;
case t:
        System.out.println("Tuesday");
        break;
case w:
        System.out.println("Wednesday");
        break;
case th:
        System.out.println("Thursday");
        break;
case fr:
        System.out.println("Friday");
        break;
case sa:
        System.out.println("Saturday");
        break;
     }
  }
}
}
Output: Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
String In Switch Case

public class Example{


public  static void main(String args[]){
String player = "batsmen";
 
switch(player){
case "batsmen":
       System.out.println(" Batsmen are players who plays with a bat");
       break;
case "bowler":
       System.out.println("who throws the ball");
       break;
case "wicket-keeper":
       System.out.println("who keeps the ball behind the wickets");
       break;
case "fielder":
       System.out.println("who fields in the ground");
       break;
default:
       System.out.println("no entry present");
}
}
}
Output: Batsmen are players who play with a bat
jump:

Java supports three jump statement: break, continue and return. These


three statements transfer control to other part of the program.
Break: 
// Java program to illustrate using
// break to exit a loop
class BreakLoopDemo
{
public static void main(String args[])
{
// Initially loop is set to run from 0-9
for (int i = 0; i < 10; i++)
{
// terminate loop when i is 5.
if (i == 5)
break;

System.out.println("i: " + i);


}
System.out.println("Loop complete.");
}
}
Output:
i: 0
i: 1
i: 2
i: 3
i: 4
Loop complete.
public class Test
{
public static void main(String args[])
{
for (int i = 5; i < 10; i++)
{
if (i == 8)
break;
System.out.println(i);
}
}
}
Output:
5
6
7
Continue:
// Java program to illustrate using
// continue in an if statement
class ContinueDemo
{
public static void main(String args[])
{
for (int i = 0; i < 10; i++)
{
// If the number is even
// skip and continue
if (i%2 == 0)
continue;

// If number is odd, print it


System.out.print(i + " ");
}
}
}
Output:
13579
public class Main
{
public static void main(String args[])
{
for (int k = 5; k < 15; k++)
{
// Odd numbers are skipped
if (k%2 != 0)
continue;
// Even numbers are printed
System.out.print(k + " ");
}
}
}
Output:
6 8 10 12 14
Return:
// Java program to illustrate using return
class Return
{
public static void main(String args[])
{
boolean t = true;
System.out.println("Before the return.");

if (t)
return;

// Compiler will bypass every statement


// after return
System.out.println("This won't execute.");
}
}
Output:
Before the return.
What is for loop?
Syntax
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Example of for loop

public class MyClass {


{
public static void main(String[] args) {
{for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}}
Output:
0
1
2
3
4
public class forLoop
{
public static void main(String args[])
{
for (int i = 1; i <= 10; i++)
System.out.println(i);
}
}
Output:
5
6
7
8
9
10
For-Each

public class foreachLoop{


public static void main(String args[]){
int s[] = {18,25,28,29,30};
for (int i : s) {
System.out.println(i);
}
}
}
Output:
18
25
28
29
30
Java nested for loop

public class Example{


public static void main(String[] args) {
for(int i=1;i<=3;i++){
for(int j=1;j<=3;j++){
System.out.println(i+" "+j);
}
}
}
}
Output:
11
12
13
21
22
23
31
32
33
Pyramid Example: Case 1

public class PyramidExample {


public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print("* ");
}
System.out.println();//new line
}
}
}
Output:
*
**
***
****
*****
Pyramid Example: Case 2

package MyPackage;
public class Demo {
public static void main(String[] args) {
int term=6;
for(int i=1;i<=term;i++){ for(int j=term;j>=i;j--){
System.out.print("* ");
}
System.out.println();//new line
}
}
}
Output:
* * * * * 
* * * * 
* * * 
* * 
*
What is a While Loop in
Java and how to use it?
Syntax:
while (condition) {

// code block to be executed

}
class Example {
public static void main(String args[]){
int i=10;
while(i>1){
System.out.println(i);
i--;
}
}
}
Output:
10
9
8
7
6
5
4
3
2
// Java While Loop example

package Loops;

import java.util.Scanner;

public class WhileLoop {


private static Scanner sc;

public static void main(String[] args) {


int number, sum = 0;
sc = new Scanner(System.in);

System.out.println("n Please Enter any integer Value below 10: ");


number = sc.nextInt();

while (number <= 10) {


sum = sum + number;
number++;
}
System.out.format(" Sum of the Numbers From the While Loop is: %d ", sum);
}
}
Output:
Please Enter any integer Value below 10: 7
Sum of the Numbers From the While  Loop is: 34
public class whileTest
{
public static void main(String args[])
{
int i = 5;
while (i <= 15)
{
System.out.println(i);
i = i+2;
}
}
}
Output:
5
7
9
11
13
15
Infinite while loop in Java

Syntax:
while (true){
statement(s);
}
class Example {
public static void main(String args[]){
int i=10;
while(i>1)
{
System.out.println(i);
i++;
}
}
}
What is a Do while loop in
Java and how to use it?
Syntax:
do{
//code to be executed
}while(condition);
public class Example {

public static void main(String args[]) {


int x = 1;

do {
System.out.print("value of x : " + x );
x++;
System.out.print("n");
}while( x < 11 );
}
}
Output:
value of x : 1
value of x : 2
value of x : 3
value of x : 4
value of x : 5
value of x : 6
value of x : 7
value of x : 8
value of x : 9
value of x : 10
public class Main
{
public static void main(String args[])
{
int i = 20;
do
{
System.out.println(i);
i = i+1;
} while (i <= 20);
}
}
Output:
20
Infinite do-while loop in Java

Syntax:
do{
//code to be executed
}while(true);
public class DoWhileInfinite {
public static void main(String[] args) {
do{
System.out.println("infinitive do while loop");
}while(true);
}
}
Output:
infinitive do while loop
infinitive do while loop
infinitive do while loop
In order to exit the loop, press ctrl+c.

You might also like