Statement, Condition And Loop

Statement, Condition And Loop


Explain the methods print() and println()?

Ans: A computer program is written to manipulate a given set of data and to display or print the results. Java supports two output methods that can be used to send the results to the screen. print() method println() method.

The print() method sends information into a buffer. This buffer is not flushed until a new line (or end-of-line) character is sent. As a result print() method prints output on one line.

The println() method by contrast takes the information provided and displays it on a line followed by a line feed.

What is an Expression? Explain its different types.

Ans: An Expression is any statement which is composed of one or more operands and return a vale. It may be combination of operators, variables and constants. There are three different types of expressions.

(1) Constant Expressions: 8 * 12 /2

(2) Integral Expressions: formed by connecting integer constants x = (a + b)/2

(3) Logical Expressions: a > b    or a!=b

Mention two different styles of expressing a comment in a program.

Ans: The two ways of inserting a comments in a program are:

(i) using //                     single line comments

(ii) using /*     */           multiple line comments

Differentiate between operator and expression.

Ans: The operations are represented by operators and the object of the operations are referred to as operands. The expression is any valid combination of operators, constant and variables.

What is a compound Statement? Give an Example.

Ans: It is a block of code containing more then one executable statement. In Java the { } is called block and the statements written under {} is called compound statements or block statement. The { } opening and closing braces indicates the start and end of a compound statement.

for(int i=1;i<=5;i++)

{

System.out.println(“Hello”);

System.out.println(“How”);

System.out.println(“are you?”);

}

Java Conditions and If Statements

Java supports the usual logical conditions from mathematics:

    Less than: a < b
    Less than or equal to: a <= b
    Greater than: a > b
    Greater than or equal to: a >= b
    Equal to a == b
    Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

Java has the following conditional statements:

    Use if to specify a block of code to be executed, if a specified condition is true
    Use else to specify a block of code to be executed, if the same condition is false
    Use else if to specify a new condition to test, if the first condition is false
    Use switch to specify many alternative blocks of code to be executed

The if Statement

Use the if statement to specify a block of Java code to be executed if a condition is true.

Syntax

if (condition) {
  // block of code to be executed if the condition is true
}

Note:  that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.

In the example below, we test two values to find out if 20 is greater than 18. If the condition is true, print some text:
Example
if (20 > 18) {
  System.out.println("20 is greater than 18");
}

Switch Statements

Use the switch statement to select one of many code blocks to be executed.
Syntax
switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

This is how it works:
    The switch expression is evaluated once.
    The value of the expression is compared with the values of each case.
    If there is a match, the associated block of code is executed.
    The break and default keywords are optional, and will be described later in this chapter
The example below uses the weekday number to calculate the weekday name:
Example

public class MyClass {
  public static void main(String[] args) {
    int day = 4;
    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;
    }
  }
}

The break Keyword

When Java reaches a break keyword, it breaks out of the switch block. This will stop the execution of more code and case testing inside the block. When a match is found, and the job is done, it's time for a break. There is no need for more testing. A break can save a lot of execution time because it "ignores" the execution of all the rest of the code in the switch block.

The default Keyword

The default keyword specifies some code to run if there is no case match:
Example

int day = 4;
switch (day) {
  case 6:
    System.out.println("Today is Saturday");
    break;
  case 7:
    System.out.println("Today is Sunday");
    break;
  default:
    System.out.println("Looking forward to the Weekend");
}
// Outputs "Looking forward to the Weekend"
Note that if the default statement is used as the last statement in a switch block, it does not need a break.

What is Loops?

Loops can execute a block of code as long as a specified condition is reached. Loops are handy because they save time, reduce errors, and they make code more readable.

While Loop

The while loop loops through a block of code as long as a specified condition is true:
Syntax

while (condition) {
  // code block to be executed
}

In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:
Example

public class MyClass {
  public static void main(String[] args) {
    int i = 0;
    while (i < 5) {
      System.out.println(i);
      i++;
    }  
  }
}
 

Note: Do not forget to increase the variable used in the condition, otherwise the loop will never end!

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Syntax

do {
  // code block to be executed
}
while (condition);

The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:
Example

public class MyClass {
  public static void main(String[] args) {
    int i = 0;
    do {
      System.out.println(i);
      i++;
    }
    while (i < 5);  
  }
}

Note : Do not forget to increase the variable used in the condition, otherwise the loop will never end!
 

For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
Syntax

for (statement 1; statement 2; statement 3) {
  // code block to be executed
 }

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed.

The example below will print the numbers 0 to 4:
Example

public class MyClass {
  public static void main(String[] args) {
    for (int i = 0; i < 5; i++) {
      System.out.println(i);
    }  
  }
}

Statement 1 sets a variable before the loop starts (int i = 0).

Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again, if it is false, the loop will end.

Statement 3 increases a value (i++) each time the code block in the loop has been executed.

Another Example
public class MyClass {
  public static void main(String[] args) {
    for (int i = 0; i <= 10; i = i + 2) {
      System.out.println(i);
    }  
  }
}

Break statement

You have already seen the break statement used in switch statement. It was used to "jump out" of a switch statement.

The break statement can also be used to jump out of a loop.

This example jumps out of the loop when i is equal to 4:
Example

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    break;
  }
  System.out.println(i);
}
 

Continue statement

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

This example skips the value of 4:
Example

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    continue;
  }
  System.out.println(i);
}

You can also use break and continue in while loops