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

java13

The document explains control statements in Java, which allow programmers to manage the flow of their programs through decision making, selection, iteration, and jumping. It details various types of decision-making statements, including if statements, if-else statements, and if-else if ladders, along with examples and syntax. Additionally, it provides sample Java programs for practical applications of these control statements.

Uploaded by

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

java13

The document explains control statements in Java, which allow programmers to manage the flow of their programs through decision making, selection, iteration, and jumping. It details various types of decision-making statements, including if statements, if-else statements, and if-else if ladders, along with examples and syntax. Additionally, it provides sample Java programs for practical applications of these control statements.

Uploaded by

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

Control Statements

==================
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.

In java, we have four types of control statements.

1) Decision Making Statement

2) Selection Statement

3) Iteration Statement

4) Jump Statement

1) Decision Making Statement


-----------------------------
Decision making statement is used to declare the conditions in our program.

Decision making statement is possible by using following ways.

i) if stmt

ii) if else stmt

iii) if else if ladder

iv) nested 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

Q)Write a java program to find out greatest of two numbers?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the first number :");


int a=sc.nextInt();

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


int b=sc.nextInt();

if(a>b)
System.out.println(a+" is greatest");
if(b>a)
System.out.println(b+" is greatest");
}
}

Q)Write a java program to find out greatest of three numbers?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the first number :");


int a=sc.nextInt();

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


int b=sc.nextInt();

System.out.println("Enter the third number :");


int c=sc.nextInt();

if((a>b) && (a>c))


System.out.println(a+" is greatest");
if((b>a) && (b>c))
System.out.println(b+" is greatest");
if((c>a) && (c>b))
System.out.println(c+" is greatest");

}
}

ii) if else stmt


-----------------
It will execute the code either our condition is true or false.

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

System.out.println("Enter the Age :");


int age=sc.nextInt();

if(age>=18)
System.out.println("U r eligible to vote");
else
System.out.println("U r not eligible to vote");

}
}

Q)Write a java program to find out given number is even or odd ?

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

if(n%2==0)
System.out.println("It is even number");
else
System.out.println("It is odd number ");

}
}

Q)Write a java program to find out given number is odd or not?

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%2==1 || n%2!=0)
System.out.println("It is odd number");
else
System.out.println("It is not odd number ");

}
}

Q)Write a java program to check given year is a leap year or not?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the year :");


int year=sc.nextInt();

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


{
System.out.println(year + " is a leap year.");
}
else
{
System.out.println(year + " is not a leap year.");
}

}
}

ii) if else if ladder


----------------------
It will execute the source code based on multiple conditions.

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

System.out.println("Enter the option :");


int option=sc.nextInt();

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

System.out.println("Enter the Alphabet :");


char ch=sc.next().charAt(0);

if(ch>='A' && ch<='Z')


System.out.println("It is a upper case letter");
else if(ch>='a' && ch<='z')
System.out.println("It is a lower case letter");
else if(ch>='0' && ch<='9')
System.out.println("It is a digit");
else
System.out.println("It is a special symbol");
}
}

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

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

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?

i) if average is greater then equals to 70 then A grade .

ii) if average is greater then equals to 50 then B grade.

iii) if average is greater then equals to 35 then C grade.

iv) if average is less then 35 then Failed.

You might also like