java13
java13
==================
Control statement enables the programmer to control flow of our program.
Control statement allows us to make decisions, to jump from one section of code to
another section and to execute the code repeatedly.
2) Selection Statement
3) Iteration Statement
4) Jump Statement
i) if stmt
i) if stmt
-------------
It will execute the source code only if our condition is true.
syntax:
-------
if(condition)
{
-
- //code to be execute
-
}
ex:1
----
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
if(5>2)
{
System.out.println("stmt2");
}
System.out.println("stmt3");
}
}
o/p:
stmt1
stmt2
stmt3
ex:2
----
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
if(5>20)
{
System.out.println("stmt2");
}
System.out.println("stmt3");
}
}
o/p:
stmt1
stmt3
ex:
----
class Test
{
public static void main(String[] args)
{
if(!(5>2))
System.out.println("stmt1");
System.out.println("stmt2");
System.out.println("stmt3");
}
}
o/p:
stmt2
stmt3
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
if(a>b)
System.out.println(a+" is greatest");
if(b>a)
System.out.println(b+" is greatest");
}
}
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
}
}
syntax:
------
if(condition)
{
- // if condition is true
}
else
{
- // if condition is false
}
ex:
----
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
if(5>2)
{
System.out.println("stmt2");
}
else
{
System.out.println("stmt3");
}
System.out.println("stmt4");
}
}
o/p:
stmt1
stmt2
stmt4
ex:
---
class Test
{
public static void main(String[] args)
{
System.out.println("stmt1");
if(5>20)
{
System.out.println("stmt2");
}
else
{
System.out.println("stmt3");
}
System.out.println("stmt4");
}
}
o/p:
stmt1
stmt3
stmt4
Q)Write a java program to find out given age is eligible to vote or not?
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
if(age>=18)
System.out.println("U r eligible to vote");
else
System.out.println("U r not eligible to vote");
}
}
ex:
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
if(n%2==0)
System.out.println("It is even number");
else
System.out.println("It is odd number ");
}
}
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
if(n%2==1 || n%2!=0)
System.out.println("It is odd number");
else
System.out.println("It is not odd number ");
}
}
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
}
}
syntax:
------
if(cond1)
{
- //code to be execute if cond1 is true
}
else if(cond2)
{
- //code to be execute if cond2 is true
}
else if(cond3)
{
- //code to be execute if cond3 is true
}
else
{
- //code to be execute if all conditions are false
}
ex:
---
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
if(option==100)
System.out.println("It is police number");
else if(option==103)
System.out.println("It is enquiry number");
else if(option==108)
System.out.println("It is emergency number");
else
System.out.println("Invalid option");
}
}
Q)Write a java program to check given alphabet is a upper case letter ,lowercase
letter, digit or a special symbol?
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
ex:
import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
if(ch=='a' || ch=='A')
System.out.println("It is a vowel");
else if(ch=='e')
System.out.println("It is a vowel");
else if(ch=='i')
System.out.println("It is a vowel");
else if(ch=='o')
System.out.println("It is a vowel");
else if(ch=='u')
System.out.println("It is a vowel");
else
System.out.println("It is not a vowel");
}
}
Assignment
==========
Q)Write a java program to accept six marks of a student then find out
total , average and grade?