#StoneProfitsSystem


Java - Decision Making Statements and Loops

If Statement:

  • If the Boolean expression evaluates to true then the block of code inside the if statement will be executed. If not the first set of code after the end of the if statement (after the closing curly brace) will be executed.
Syntax:

if(Boolean_expression)
{
   //Statements will execute if the Boolean expression is true
}

Example:



If-else Statement:

  • If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.
Syntax:

if(Boolean_expression){

   //Executes when the Boolean expression is true
}else{
   //Executes when the Boolean expression is false

}

Example:



Nested-if Statement:

  • It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.
Syntax:

if(Boolean_expression 1){

   //Executes when the Boolean expression 1 is true
   if(Boolean_expression 2){
      //Executes when the Boolean expression 2 is true
   }

}

Example:




Switch Statement:

  • switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
Syntax:


switch(expression){
    case value :
       //Statements
       break; //optional
    case value :
       //Statements
       break; //optional
    //You can have any number of case statements.
    default : //Optional
       //Statements
}

Example:



Loop Control:


While Loop:

  • A while loop statement in java programming language repeatedly executes a target statement as long as a given condition is true.
Syntax:

while(Boolean_expression)
{
   //Statements
}

Example:

For Loop:

  • for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax:

for(initialization; Boolean_expression; update)
{
   //Statements
}

Example:


Do While Loop:

  • do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
Syntax:


do
{
   //Statements
}while(Boolean_expression);

Example:


No comments:

Post a Comment