Posts

Showing posts with the label Part C : JAVA Loops

Switch statement (Part 6)

Image
The Switch Statement A 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 : // Statement s break; // optional case value : // Statements break; // optional // You can have any number of case statements. default : // Optional // Statements } Flowchart:- Eg:- import java.util.Scanner; public class MyClass { public static void main(String args[]) { Scanner scan = new Scanner (System.in); System.out.println(" Enter a value: "); int day = scan .nextInt(); switch ( day ) { case 1: System.out.println(" Monday "); case 2: System.out.println(" Tuesday "); case 3: ...

The do-while Loop (Part 5)

Image
  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. The do-while loop  is a exit controlled loop , as it will run about the first time without even checking the condition and afterwards the do statement will be executed as long as the while statement condition remains true. Syntax:- do { // code block to be executed } while (condition); Flowchart:- Eg:- public class MyClass { public static void main(String args[]) { int num = 1; String str = " Hello "; do { System.out.println( str + " World "); num ++; } while ( num <= 5); } } Output:- Hello World Hello World Hello World Hello World Hello World < Previous Next >

While Loop (Part 4)

Image
While Loop The  while  loop loops through a block of code as long as a specified condition is true , as soon as block of code fails the condition of  while  loop   , it exits out of loop. While loop is also an entry controlled group. ➤We use  For Loop when we know the number of times the loop needs to be executed , while while loop  is used when we dont know the number of times the loop needs to be executed.  The working of both loops is similar. Syntax:- while (condition) { // code block to be executed } Flowchart:- Eg:- public class MyClass { public static void main(String args[]) { int age = 12; while ( age <=18) { System.out.println(" Age is: "+ age ); age ++; } } } Output:- Age is: 12 Age is: 13 Age is: 14 Age is: 15 Age is: 16 Age is: 17 Age is: 18 < Previous Next >

For Loop (Part 3)

Image
  For Loop When you know exactly how many times you want to loop through a block of code, we use the  for  loop. The block of code runs until the conditions of for loop are satisfied , as soon as block of code fails the condition , it exits the loop . The for loop  is also called as an entry controlled loop , as before executing condition is checked. Syntax:- for (initialization ; condition ; increment/decrement operator) { //block of code } Flowchart:- Eg:- public class MyClass { public static void main(String args[]) { int num = 5; for (int i =1; i <=5; i ++) { System.out.println(" The block has run " + i +" times ."); } } } Output:- The block has run 1 times. The block has run 2 times. The block has run 3 times. The block has run 4 times. The block has run 5 times. < Previous Next >

If - else statement (Part 2)

Image
  Java if-else Statement The Java if-else statement  tests the condition , it executes the  if block if condition is true otherwise  else block is executed. Syntax:- if (condition) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false } Flowchart:- Eg:- public class MyClass { public static void main(String args[]) { int time = 20; if ( time < 18) { System.out.println(" Good day. "); } else { System.out.println(" Good evening. "); } } } Output:- Good day. Ternary Operator In place of if-else  sometimes ternary operator is used as it replaces multiple lines of code into a single line Syntax:- variable = (condition) ? expressionForTrue : expressionForFalse; Eg:- Above block of code by replacing if-else with t ernary operator   can be written as: int time = 20; String result = ( time < 18...

If statement (Part 1)

Image
 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 } Flowchart: (Representation of loop in form of diagram) Eg:- public class MyClass { public static void main(String args[]) { int x = 10; if( x < 20 ) { System.out.print(" This is if statement "); } } } Output:- This is if statement. < Previous Next >