Home | | Sitemap ||Page number :15

Menu Based Programs

You can use if-else to design menu based program. A menu based program has an ability to perform a variety of tasks but it asks the users input and carries out a particular job. Example---Calculator

Nested If statements

A nested if statement contains another if statement inside its block. Look at this format.

if (condition) { if(condition2) { /* Statements…………….

Ending */ } // Some extra statements }

Nested ifs can sometimes be used as a substitute to the “&&” operator. Example

if (x==1 && x== 2) System.out.println (“Condition satisfied”);

OR

if (x==) { if(x==2) System.out.println(“Condition satisfied”); An example demonstrating use of nested ifs.

/* This program gives you a compliment according to your marks and behavior*/

class nested { public static void main(String args[]) { int marks=64; int behm=10;

if(marks>=80 && marks<=100) { if(behm>=8 && behm<=10) System.out.println("You are a Good Boy/Girl...KEEP IT UP!!!"); else

if(behm>=5 && behm<8) System.out.println("You are Really good in studies but can work on you behavior"); else System.out.println("Yu are good in studies...Need to work a lot on your behavior"); } else if(marks>=50 && marks<80) { if(behm>=8 && behm<=10) System.out.println("Average. You are a Good Boy/Girl...KEEP IT UP!!!"); else if(behm>=5 && behm<8) System.out.println("You are average in studies but can work on you behavior"); else System.out.println("Average in Studies...Need to work a lot on your behavior"); } else System.out.println("Improve in your studies..No behavior marks "); } }.

Output---

df

If we edit our input to these figures.

Input -marks=84; behm=7;

The Output will be----- Switch Keyword Just like the “if-else” statement, we ca even use “switch” for conditional execution of our program.

df

REMEMEBR:: We don’t use switch very often as it is not as versatile as “if”. The reason for the limitation of “switch” is its inability to work with a range of values.

General syntax---

switch (data type to check for a condition) { case cond1 { Action; break; } case cond2 { Action; break; } case n; { Action; break; } default { Action; }

A simple program that if a character is a vowel or consonant.

class swtch { public static void main(String args[])

{ int inp='i';

switch (inp) { case 'a': System.out.println("A vowel"); break;

case 'e': System.out.println("A vowel"); break;

case 'i': System.out.println("A vowel"); break;

case 'o': System.out.println("A vowel"); break;

case 'u': System.out.println("A vowel"); break;

default: System.out.println("A Consonant"); } } }

It is easy to judge the meaning of break. The “break” keyword is used to get out of a switch condition. The “break” keyword is widely used in other conditions too. We will discuss about it later in this chapter.

Loops ---

Sometimes we need to repeat an action again and again. Imagine writing a program that prints the line “HAPPY BIRTHDAY” thousand times. Until you use loops, writing such a program is fairly impossible. Java provides us with three kinds of Loops—

  1. while
  2. for
  3. do-while

while

You will often use while loop to repeat statements in a Java program. After going through each and every type of loop you will find that it is best to use this loop when— 1.We are not sure about the number of times we have to repeat a statement.

2. We are not sure if our loop will even execute a single time.

Syntax for while loop

while(condition) { // Statements; every thing we ever wanted to execute }

Example /*A program to print a message 1000 times */

class whloop { public static void main(String args[]) { int c=1;

while(c<=1000) { System.out.println("HAPPY B'DAY"); c++; }

} }

Compile and Execute this program, it will fill your screen with that message. If you will print the message using System.out.print, a nice screen full of message will be printed. Here is a screenshot

d

Now you need to understand the internal working of the program. Look at these statements—

int c=1; while(c<=1000) { System.out.println("HAPPY B'DAY"); c++; }

Here we have defined a condition (c<=1000). The Loop will terminate if this condition goes wrong . We have also declared an integer c which has an initial value of 1. As the loop prints the message, the value of c too increases(Remember the use of increment operator studied in chapter-2) and finally causes the loop to fail when its value changes to thousand.