0% found this document useful (0 votes)
7 views7 pages

JBKCORE9009 Control Statements

Uploaded by

Pranay Mhatre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

JBKCORE9009 Control Statements

Uploaded by

Pranay Mhatre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

www.thekiranacademy.com JBKCORE9009-control-statements Mob No.

:8888809416

Control Statements
• if-else
• nested if
• while
• do-while
• for
• switch
• break
• continue

if
package com.javabykiran;

public class If {
public static void main(String[] args) {
int num = 10;
if (num % 2 == 0) // this is condition to be check
{
System.out.println("Number is even"); // true part of
condition
} else {
System.out.println("Number is odd");
}
}
}

1|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9009-control-statements Mob No.:8888809416

If else
package com.javabykiran;

public class ifelse {

public static void main(String[] args) {


int num = 1;
if (num == 1) {
System.out.println("One");
} else if (num == 2) {
System.out.println("Two");
} else if (num == 3) {
System.out.println("Three");
} else {
System.out.println("Wrong input Given");
}
}
}

Nested if
package com.javabykiran;

public class Nestedif {

public static void main(String[] args) {


int x = 10;
int y = 11;
if (x == 10) {
if (y == 10) {
System.out.print("Both variables are equal");
} else {
System.out.println("Both are not equal");
}
}
}
}

2|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9009-control-statements Mob No.:8888809416

Simple if
package com.javabykiran;

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!");
}
}

While
package com.javabykiran;

public class whileloop {

public static void main(String[] args) {


int i = 0; // initialization
while (i < 5) { // condition to be check
System.out.println("i is : " + i);
i++; // update statment
}
}
}

3|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9009-control-statements Mob No.:8888809416

Do while
package com.javabykiran;

public class DoWhileLoop {


public static void main(String[] args) {
int i = 0;
do {
System.out.println("i is : " + i);
i++;
} while (i < 5);
}

Switch Case
package com.javabykiran;

public class Switchcase {


public static void main(String[] args) {
int day = 2;
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thuesday");
break;

4|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9009-control-statements Mob No.:8888809416

case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid Input");
}
}
}

for
package com.javabykiran;

public class checkOddNumber {

public static void main(String[] args) {


for (int i = 1; i < 100; i++) {
if (i % 2 != 0) {
System.out.println(i);
}
}
}
}

5|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9009-control-statements Mob No.:8888809416

break
The break statement in java is used to terminate a loop and break the current
flow of the program.

package com.javabykiran;

public class BreakTest {

public static void main(String args[]) {


for (int i = 5; i < 10; i++) {
if (i == 8)
break;
System.out.println(i);
}
}
}

continue
To jump to the next iteration of the loop, we make use of the continue
statement. This statement continues the current flow of the program and skips a
part of the code at the specified condition.

package com.javabykiran;

public class ContinueTest {


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 + " ");
}
}
}

6|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9009-control-statements Mob No.:8888809416

Homework
• Solve test from jbktest.com - Control Statement
• start solving milestones assignments

Download
https://drive.google.com/drive/folders/19_zVro_6ukYMtSquV99l-
XApMm3Y_Exf?usp=sharing

7|Page www.jbktest.com

You might also like