Class 19

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

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 a police number");
break;
case 103: System.out.println("It is a enquiry number");
break;
case 108: System.out.println("It is a emergency number");
break;
default : System.out.println("Invalid option");
}
}
}

Declaration of break stmt in switch case is optional.If we won't declare 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 a police number");
//break;
case 103: System.out.println("It is a enquiry number");
//break;
case 108: System.out.println("It is a emergency number");
//break;
default : System.out.println("Invalid option");
}
}
}

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

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

Note:
----
The allowed datatype for switch case are byte,short,int,char and String.

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

Iteration statement is possible by 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 how long 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);
}
}

In do while loop, our code will execute atleast for one time either our condition
is true or false.

ex:
class Test
{
public static void main(String[] args)
{
int i=11;

do
{
System.out.print(i+" "); // 11
}
while (i<=10);
}
}

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

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

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 perform sum of 10 natural numbers?

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

output:
120 (5*4*3*2*1)
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 display 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);

}
}

You might also like