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

By | March 10, 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 : :


 


OUTPUT : :


 

 

Leave a Reply