The Artima Developer Community
Sponsored Link

Java Buzz Forum
Control statements in java with examples

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Control statements in java with examples Posted: Apr 29, 2016 9:07 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Control statements in java with examples
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Programs in java will be executed in sequential manner means line by line execution of statements.
  • By using control statements we can control the flow of execution.
  • We have three types of control statements in java.
  1. Decision making Statements.
  2. Iteration statements.
  3. Branching statements. 




1.Decision making statements in java: 

  • In some cases we will have a situation like need to execution set of statements based on a condition
  • In this scenario we need to use decision making statements in java.
  1. if
  2. if-else
  3. if-else-if
  4. switch

Simple if

  • Based on some condition if we want to execute some set of statements then we need to use if condition in java.
  • We need to keep all the required statements inside if block .
  • If the condition is true then control executes the whole block otherwise it skips the if block of statements.

  1. if( condition ){
  2. // code
  3. }

Java example program on if control statement:

  1. package controlstatementsinjava;
  2. import java.util.Scanner;
  3.  
  4. public class ControlStatementsInjava {
  5.  
  6.     /**
  7.      * @website: www.instanceofjava.com
  8.      */
  9. public static void main(String[] args) {
  10.         
  11.   Scanner in= new Scanner(System.in);
  12.  
  13.   System.out.println("Please enter a number");
  14.   int n=in.nextInt();
  15.         
  16. if(n%2==0){
  17.    System.out.println(n+" is even number");
  18.         
  19.  }
  20.   
  21. if(n%2!=0){
  22.      System.out.println(n+" is odd number");
  23. }
  24.  
  25. }
  26. }

Output:

  1. Please enter a number
  2. 5
  3. 5 is odd number

If else condition:

  • If the condition is true then control executes the whole block otherwise it skips the if block of statements
  • if condition is false  if we need to execute another set of statement we can write another if and keep all the statements in that block.
  • instead of checking multiple times we can simply use else block .
  • More about in else statement in java

    1. if( condition ){
    2. // code
    3. }else{
    4. //code
    5. }

     if else control statement with example programs:

    1. package controlstatementsinjava;
    2. import java.util.Scanner;
    3.  
    4. public class ControlStatementsInjava {
    5.  
    6.     /**
    7.      * @website: www.instanceofjava.com
    8.      */
    9. public static void main(String[] args) {
    10.         
    11.   Scanner in= new Scanner(System.in);
    12.  
    13.   System.out.println("Please enter a number");
    14.   int n=in.nextInt();
    15.         
    16. if(n%2==0){
    17.    System.out.println(n+" is even number");
    18.         
    19.  }else{
    20.      System.out.println(n+" is odd number");
    21. }

    22.  
    23. }
    24. }

    Output:

    1. Please enter a number
    2. 5
    3. 5 is odd number


    if else ladder :

    • In some cases we may get requirement with multiple conditions in such cases we need to use if else ladder.(if -else-if-else if -else...)

    1. if( condition ){
    2. // code
    3. }else if{
    4. //code
    5. }else {
    6.  //code
    7. }

     if else if control statement with example programs:


    1. package controlstatementsinjava;
    2. import java.util.Scanner;
    3.  
    4. public class ControlStatementsInjava {
    5.  
    6.     /**
    7.      * @website: www.instanceofjava.com
    8.      */
    9. public static void main(String[] args) {
    10.         
    11.   Scanner in= new Scanner(System.in);
    12.  
    13.   System.out.println("Please enter your age");
    14.   int age=in.nextInt();
    15.         
    16. if(age<=18){
    17.    System.out.println("Your age is between 1-18");
    18.         
    19.  }else if ((age>=18) && (age<=35)){
    20.      System.out.println(" Your age is between 18-35");
    21. }
    22. else if ((age>=35) && (age<=50)){
    23.      System.out.println(" Your age is between 35-50");
    24. }else{
    25.  
    26.   System.out.println(" Your age is above 50");
    27.  
    28. }
    29.  
    30. }
    31. }

    Output:

    1. Please enter your age
    2. 20
    3. Your age is between 18-35

    control statements in java

    Switch statement in java:

    • Instead if writing number of if conditions we can use single switch statement


    1. switch(value)
    2.  case n1: statements;
    3. break;
    4. case n2 : statements;
    5. break;
    6. default: statements;
    7. }

    Java example program on switch statement in java:


    switch statement in java

    Read: Control statements in java with examples

    Topic: Java Tutorial : Java StringBuilder [substring method] Previous Topic   Next Topic Topic: Pitfalls of a Non-technical Manager

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use