Monday, March 18, 2013

Java Control Structure & Looping Structure with Example

Java Control Structure & Looping structure explained with examples. 

* java if... else statement
* java switch case
* java while loop
* java for loop
* java do....while loop
* java break statement
* java continue statement.
 

Control flow Statement

All application development environment provide decision making process called control flow statements that directs the execution of application. It facilitates the programmer to examine the existing condition and decide suitable flow of action.

It controls the flow statements  that, repetition of loop upto condition is true and when condition is false  it jumps to next statement.

If-else statement

The if-else statement tests the result of a condition, and perform  appropriate action based on result.
The syntax of if-else statement is:
if(condition 1){
 action 1;
}else if(condition 2){
 action 2;
}else{
 action 3;
}

where condition is boolean expression, it returns true or false value;

Example :

class CheckNumber{

      public static void main(String args[]){

            int num = 10;

            if (num % 2 == 0){
                  System.out.println(num + “ is even number );
            } else {
                  System.out.println(num + “ is odd number );
            }
      }
}


switch-case statement

switch-case can be used as an alternative for if-else condition. If the programmer has to make number of decisions, and all the decisions depend on the value of variable, then switch-case is used instead of if-else condition.

Syntanx of switch-case:

switch(expression){
      case 1 :
      action1 statement;
      break;
      case 2:
      action2 statement;
      break;
      .    
      .    
      .
      case N:
      actionN statement;
      break;
      default:
      default statement;
}
where expression: is variable containing the value to be evaluated. It must be of type byte, short, int, char.
      default:  is an optional keyword used to specify the statements to be executed only when all the case statements evaluate to false.


Example :

int month = 1;
String name;
switch (month) {
      case 1:
            name = “January”;
            break;
      case 2:
            name=”February”;
            break;
      .
      .
      .
      default:
            name=”Invalid Month”;
}


While Loop

The while loop executes till condition is specified. It executes the steps mentioned in the loop only if the condition is true. This is useful where programmer doesn't know in advance that how many times the loop will be executed.

Syntanx of while loop:

while (condition) {
      action statements:
.     .
      .
      .
}
Where condition: is any boolean expression that returns a true or false value. The loop continues upto condition returns true value.

Example :

int i = 1;

while(i<=5){
      i++;
      System.out.println(“value of i is : “+i);
}

do-while loop

The do-while loops execute certain statements till the specified condition is true. This loop ensures that the loop body is executed atleast once.

Syntax of do-while loop:
do{
      action statements;
}while(condition){
      action statements;
      .
      .
}

Example :

int j = 8;
do{
      j = j+2;
      System.out.println(“value of j is : “ +j);               
}while(j>=10 && j<=50){
      j = j+5;
      System.out.println(“value of j is : “ +j);
}


for loop

All loops have some common feature: a counter variable that is initialized before the loop begins. The for loop provides the feature that, initialized the variable before loop begins, a condition for counter variable and counter upto which loop lasts.

Syntax of for loop:

for(initialization statements; condition; increment/decrement statements){
      action statements;
      .
      .
}

where initialization statements : sets or initialize the value for counter variable.
condition : A boolean expression that returns true or false value. The loop terminates if false value is returned.
Increment/decrement statements : Modifies the counter variable. This statements are always executed after the action statements, and before the subsequent condition check.

 Example :

for(int i=1;i<=10;i++){
      System.out.println(“Value of i is “ +i);
}


break statement

The break statements are used to,

-        Terminate a statement sequence in a switch statement
-        Exit a loop

 Example :

class BreakExample{

      public static void main(String args[]){
           
            for(int count=1;count<=100;count++;{
                  if(count==10){
                        break;
                  }
                  System.out.println(“The value of num is : “ +count);
            }
      System.out.println(“Outside loop“);
      }
}

continue statement

Sometimes the programmer might want to continue a loop, but stop processing the remainder of the code in its body for a particular iteration. The continue statement can be used for such a scenario. In while and do-while loops, a continue statement can be used, as shown in below example.

 Example :

class ContinueDemo{
     
      public static void main(String args[]){
           
            for(int count = 1; count <= 10; count++){
                  System.out.println(“Value of count before :  “+count);   
                  if(count%2==0){
                        continue;
                  }
                  System.out.println(“Value of count after :  “+count);
            }
      }
}

In above example, whenever even number is iterated in loop, second print statement will not execute.

47 comments:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete
  2. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete
  3. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
    Java Training in Chennai

    ReplyDelete
  4. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  5. Thanks, Now i can Done my BSCS Assignment :P
    Best Like ilm space

    ReplyDelete
  6. very informative blog and useful article thank you for sharing with us , keep posting Data Science online Training India

    ReplyDelete
  7. I enjoy what you guys are usually up too. This sort of clever work and coverage! Keep up the wonderful works guys I’ve added you guys to my blog roll.


    MEAN stack training in Chennai

    MEAN stack training in bangalore

    MEAN stack training in tambaram

    MEAN stack training in annanagar

    MEAN stack training in Velachery

    ReplyDelete
  8. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.

    selenium training in chennai
    aws training in chennai

    ReplyDelete
  9. Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.

    selenium training in chennai
    aws training in chennai

    ReplyDelete
  10. Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me.

    java training in chennai | java training in bangalore

    java online training | java training in pune

    ReplyDelete
  11. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Devops Training in Chennai

    Devops Training in Bangalore

    ReplyDelete
  12. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.


    Data Science training in marathahalli
    Data Science training in btm
    Data Science training in rajaji nagar
    Data Science training in chennai
    Data Science training in kalyan nagar
    Data Science training in electronic city
    Data Science training in USA


    ReplyDelete
  13. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    rpa training in Chennai | rpa training in pune

    rpa training in tambaram | rpa training in sholinganallur

    rpa training in Chennai | rpa training in velachery

    rpa online training | rpa training in bangalore

    ReplyDelete
  14. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.

    rpa training in Chennai

    rpa training in anna nagar | rpa training in marathahalli

    rpa training in btm | rpa training in kalyan nagar

    rpa training in electronic city | rpa training in chennai

    rpa online training | selenium training in training

    ReplyDelete
  15. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.

    rpa training in Chennai

    rpa training in anna nagar | rpa training in marathahalli

    rpa training in btm | rpa training in kalyan nagar

    rpa training in electronic city | rpa training in chennai

    rpa online training | selenium training in training

    ReplyDelete
  16. Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks
    python training in rajajinagar
    Python training in btm
    Python training in usa

    ReplyDelete
  17. This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.
    python training in rajajinagar
    Python training in btm
    Python training in usa

    ReplyDelete
  18. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 

    java training in chennai | java training in bangalore

    java online training | java training in pune

    ReplyDelete
  19. We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

    python training in velachery
    python training institute in chennai


    ReplyDelete
  20. Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks
    Best Online Training From India Instittue

    MSBI Online Training From India

    Mysql Online Training From India

    ReplyDelete
  21. This is a nice post in an interesting line of content.Thanks for sharing this article, great way of bring this topic to discussion.
    Online IT Selfplaced Videos

    Microsoft Azure Selfplaced Videos

    SCCM Selfplaced Videos

    ReplyDelete
  22. Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks
    Best Training and Real Time Support
    Android Training
    Appium Training

    ReplyDelete
  23. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Devops Training in puneDevops Training in pune

    ReplyDelete
  24. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    Best Selenium Training in Chennai | Selenium Training Institute in Chennai | Besant Technologies

    Selenium Training in Bangalore | Best Selenium Training in Bangalore

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    ReplyDelete
  25. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.

    angularjs Training in chennai
    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    ReplyDelete
  26. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.
    Thank you for this blog. This for very interesting and useful.
    Java training in Chennai
    Java training in Bangalore
    Java online training
    Java training in Pune
    Java training in Bangalore|best Java training in Bangalore

    ReplyDelete
  27. Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  28. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    Microsoft Azure online training
    Selenium online training
    Java online training
    uipath online training
    Python online training


    ReplyDelete
  29. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
    linux Training in Chennai | Best linux Training in Chennai
    Unix Training in Chennai | Best Unix Training in Chennai
    Sql Training in Chennai | Best Sql Training in Chennai
    Oracle Training in Chennai | Best Oracle Training in Chennai
    Digital Marketing Training in Chennai | Best Digital Marketing Training in Chennai

    ReplyDelete
  30. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
    Data Science Training in Chennai | Data Science Course in Chennai
    Python Course in Chennai | Python Training Course Institutes in Chennai
    RPA Training in Chennai | RPA Training in Chennai
    Digital Marketing Course in Chennai | Best Digital Marketing Training in Chennai

    ReplyDelete
  31. QuickBooks Payroll Support Phone Number management quite definitely easier for accounting professionals. There are so many individuals who are giving positive feedback if they process payroll either QB desktop and online options.

    ReplyDelete
  32. when you feel something went wrong along with your accounting software and may not discover a way out, you could get tech support team from our experts’ team, working day and night to correct any issues with respect to QuickBooks Customer Support Number.

    ReplyDelete
  33. Any user can try to find available these days payroll update when you head to “employee” menu, selecting “get payroll updates” after which option “update”. Within the window “get payroll updates” you can examine whether you're making use of the latest updates or perhaps not. For every information or update, you can contact QuickBooks Payroll Support Number.

    ReplyDelete
  34. QuickBooks Customer Support Number, a credit card applicatoin solution which will be developed this kind of a means that you can manage payroll, inventory, sales and each other need of small enterprises.

    ReplyDelete
  35. Characteristics like these and many more make our team and its services matchless. Call us anytime at our QuickBooks Enterprise Support to enjoy our exceptional services. You can also mail QuickBooks Enterprise Technical Support at our email.

    ReplyDelete
  36. QuickBooks Tech Support Number was made to meet your every accounting needs and requirement with a fantastic ease. This software grows and your business and perfectly adapts with changing business environment.

    ReplyDelete