can anyone give me a sample code of a while statement with more than one condition? please...

Recommended Answers

Simple example:

static int MAX_TESTS = 5;
int test = 0;
boolean result = true;
int something = 6;
while(test < MAX_TESTS && result)
{
  result = (something > 7);
  test++;
}

EDIT: Basically, the while loop takes a boolean condition. This can be the result of a single …

Jump to Post

All 3 Replies

Simple example:

static int MAX_TESTS = 5;
int test = 0;
boolean result = true;
int something = 6;
while(test < MAX_TESTS && result)
{
  result = (something > 7);
  test++;
}

EDIT: Basically, the while loop takes a boolean condition. This can be the result of a single boolean method or evaluation, or multiple boolean results and'ed or or'ed together (or any other boolean arithmetic).

A conditional expression with more than 2 statement is just the same
as two separate conditional expression.

For example :

while( gameIsRunning )
{
        if( keyPressed )
            if( keyPressedIsX){ .. }
}

Is the same as :

while(gameIsRunning)
{
    if(keyPressed && keyPressedIsX) { ... }
}

So you see, you can think of a conditional loop with more than
1 expression as 2 or more.

Here are more examples :

do{
}while( playerIsAlive && ScoreIsNotNegative);
int grade = 0;
grade = scanner.nextInt();
if( grade >= 90 ) System.print(" You got an A" );
else if( grade >= 80 ) System.print("You got an B");
else System.print("You didn't get a B or an A\n";

thank you guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.