Decision Making Conditional Statement in Java

0
Decision Making Conditional Statement in Java
Decision Making Conditional Statement in Java

📑 Table of Contents

Definition of Conditional Statement in Java

Conditional Statement in Java:  A java conditional statement can be using decision making control system. It can be used condition are satisfied or not satisfied. If the given condition are true and given condition are false than return compiler are false.

It is very most important in every computer programming languages because conditional statement are handle every situation of condition. Conditional statement are different name is called like Decision makingCondition based, and more.  I can explain java every topic of conditional statement related simple and easy language. 

Read C Conditional Statement

Types of Conditional Statements in Java

  • If Statement in Java
  • If-Else Statement in Java
  • Else-If Statement in Java
  • Nested If Statement in Java
  • Switch Statement in Java

If Conditional Statement in Java

if Statement: If the given condition is true than if body will be executed or given condition are false than no if body will be executed. In the case of place if in the place of condition always 0 and non 0 values is checked. Its means condition false and non 0 condition is true. It is called if conditional statement.

Syntax of If Statement in Java

if(condition)
{
Statements;
}

Explain If Statement With Syntax

int age=18;
if(age>=18)
{
System.out.println("Your age is 18 this statement are executed");
}

Example of If Statement in Java

import java.util.Scanner;
public class condition {
public static void main(String[] args) {
int age;
Scanner s=new Scanner(System.in);
System.out.print("Enter Your Age:");
age=s.nextInt();
if(age>=18)
{
System.out.println("
Your Age 18 You can derive a car");
}
}
}

============OUTPUT============
Enter Your Age:18
Your Age 18 You can derive a car

If Else Conditional Statement in Java

If-Else Statement: A if-else statemen can be used two condition are applied if condition are true and if statement/ body will be executed or if condition is false than else statement/body will be executed. It can be most useful statement in every programming language.

Syntax of If Else Statement in Java

if(condition)
{
Statements;
}
else
{
Statements;
}

Explain If Else Statement in Java

int age=18;
if(age>=17)
{
System.out.println("Your age is 18 this statement are executed");
}
else
{
System.out.println("Your age is less than 18 this statement are executed");
}

Example of If Else Statement in Java 

import java.util.Scanner;
public class condition {
public static void main(String[] args) {
int age;
Scanner s=new Scanner(System.in);
System.out.print("Enter Your Age:");
age=s.nextInt();
if(age>=18)
{
System.out.println("Your Age 18 You can derive a car");
}
else
{
System.out.println("Your Age is less 18 you can not derived a car");
}
}
}

============OUTPUT============
Enter Your Age:17
Your Age is less 18 you can not derived a car

Else If Ladder Statement in Java

Else-if Statement: A else-if statement can be used where multiple condition are applied that use else if statement. It is a part of decision making  that execute only one condition at a time. If all condition is false than else part will be execute. If the condition are first condition are true than first statement are executed. if the first condition are false than checked second condition when second condition are true than second statement are executed.

Syntax of Else If  Ladder Statement in Java

if(condition1)
{
Statements 1;
}
else if(condition2)
{
Statement 2;
}
else if(condition3)
{
Statement 3;
}
else
{
Else Statement
}

Explain Else If Statement With Syntax

int age=18;
if(age>=18)
{
System.out.println("Your age is 18 this statement are executed");
}

Else If Statement in Java Example Program

import java.util.Scanner;
public class condition {
public static void main(String[] args) {
int per;
Scanner s=new Scanner(System.in);
System.out.print("Enter Your Percentage:");
per=s.nextInt();
if(per<=100&&per>=60){
System.out.print("You got First Division");
}
else if(per<=60 &&per>=50){
System.out.print("You got Second Division");
}
else if(per<=50 &&per>=33){
System.out.print("You got Third Division");
}
else{
System.out.print("You are Fail...");
}

}
}

============OUTPUT============
Enter Your Percentage:61
You got First Division

Nested Conditional Statements in Java

Nested If Statement: A nested statement can be used one inside if to another if that is called nested if statement. In the case if if in the place of condition always 0 and non 0 value is checked in which 0 means condition is false and non 0 means condition is true.

Syntax of Nested If Statement in Java

if(condition1)
{
if(condition 2)
{
Statements;
}
}

Explain Nested If Statement With Syntax

int x,y;
if(x>=y)
{
    if(x<=y){
        
System.out.println("This statement Executed");
}
}


Nested If Statement in Java Example Program

public class condition {
public static void main(String[] args) {

int x=10;
if(x>5){
     if(x<15){
System.out.println("x is greater than 5 and less than 15");
}
}
}
}

============OUTPUT============
x is greater than 5 and less than 15

Switch Statement in Java Definition

Switch Statement: A java switch statement is allows us to execute one statement from many statement and that statements are called case. Switch statement inside the body of switch a number of cases are used and a parameter are passed. If parameter number is matched the any cases than statement are executed. If the parameter number are not matched than default case are executed. 

In the switch statement a value is passed in the place of parameter and the case from the parameter is matched is executed. If no case matched with parameter

Syntax of Switch Statement in Java

switch(condition1)
{
case 1:
        statement 1;
        break;
case 2:
        statement 2;
        break;
case 3:
        statement 3;
        break;
default:
       default Statement;
}

Explain Switch Statement With Syntax

int n=2;
switch(2)
{
case 1:
        
System.out.println("Case 1 executed");
        break;
case 2:
        
System.out.println("Case 2 executed");
        break;
case 3:
       
System.out.println("Case 3 executed");
        break;
default:
        
System.out.println("Case are not matched");
}


Switch Statement in Java Simple Example

import java.util.Scanner;
public class condition {
public static void main(String[] args) {
int n;
Scanner s=new Scanner(System.in);
System.out.print("Enter any Number:");
n=s.nextInt();
switch(n)
{
case 1:
        System.out.print("Case 1 executed");
                break;
case 2:
        System.out.print("Case 2 executed");
                break;
case 3:
        System.out.print("Case 3 executed");
                break;
default:
        
System.out.print("Case are not Matched");
}
}
}

============OUTPUT============
Enter any Number:3
Case 3 executed

Switch Statement in Java Simple Example 2

import java.util.Scanner;
public class condition {
public static void main(String[] args) {
char c;
Scanner s=new Scanner(System.in);
System.out.print("Enter any Character:");
c=s.next().charAt(0);
switch(c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.print("You Entered Vowel Character is:"+c);
break;
default:
        System.out.print("You Entered Consonant Character is:"+c);
}
}
}

============OUTPUT============
Enter any Character: f
You Entered Consonant Character is: f

Conclusion of Conditional Statement in Java

Conclusion: This is all about java conditional statement in very simple easy concept and with example. It is very most important features of every programming languages. consonant It can be used condition creaking true and false. I can explain every topic in this article if your any question or query you can contact me by email or comments. I can provide solution simple and easy way.   


Post a Comment

0 Comments
Post a Comment (0)
To Top