Posts

Java Course

CONTENTS :   Introduction             >   What is Java  ?             >   History of Java  ?              >  Features of Java  ?       Java Setup             >  Installing Java             >  Setting PATH variable              >  Compiling and execution        Java Basics             >  Syntax             >  Hello World!!             >  Comments in Java              >  Java Keywords              >  Naming Conventions        Data Types & Variables             >  Data Types             >  Reference Data Types              >  Variables         Decision Making             >  if Statement             >  if..else Statement              >  if..else..if Loader             >  Nested if..else Statement             >  Switch Statement         Loops             > while Loop             > do..while Loop              > for Loop             > Enhanced for loops    

Switch Statement

Switch statements are also used when we need our program to make a certain decision based on a condition and then execute accordingly. Syntax: switch(expression) {    case value-1:    statements which will be executed if the value obtained by evaluating the expression is value-1               break;    case value-2:    statements which will be executed if the value obtained by evaluating the expression is value-2               break;    ...    ...    ...    case value-n:     statements which will be executed if the value obtained by evaluating the expression is value-n               break;     default :     statements which will be executed if the value is not matching with any of the above values               break;    }    //statements after the switch block Example: /* * File Name : SwitchStatementDemo1.java */ public class SwitchStatementDemo1 {    public static void main(String args[])    {        int choice = 2;

Nested if..else Statement

You can combine multiple if / if-else /if-else-if ladders when a series of decisions are involved. So you can make sure that your program executes certain instructions when a series of conditions are met. 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 } //Statements which will be executed always      Example: public class Test {    public static void main(String args[])    {        int x = 30;        int y = 10;        if( x == 30 )        {            if( y == 10 )            {            System.out.print("X = 30 and Y = 10");            }        }    } }     Output: X = 30 and Y = 10 Since both the if conditions are true, the program prints "X = 30 and Y = 10". .

if..else..if Loader

Syntax: if(condition-1) {    //Statements which will be executed if condition-1 is true } else if (condition-2) {    //Statements which will be executed if condition-2 is true } . . . . else if (condition-n) {    //Statements which will be executed if condition-n is true } else {    //Statements which will be executed if none of the conditions in condition-1, condition2,…condition-n are true. }    //Statements which will be executed always As you can see in this if-else-if ladder, conditions are evaluated from top. First condition-1 will be evaluated and if this is true, the code inside the if block will be executed. If condition-1 is false, condition-2 will be evaluated. If condition-2 is true, the code inside that else if block will be executed. If condition-2 is false, condition-3 will be evaluated. This will go on like this. If none of the conditions are true, the code inside the else block will be executed. Example: We can take a look on a program which will dis

if..else Statement

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. Syntax: if(condition) {    //Statements which will be executed if the condition is true } else {    //Statements which will be executed if the condition is false } Here if the condition is true, the code which is written inside the curly brackets { } of the if block will be executed.   If the condition is false, the code which is written inside the curly brackets { } of the else block will be executed. Example: We can take a look on a program which will display a message “Success” if a particular value is greater than 5. If this value is not greater than 5, it will display a message “Failure”. It then displays a message “Executed successfully” and complete its execution. /* * FileName :  IfElseStatementDemo1.java */ public class IfElseStatementDemo1 {     public static void main(String args[])     {         //Declaring a variable "test" a

if Statement

There are two types of decision making statements in Java. They are: if statements switch statements The if Statement: An if statement consists of a Boolean expression followed by one or more statements. Syntax: if(Boolean_expression) {   //Statements will execute if the Boolean expression is true } 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. Example: public class TestIf {   public static void main(String args[])   {      int i = 5;      if( i < 10 )      {         System.out.print("This is if statement");      }   } }