JAVA
"WRITE ONCE, RUN ANYWHERE”
Lec--4
By: Zubair Khalid
Java
£ Conditional Statements and their types
£ Loops and their types
To check a condition and execute certain parts of code depending on whether the
condition is true or false. Such statements are called conditional, and are a form of
composite statement.
Conditional Statements
In Java there are two forms of conditional statements
£ if-else (to choose between two alternatives)
£ switch (to choose between multiple alternatives)
The if-else statement allows us to select between two alternatives.
if-else
if (condition )
then-statement
else
else-statement
Syntax:
If we have to realize a multiple-choice selection we can use several nested if-else statements. Java
has also a specific statement that can be used in specific cases to realize in a simpler way a
multiple-choice selection.
switch
switch (expression ) {
case label-1 : statements-1; break;
...
case label-n : statements-n; break;
default: default-statement;
}
Syntax:
Loops are used to execute a statement or set of statements multiple times
Loops
£ while Loop
£ do-while Loop
£ for loop
There are following type of loops in Java:
A while loop is a control flow statement that allows code to be executed repeatedly
based on a given Boolean condition. The while loop can be thought of as a
repeating if statement.
while Loop
while(condition){
Statements;
}
Syntax:
Unlike for and while loops, which test the loop condition at the top of the loop, the do-while
loop 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.
do-while Loop
do{
Statements;
} while(condition);
Syntax:
For loop is a conditional iterative statement which is used to check for
certain conditions and then repeatedly execute a block of code as long as
those conditions are met.
for Loop
for (var_initialization ; condition ; increment/decrement){
Statements;
}
Syntax:

Java -lec-4

  • 1.
    JAVA "WRITE ONCE, RUNANYWHERE” Lec--4 By: Zubair Khalid
  • 2.
    Java £ Conditional Statementsand their types £ Loops and their types
  • 3.
    To check acondition and execute certain parts of code depending on whether the condition is true or false. Such statements are called conditional, and are a form of composite statement. Conditional Statements In Java there are two forms of conditional statements £ if-else (to choose between two alternatives) £ switch (to choose between multiple alternatives)
  • 4.
    The if-else statementallows us to select between two alternatives. if-else if (condition ) then-statement else else-statement Syntax:
  • 5.
    If we haveto realize a multiple-choice selection we can use several nested if-else statements. Java has also a specific statement that can be used in specific cases to realize in a simpler way a multiple-choice selection. switch switch (expression ) { case label-1 : statements-1; break; ... case label-n : statements-n; break; default: default-statement; } Syntax:
  • 6.
    Loops are usedto execute a statement or set of statements multiple times Loops £ while Loop £ do-while Loop £ for loop There are following type of loops in Java:
  • 7.
    A while loopis a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. while Loop while(condition){ Statements; } Syntax:
  • 8.
    Unlike for andwhile loops, which test the loop condition at the top of the loop, the do-while loop 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. do-while Loop do{ Statements; } while(condition); Syntax:
  • 9.
    For loop isa conditional iterative statement which is used to check for certain conditions and then repeatedly execute a block of code as long as those conditions are met. for Loop for (var_initialization ; condition ; increment/decrement){ Statements; } Syntax: