0% found this document useful (0 votes)
4 views

java project

The document contains Java code examples demonstrating various control flow structures including if, else if, switch cases, and loops (while and for). It also includes examples of break and continue conditions within loops. Each code snippet is accompanied by a description of its functionality and expected output.

Uploaded by

shresthabiju6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

java project

The document contains Java code examples demonstrating various control flow structures including if, else if, switch cases, and loops (while and for). It also includes examples of break and continue conditions within loops. Each code snippet is accompanied by a description of its functionality and expected output.

Uploaded by

shresthabiju6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1 if case

import java.util.Scanner;
public class CWH_chapter_4 {
public static void main(String[] args) {

Scanner s=new Scanner(System.in);


System.out.println("Enter your age :");
int age=s.nextInt();
if (age>= 20)
{
System.out.println("yes you can drive!");
}
else {
System.out.println(" sorry you can't drive!");
}
Output

2 Else if case
import java.util.Scanner;
public class CWH_chapter_4 {
public static void main(String[] args) {
System.out.println("Enter you age :");
Scanner s = new Scanner(System.in);
int a;
a = s.nextInt();

if (a > 20) {
System.out.println("yess ");
} else if (a > 35) {
System.out.println("ok");
} else if (a > 15) {
System.out.println("no ");
} else if (a > 50) {
System.out.println("happy happy ");
} else {
System.out.println("super happy");
}
}
}

Output
3 Switch case
import java.util.Scanner;
public class CWH_chapter_4 {
public static void main(String[] args) {

System.out.println("Enter your age :");


Scanner s = new Scanner(System.in);
int a;
a = s.nextInt();
switch (a) {
case 20:
System.out.println("wooo");
break;
case 30:
System.out.println("a");
break;
case 40:
System.out.println("b");
break;
case 50:
System.out.println("c");
break;
case 60:
System.out.println("d");
break;
default:
System.out.println("sorry you put incorrect age");
}
System.out.println(" thanks for using my code");
}
}

Output
4 Making calculator
import java.util.Scanner;
public class CWH_chapter_4 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Entr the first number: ");
int nu1 = s.nextInt();

System.out.println("Enter second number :");


int nu2 = s.nextInt();

System.out.println("add multipliy subract divide");


String a = s.next();

switch (a) {
case "add":
System.out.println("the result is = " + (nu1 + nu2));
break;

case "multiply":
System.out.println(" the result is = " + (nu1 * nu2));
break;

case "subtract":
System.out.println("the result is = " + (nu1 - nu2));
break;

case "divide":
System.out.println(" the result is = " + (nu1 / nu2));
break;

default:
System.out.println("error");

}
}
}

Output
4 While loop
public class CWH_chapter_5 {
public static void main(String[] args) {
// while loop
int a =1;
while (a <= 10)
{
System.out.println(a);
a++;
}

Output
5 for loop
public class CWH_chapter_5 {
public static void main(String[] args) {
int a = 7;
int i;
for (i = 0; i < a; i++) {
System.out.println(2 * i);
}
}
}

Output

6 Break condition
public class CWH_chapter_5 {
public static void main(String[] args) {
int a = 10, i;
for (i = 0; i <= a; i++) {
System.out.println(i);
if (i == 4) {
System.out.println("break for the work");
break;
}
}
}
}

Output
7 Continue condition
public class CWH_chapter_5 {
public static void main(String[] args) {
int a=5,i;
for (i=0; i<=a; i++)
{
if (i==4)
{
System.out.println("java is continue");
continue;
}
System.out.println(i);
System.out.println("java is going on");

Output

You might also like