The if Statement

A simple if statement in java consists of the keyword if followed by a logical expression, which is an expression that evaluates to either true or false. These expressions are surrounded by ( ) parentheses. You follow the parentheses with the statement that you want executed if the logical expression is true

if (<logical expression>)

{

<statements executed when true>

}

if (<logical expression>)

{

<statements executed when true>

}

else

{

<statements executed when false>

}

if (<logical expression>)

{

<statements executed when true>

}

else if (<logical expression>)

{

<statements executed when false>

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The else keyword enables you to use a single if statement to choose between two outcomes. When the if statement evaluates to true, the second part of the statement is executed. Otherwise, the else portion is executed. The else if clause is used when there is more than two possible outcomes. [The if statement, 1 of 10]

In the example, the first if statement prints" You chose 5!"when choice is equal to 5. The next if statement uses the else clause. It allows the user to choose between the number 1 and any other number. If the choice is equal to 1 then it assign the value 1 to num and 10 to num 2; else it will assign the value 2 to num and 20 to num2. The last example uses the else if clause. The user is now given three choices (1,2, and 3) and executes the lines corresponding to the choice.

The Switch Statement

Another way you can add decision-making code to your programs is with the switch statement. The switch statements enables a computer program to switch between different outcomes based on given value. Similar to an if statement, a switch statement is more appropriate when you have many choice and on occasion when different sets of values require the same outcome.

 

Switch (<control variable>)

{

case n: statement>;

break;

case n + 1 : …

default : …

}

 

The first line of a switch statement is the keyword switch followed by the variable whose value determines the outcome. This variable is called the control Variable. Inside the main switch statement are a number of case clauses, one for each possible value of the control variable. [The Switch Statement, 3 of 10]

In the first example, y may take on the value 1 , 2 or then y = 0. The next example gets a number’s equivalent word value. [Examples: switch Statement, 4 of 10].

The while Loop

A while loop continues running until its control expression becomes false. Depending on the control expression, the loop may not be executed at all. A do while loop, on the other hand, allows statements inside the {} to be executed at least once.

 

While (<control expression>)

{

}

 

Do

{

}

while (<control expression>);

A while loop consists of the keywords while followed by the control structure enclosed in parenthesis. The do-while loop starts with do keyword followed by the statement(s) to be executed. At the end of the loop is the while keyword and the control expression enclosed in parenthesis. [The while Loop, 5 of 10]

num = 0 ;

while ( num < 10 ) {

++ num ;

String s = String . valueOf (num) ;

g. drawString ("num is now equal to ", 20, 40 ) ;

g. drawString (s, 20, 55) ;

}

In the above example, the program will enter the loop since num is initialized to 0. num will be incremented and will display "num is now equal to : " + the value of num. The loop will be executed while num < 10 (or 10 times).

num = 0 ;

{

+ + num ;

String s = String.valueOf (num) ;

g . drawString ("num is now equal to ", 20, 40 ) ;

g. drawString (s, 20, 55 ) ;

}

while (num < 10 ) ;

In the above example, the program will enter the loop without checking any condition and perform the same function as the first example. [Examples:while Loop, 6 of 10]

 

The for Loop

A for loop instructs a program to perform a block of code a specified number of times. There are many applications for a for loop, including tasks such as reading through a list of data items or initializing an array.

for (<initialization>; <condition>; <increment>)

{

. . .

}

The for construct has three parts: the initialization section, condition section and the increment section.

String strArray[] = new String [10] ;

int i ;

for (i = 0 ; i < strArray. Length; i + + )

strArray [i] = " " ;

 

 

 

In the above example, spaces ("") are assigned to the elements of the array strArray. [Examples: for Loop, 8 of 10]

 

 

The Break Statement

The break statement is used to "break out" of the loop. An example is shown where the break statement is placed inside a for loop. The program breaks out of the for loop when count is equal to 5. [The Break Statement, 9 of 10]

 

The Continue Statement

A continue statement is used to skip a loop. This is different from the break statement, in that it will stay inside the loop. An example is shown where the program skips a loop when count is equal to 5 but will continue executing the loop afterwards. [Examples: The Continue Statement, 10 of 10]