Write a Menu Driven Java Program for If, Switch, While, Do-While and For Loop

By | 10.03.2017

Java Program for Syntax Generator for If, Switch, While, Do-While and For Loop


Write a Menu Driven Java Program for Syntax Generator for If, Switch, While, Do-While and For Loop.In this program , we create a menu driven program to select any choice from multiple option to obtain desired result.


IF Statement :


An if statement consists of a Boolean expression followed by one or more statements.

If the Boolean expression evaluates to true, then the block of code inside the ‘if’ statement will be executed. If the Boolean expression evaluates to false, then the first set of code after the end of the ‘if’ statement (after the closing curly brace) will be executed.


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 switch case.

The following rules apply to a switch statement −

  • The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
  • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

 


WHILE Loop


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

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.

When the condition becomes false, the program control passes to the line immediately following the loop.


 

Do – WHILE Loop


Unlike for and while loops, which test the loop condition at the top of the loop, the do…while loop in programming checks its condition at the bottom of the loop.

A do…while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.

If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false.


FOR Loop


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

Here is the flow of control in a ‘for’ loop −

  • The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
  • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the ‘for’ loop.
  • After the body of the ‘for’ loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
  • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the ‘for’ loop terminates.

 


Here ,below is the source code and the output of the program which is successfully compile and run on the Windows System.


SOURCE CODE : :


 

import java.util.Scanner; 

public class Menu {
  public static void main(String args[]) 
    throws java.io.IOException {
    int choice;
    Scanner sc = new Scanner(System.in);
 
    while(true)
    {
      System.out.println("\n\n-------Syntax Generator for ------------");
      System.out.println("  1. if");
      System.out.println("  2. switch");
      System.out.println("  3. while");
      System.out.println("  4. do-while");
      System.out.println("  5. for");
      System.out.println("  6. Exit the program...\n");
      System.out.println("Choose one:");
      choice = sc.nextInt();
      
      System.out.println("");
  
    switch(choice) {
      case 1:
        System.out.println("The if:\n");
        System.out.println("if(condition) statement;");
        System.out.println("else statement;");
        break;
      case 2:
        System.out.println("The switch:\n");
        System.out.println("switch(expression) {");
        System.out.println("  case constant:");
        System.out.println("    statement sequence");
        System.out.println("  break;");
        System.out.println("  // ...");
        System.out.println("}");
        break;
      case 3:
        System.out.println("The while:\n");
        System.out.println("while(condition) statement;");
        break;
      case 4:
        System.out.println("The do-while:\n");
        System.out.println("do {");
        System.out.println("  statement;");
        System.out.println("} while (condition);");
        break;
      case 5:
        System.out.println("The for:\n");
        System.out.print("for(init; condition; iteration)");
        System.out.println(" statement;");
        break;
      case 6:
          return;
          
    }
    }
    
  }
}

OUTPUT : :


 

-------Syntax Generator for ------------
  1. if
  2. switch
  3. while
  4. do-while
  5. for
  6. Exit the program...

Choose one:
1

The if:

if(condition) statement;
else statement;


-------Syntax Generator for ------------
  1. if
  2. switch
  3. while
  4. do-while
  5. for
  6. Exit the program...

Choose one:
2

The switch:

switch(expression) {
  case constant:
    statement sequence
  break;
  // ...
}


-------Syntax Generator for ------------
  1. if
  2. switch
  3. while
  4. do-while
  5. for
  6. Exit the program...

Choose one:
3

The while:

while(condition) statement;


-------Syntax Generator for ------------
  1. if
  2. switch
  3. while
  4. do-while
  5. for
  6. Exit the program...

Choose one:
4

The do-while:

do {
  statement;
} while (condition);


-------Syntax Generator for ------------
  1. if
  2. switch
  3. while
  4. do-while
  5. for
  6. Exit the program...

Choose one:
5

The for:

for(init; condition; iteration) statement;


-------Syntax Generator for ------------
  1. if
  2. switch
  3. while
  4. do-while
  5. for
  6. Exit the program...

Choose one:
6

 

4.2 6 votes
Article Rating
Category: Basic Programs Java Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments