0% found this document useful (0 votes)
86 views28 pages

Types of Java Statements: If If-Else Switch

The document discusses different types of Java statements including conditional/selection statements like if, if-else, and switch statements as well as iteration statements like for, while, and do-while loops. It provides details on the syntax and logic of each statement type, examples of how to use them, and notes on avoiding infinite loops. Key points covered include the use of boolean expressions to control program flow, optional elements in for loops, and ensuring loops will eventually terminate.

Uploaded by

manojabhole
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views28 pages

Types of Java Statements: If If-Else Switch

The document discusses different types of Java statements including conditional/selection statements like if, if-else, and switch statements as well as iteration statements like for, while, and do-while loops. It provides details on the syntax and logic of each statement type, examples of how to use them, and notes on avoiding infinite loops. Key points covered include the use of boolean expressions to control program flow, optional elements in for loops, and ensuring loops will eventually terminate.

Uploaded by

manojabhole
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

Types Of Java Statements

• Conditional/Selection Statements
• if
• if-else
• Switch

• Iteration Statements
• for
• while
• do-while
Conditional Statements
• A conditional statement lets us choose which
statement will be executed next by using a
conditional test

• Conditional statements give us the power to make


basic decisions

• Java's conditional statements are


• the if statement

• the if-else statement

• the switch statement


The if Statement
• The if statement has the following syntax:
• if (condition)
statement;
• if is a Java reserved word

• The condition must be a boolean expression.

• If the condition is true, the statement is executed.

• If it is false, the statement is skipped.


3
The if-else Statement
• An else clause can be added to an if statement
to make an if-else statement
if ( condition )
statement1;
else
statement2;
• If the condition is true, statement1 is executed;
if the condition is false, statement2 is executed
• One or the other will be executed, but not both

4
Block Statements
• Several statements can be grouped together into a block
statement
• A block is delimited by braces : { … }
• A block statement can be used wherever a statement is
called for by the Java syntax
• if (guess == answer) {
System.out.println(“You guessed correct!”);
correct++;
} else {
System.out.println(“You guessed wrong.”);
wrong++;
}
5
Nested if Statements
• The statement executed as a result of an if
statement or else clause could be another if
statement

• These are called nested if statements


• An else clause is matched to the last unmatched if
(no matter what the indentation implies!)

• Braces can be used to specify the if statement to


which an else clause belongs

6
Multiway Selection: else if
• Sometime you want to select one option from
several alternatives
if (conditon1) conditon1
true
statement1
evaluated
statement1; false
else if (condition2) conditon2 true
statement2
statement2; evaluated
false
else if (condition3) true
conditon3 statement3
statement3; evaluated

else false

statement4
statement4;
else if example
double numberGrade = 83.6;
char letterGrade;
if (numberGrade >= 89.5) {
Output:
letterGrade = ‘A’;
} else if (numberGrade >= 79.5) { My Grade is 83.6, B
letterGrade = ‘B’;
} else if (numberGrade >= 69.5) {
letterGrade = ‘C’;
} else if (numberGrade >= 59.5) {
letterGrade = ‘D’;
} else {
letterGrade = ‘F’;
}
System.out.println(“My Grade is ” + numberGrade + “, ” + letterGrade);
The switch Statement
• The switch statement provides another means to
decide which statement to execute next

• The switch statement evaluates an expression,


then attempts to match the result to one of
several possible cases

• Each case contains a value and a list of


statements

• The flow of control transfers to statement


associated with the first value that matches
9
The switch Statement
• The general syntax of a switch statement is:

switch switch (expression) {


and case value1:
case statement-list1
are case value2:
reserved statement-list2
words case value3: If expression
statement-list3 matches value2,
} control jumps
to here
The break Statement
• Often a break statement is used as the last
statement in each case's statement list

• A break statement causes control to transfer to


the end of the switch statement

• If a break statement is not used, the flow of


control will continue into the next case

• Sometimes this can be appropriate, but usually


we want to execute only the statements
associated with one case
The default Statement
• A switch statement can have an optional default case
• The default case has no associated value and simply
uses the reserved word default

• If the default case is present, control will transfer to it if


no other case value matches

• Though the default case can be positioned anywhere in


the switch, usually it is placed at the end

• If there is no default case, and no other value matches,


control falls through to the statement after the switch
The switch Statement
• The expression of a switch statement must result
in an integral type, meaning an int or a char

• It cannot be a boolean value, a floating point


value (float or double), a byte, a short, or a long

• The implicit boolean condition in a switch


statement is equality - it tries to match the
expression with a value

• You cannot perform relational checks with a switch


statement
Switch example
char letter = 'b';
switch (letter) { char letter = 'b';
case 'a':
switch (letter) {
System.out.println("A"); case 'a':
break; System.out.println("A");
case 'b': case 'b':
System.out.println("B"); System.out.println("B");
break; case 'c':
case 'c': System.out.println("C");
System.out.println("C"); break;
break; case 'd':
case 'd': System.out.println("D");
break;
System.out.println("D");
default:
break; System.out.println(”?");
default: }
System.out.println(”?");
}
Iteration Statements
• Iteration statements allow us to execute a
statement multiple times
• Often they are referred to as loops
• Like conditional statements, they are controlled by
boolean expressions
• The programmer should choose the right kind of
loop for the situation
The for Statement
• The for statement has the following syntax:
The initialization
is executed once The statement is
Reserved
before the loop begins executed until the
word
condition becomes false

for (initialization; condition; increment)


statement;

The increment portion is executed at the end of each iteration


The condition-statement-increment cycle is executed repeatedly
Logic of a for loop

initialization

condition
evaluated

true false
statement

increment
for Example
final int LIMIT = 5;
for (int count = 1; count <= LIMIT; count++) {
System.out.println(count);
}
Output:

1
2
3
4
5
The for Statement
• Each expression in the header of a for loop is
optional
• If the initialization is left out, no initialization is

performed
• If the condition is left out, it is always

considered to be true, and therefore creates an


infinite loop
• If the increment is left out, no increment

operation is performed
• Both semi-colons are always required in the for
loop header
The while Statement
• The while statement has the following syntax:
while is a while (condition)
reserved word statement;

If the condition is true, the statement is executed.


Then the condition is evaluated again.

• The statement is executed repeatedly until the


condition becomes false.

20
Logic of a while Loop

condition
evaluated

true false

statement

Note that if the condition of a while statement is false


initially, the statement is never executed. Therefore,
the body of a while loop will execute zero or more
times
while Loop Example
final int LIMIT = 5;
int count = 1;
Output:
while (count <= LIMIT) {
System.out.println(count); 1
count += 1; 2
} 3
4
5
Infinite Loops
• The body of a while loop eventually must make
the condition false

• If not, it is an infinite loop, which will execute until


the user interrupts the program

• This is a common logical error


• You should always double check to ensure that
your loops will terminate normally

23
The do Statement
• The do statement has the following syntax:
do and do{
while are statement;
reserved } while (condition);
words

• The statement is executed once initially,and then the


condition is evaluated
• The statement is executed repeatedly until the
condition becomes false
do-while Example
final int LIMIT = 5;
int count = 1;
Output:
do {
1
System.out.println(count); 2
count += 1; 3
4
} while (count <= LIMIT); 5
Comparing while and do
while loop do loop

statement
condition
evaluated
true
true false condition
evaluated
statement
false
Nested Loops
• Similar to nested if statements, loops can be
nested as well

• That is, the body of a loop can contain another


loop

• Each time through the outer loop, the inner loop


goes through its full set of iterations
Choosing a Loop Structure
• When you can’t determine how many times you
want to execute the loop body, use a while
statement or a do statement
• If it might be zero or more times, use a while
statement
• If it will be at least once, use a do statement

• If you can determine how many times you want to


execute the loop body, use a for statement

You might also like