0% found this document useful (0 votes)
4 views12 pages

Java 14

The document explains nested if statements, switch-case statements, and iteration statements in Java. It provides syntax, examples, and sample programs for each concept, including how to determine if a number is positive or negative, identify vowels and consonants, and perform iterations using loops. Additionally, it covers the use of break statements in switch cases and the allowed data types for switch conditions.

Uploaded by

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

Java 14

The document explains nested if statements, switch-case statements, and iteration statements in Java. It provides syntax, examples, and sample programs for each concept, including how to determine if a number is positive or negative, identify vowels and consonants, and perform iterations using loops. Additionally, it covers the use of break statements in switch cases and the allowed data types for switch conditions.

Uploaded by

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

iv)nested if stmt

=================
If statement contains another if statement is called nested if stmt.

syntax:
--------
if(condition)

{
if(condition)
{
-
- //code to be execute
-
}
}

ex:
----
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
if(5>2)
{
System.out.println("stmt2");
if(true)
{
System.out.println("stmt3");
}
System.out.println("stmt4");
}
System.out.println("stmt5");
}
}
o/p:
stmt1
stmt2
stmt3
stmt4
stmt5

ex:
---
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
if(5>2)
{
System.out.println("stmt2");
if(false)
{
System.out.println("stmt3");
}
System.out.println("stmt4");
}
System.out.println("stmt5");
}
}
o/p:
stmt1
stmt2
stmt4
stmt5

ex:
----
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
if(5>20)
{
System.out.println("stmt2");
if(true)
{
System.out.println("stmt3");
}
System.out.println("stmt4");
}
System.out.println("stmt5");
}
}

Q)Write a java program to find out given number is +ve or -ve ?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt();

if(n!=0)
{
if(n>0)
{
System.out.println("It is a positive number");
System.exit(0);
}
System.out.println("It is negative number");
}
}
}

2) Selection statement
=======================

switch case
===========
It will execute the source code based on multiple conditions.
It is similar to if else if ladder.

syntax:
------
switch(condition)
{
case value1: //code to be execute
break stmt;

case value2: //code to be execute


break stmt;
-
-
default: //code to be execute if all cases are false.
}

ex:
----
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the option :");
int option=sc.nextInt();

switch(option)
{
case 100: System.out.println("It is police number");
break;
case 103: System.out.println("It is enquiry number");
break;
case 108: System.out.println("It is emergency number");
break;
default: System.out.println("Invalid option");
}
}
}

Declaration of break statement is optional in switch case.If we won't define break


statement then from where our condition is satisfied from there all cases will be
executed that state is called fall through state of switch case.

ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the option :");
int option=sc.nextInt();

switch(option)
{
case 100: System.out.println("It is police number");
case 103: System.out.println("It is enquiry number");

case 108: System.out.println("It is emergency number");

default: System.out.println("Invalid option");


}
}
}

Q)Write a java program to find out given alphabet is a vowel or consonent?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the alphabet :");
char ch=sc.next().charAt(0);

switch(ch)
{
case 'a': System.out.println("It is a vowel"); break;
case 'e': System.out.println("It is a vowel"); break;
case 'i': System.out.println("It is a vowel"); break;
case 'o': System.out.println("It is a vowel"); break;

case 'u': System.out.println("It is a vowel"); break;


default: System.out.println("It is a consonent");
}
}
}

The allowed datatype for switch case are byte,short,int,char and String.
if we take other datatypes then we will get compile time error.

ex:
--
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String :");
String str=sc.next();

switch(str)
{
case "one": System.out.println("January"); break;
case "two": System.out.println("February"); break;
case "three": System.out.println("March"); break;
case "four": System.out.println("April"); break;

case "five": System.out.println("May"); break;


default: System.out.println("Coming Soon...");
}
}
}

3)Iteration statement
======================
Iteration statement is used to execute the code repeatedly.

Iteration statement is possible using loops.

We have four types of loops.

i) do while loop

ii) while loop

iii) for loop

iv) for each loop

i) do while loop
-----------------
It will execute the source code untill our condition is true.

syntax:
------
do
{
-
- //code to be execute
-
}while(condition);

ex:
---
class Test
{
public static void main(String[] args)
{
int i=1;
do
{
System.out.print(i+" "); // infinite 1
}
while (i<=10);
}
}

ex:
----
class Test
{
public static void main(String[] args)
{
int i=11;
do
{
System.out.print(i+" ");//11
}
while (i<=10);
}
}
Note:
------
In do while loop, our code will execute atleast for one time either our condition
is true or false.

Q)Write a java program to display 10 natural numbers?

class Test
{
public static void main(String[] args)
{
int i=1;
do
{
System.out.print(i+" "); // 1 2 3 4 5 6 7 8 9 10

i++;
}
while (i<=10);
}
}

ex:
---
class Test
{
public static void main(String[] args)
{
int i=10;
do
{
System.out.print(i+" "); //10 9 8 7 6 5 4 3 2 1

i--;
}
while (i>=1);
}
}

Q)Write a java program to display sum of 10 natural numbers?

1+2+3+4+5+6+&+8+9+10 = 55

class Test
{
public static void main(String[] args)
{
int i=1,sum=0;
do
{
sum=sum+i;
i++;
}
while (i<=10);

System.out.println(sum);
}
}

Q)Write a java program to find out factorial of a given number?

input:
n=5 (5*4*3*2*1)

output:
120

ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt(); // 5

int i=n,fact=1;
do
{
fact=fact*i;
i--;
}
while (i>=1);

System.out.println(fact);
}
}

Q)Write a java program to find out multiplication table of a given number?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt(); // 5

int i=1;
do
{
System.out.println(n+" * "+i+" = "+n*i);
i++;
}
while (i<10);
}
}

ii) while loop


--------------
It will execute the source code untill our condition is true.
syntax:
while(condition)
{
-
- //code to be execute
-
}

ex:
---
class Test
{
public static void main(String[] args)
{
int i=1;
while(i<=10)
{
System.out.print(i+" ");// infinite 1
}
}
}

ex:
---
class Test
{
public static void main(String[] args)
{
int i=11;
while(i<=10)
{
System.out.print(i+" ");// nothing
}
}
}

Q)Write a java program to display 10 natural numbers ?

class Test
{
public static void main(String[] args)
{
int i=1;
while(i<=10)
{
System.out.print(i+" ");//1 2 3 4 5 6 7 8 9 10
i++;
}

}
}

Q)Write a java program to find out sum of 10 natural numbers?

class Test
{
public static void main(String[] args)
{
int i=1,sum=0;
while(i<=10)
{
sum+=i;
i++;
}
System.out.println(sum);
}
}

You might also like