yt

Header Ads

Chapter 10 What is Control Statements and Types Explain in Details.

Post Intro:- So Frirnds Fainaly Hum log ab es post main Operator Kiya hota hai and Kitne Types ke hota hai, and kaise declared karna hai, and a propar example ke sath dekne bale hai.

Control Statements

Introduction to control statements

आप के पड़ौसी कँही जा रहे थे उन्होंने खुद के घर की चाबी देते हुए कहा की मेरी माँ आये तो उन्हें ये चाबी दे देना। आपके पड़ौसी ने आपको condition बताई है की मेरी माँ आये तो उन्हें ही ये चाबी देना और किसी को मत देना। ये condition आपके चाबी देने के action को control करती है। आपका चाबी देना या ना देना इसी condition पर based है। ऐसे ही जब आप कोई program बनाते है तो उसके statements के execution को किसी condition के द्वारा control कर सकते है। और जब वो condition आये तो आप जो statements execute करवाना चाहते है उन्हें execute करवा सकते है। 

An Other Example:
Let me known, You and Your Girlfriend said to you, if rain is close then go for eat ice-cream.
so here now if rain is close then then go for ice-cream otherwise not go so this is condition .
So hence used this time condition statments, so it called conditinal statement.

An real life example of Email id such as: 

इसका एक उदाहरण है की जब आप Gmail की website पर जाते है तो आपसे id और password माँगा जाता है। जब आप Email और password डालते है तो check किया जाता है की email और password सही है या नहीं। यदि email password सही होते है तो आपको अपने account में मिल जाती है। और email password गलत होने पर आपको एक message show किया जाता है। ये काम control statements की मदद से ही किया जाता है।

Control statements Vo statements होते है जिनसे आप program का execution (flow) control करते है। कोन से statements कब execute होंगे,  ये control statements के द्वारा ही तय किया जाता है। 

Java में आपके 3 तरह के control statements होते है। जिनमे selection statements ek  statements को condition के according select करके execute करते है। Iteration statements के द्वारा कुछ statements को बार बार execute किया जा सकता है। और jump statements program में एक जगह से दूसरी जगह जाने के काम में आते है।


Selection statements 

Selection statements kisi statement को condition check करने के बाद select करके execute करते है। यदि condition true होती है तो कुछ statements को execute किया जाता है और यदि | condition false होती है तो दूसरे किन्ही statements को execute किया जाता है। 

Selection statements मुख्यतः 3 प्रकार के होते है। इनके बारे में निचे दिया जा रहा है।

  • 1. If Statemnets : IF statement के लिए if keyword का यूज़ किया जाता है। इस keyword के बाद brackets में condition दी जाती है। ये condition variables से related भी हो सकती है और कोई normal condition भी हो सकती है। Condition के बाद statements का block kar दिया जाता है। Condition true होने पर सभी statements execute हो जाते है। और यदि condition true नहीं है तो पूरा block skip कर दिया जाता है|
  • It is used to test the condition.
  • If the condition is true its body will execute otherwise does not execute. 

An Example :- 


// 1 if Statements
// Example 1.  Check input Number is even ?

class IfSt1{
  public static void main(String args[]){
    Scanner obj = new Scanner(System.in);
        System.out.println("Enter a Number:");
        int a= obj.nextInt();
        if(a % 2== 0){
          System.out.println("Even Number");
        }
  }

}

Output:- 


BLOG> javac Blog.java
BLOG> java IfSt1
Enter a Number:
20
Even Number



Example 2:- Check Given Number is is Negative or positive or zero?



class Ifst2{
  public static void main(String args[]) {
    Scanner in= new Scanner(System.in);
    System.out.println("Enter a number: ");
    int a= in.nextInt();
    if(a>0){
      System.out.println("This is Positive Number");
    }
    if(a<0){
      System.out.println("This is Negative Number");
    }
    if(a==0)
    System.out.println("This is Zero Number");
  }
}


Output


BLOG> java Ifst2
Enter a number:
45
This is Positive Number

Enter a number:
-12
This is Negative Number

Enter a number:
0
This is Zero Number


Practice Set Q1:- FInd X is greater than other .
Just Suggest:- 

int a = 5, b = 3
if(a>b)
{
System.out.println("A is bigger than B");
}

I Hope this is helful for you.  

  • 2. IF ELSE statements :   IF ELSE statements IF statements की तरह ही होते है। लेकिन IF ELSE statements condition false होने पर भी कुछ statements को execute करवा सकते है। 
  • It is used to test the condition.
  • If the condition is true body of if will execute body of else execute.

Example 1:   Check given number is even or odd?


class Ifelse{
  public static void main(String args[]){
    Scanner obj = new Scanner(System.in);
        System.out.println("Enter a Number:");
        int a= obj.nextInt();
        if(a % 2== 0){
          System.out.println("Even Number");
        }
        else{
          System.out.println("Odd Number");
        }
  }
}


Output:


JAVA OOP\BLOG> javac Blog.java
PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> java Ifelse

Enter a Number:
45
Odd Number

46 Even Number


Example 2:   Check input two number who is greater ?


class Ifelse1{
  public static void main(String args[]){
    Scanner obj = new Scanner(System.in);
        System.out.println("Enter a Number:");
        int a= obj.nextInt();
        System.out.println("Enter b Number:");
        int b= obj.nextInt();
        if(a>b){
          System.out.println("A is greater");
        }
        else{
          System.out.println("B is greater");
        }
  }
}


Output:- 


Enter a Number:
45
Enter b Number:
65
B is greater



// Practice Set:- Q1. Find a number  Negative or positive or zero

  • 3. If else Ladder: 

  • It is used to test the condition.
  • If the executes only one condition at a time.
  • The condition which is true first from the top will executes.
Example 1:-   FInd a Number Neg(-) or Pos(+) or Zero(0) .


Example 1. Find a number  Negative or positive or zero

class Ladderif{
    public static void main(String args[]) {
      Scanner in= new Scanner(System.in);
      System.out.println("Enter a number: ");
      int a= in.nextInt();
      if(a>0){
        System.out.println("This is Positive Number");
      }
      else if(a<0){
        System.out.println("This is Negative Number");
      }
      else if(a==0){
        System.out.println("This is Zero Number");
      }
  }
}


Output:- 

\BLOG> java Ladderif

Enter a number:
0
This is Zero Number


Example 2: Check Division according to the precentage  ?



Example 2: Check Division according to the precentage

class Ladderif2{
    public static void main(String args[]) {
      Scanner in= new Scanner(System.in);
      System.out.println("Enter Your Precentage: ");
      float p= in.nextInt();
      if(p>=60 && p<=100){
        System.out.println("1st Division");
      }
      else if(p>=45 && p<=60){
        System.out.println("2nd Division");
      }
      else if(p>= 25 && p<= 45){
        System.out.println("3rd Division");
      }
      else if(p>=0  && p<= 25){
        System.out.println("Sorry!, You are fail");
      }
      else{
        System.out.println("Invailed Number");
      }
  }
}



Output:-



java Ladderif2
Enter Your Precentage:
45
2nd Division


  • 4. Nested If  : 
  • It is is used to test the condition.
  • One if Inside another if is called nested if. 
An Example of Nested if in java 

Example 1: Chech condition is belong  from between 50. 


class Nestedif{
  public static void main(String[] args) {
    int num=50;
      if (num>2){
        if(num <10){
          System.out.println(" 10 is lessthan");
        }
        System.out.println("2 is greaterthan");
      }
  }
}

Output:-

\JAVA OOP\BLOG> java Nestedif
 2 is greaterthan


Example 2: Check between three number who is greater. 


class Nestedif1{
    public static void main(String[] args){
      Scanner in= new Scanner(System.in);
      System.out.println("Enter a Number: ");
      int a= in.nextInt();
      System.out.println("Enter b Number: ");
      int b= in.nextInt();
      System.out.println("Enter c Number: ");
      int c= in.nextInt();
     
      if (a>b){
          if(a>c){
            System.out.println("A is greater");
          }
      }
      if (b>a){
            if(b>c){
              System.out.println("B is greater");
            }
      }
      if (c>a){
        if(c>b){
          System.out.println("C is greater");
        }
      }
 
  }
}


Output:- 


JAVA OOP\BLOG> java Nestedif1
Enter a Number:
4
Enter b Number:
5
Enter c Number:
6
C is greater


  • 5. Switch Statement in Java  :  Switch statements switch keyword यूज़ करते हुए एक condition pass की जाती है। और निचे cases दिए हुए होते है। जिस case से condition match कर जाती है वही case execute हो जाता है। जैसे की आपके variable की value 3 है तो तीसरे case के statements execute होंगे। कभी ऐसा भी हो सकता है की आपका कोई भी case condition से match नहीं करे। तो situation default case execute होता है।


  • Switch Statement allows us to execute one statement from many statements and the statements are called Switch Case.
  • Inside the body of switch there are a number a of cases and there is a single number is passed at the place of parameter to select and execute a case.
  • In this switch statements a value/ number is passed in the parameter and the case from which the case from which the parameter is matched is executed.
  • If no case matched with parameter then default case will execute.
An Example of Switch Statements in Java:

Example 1:  Print the day name given by input user.


class Switch1{
        public static void main(String[] args){
          Scanner in= new Scanner(System.in);
          System.out.println("Enter Number between 1 to 7: ");
          int day= in.nextInt();
          switch (day) {
            case 1:
            System.out.println("Monday");
              break;
            case 2:
            System.out.println("Tuesday");
              break;
            case 3:
            System.out.println("Wednesday");
              break;
            case 4:
            System.out.println("Thursday");
              break;
            case 5:
            System.out.println("Friday");
              break;
            case 6:
            System.out.println("Saturday");
              break;
            case 7:
            System.out.println("Sunday");
              break;
            default:
            System.out.println("Invalied Input");
              break;
          }
        }
}


Output:-

PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> java Switch1
Enter Number between 1 to 7:
5
Friday



Example 2: Input Any Character check vowel or constants .


class Switch2{
  public static void main(String[] args){
    Scanner in= new Scanner(System.in);
    System.out.println("Enter Any Alphabets Character: ");
    char alp= in.next().charAt(0);
    switch (alp){
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
     
      case 'A':
      case 'E':
      case 'I':
      case 'O':
      case 'U':
     
      System.out.println("Vowel");
      break;
      default:
      System.out.println("Constant");
        break;
    }
   
  }
}


Output:- 


BLOG> java Switch2
Enter Any Alphabets Character:
d
Constants


I hope this post is very helful. thanks for watchnig.


No comments

Theme images by Dizzo. Powered by Blogger.