7/29/2017
1
Programming Constructs . . .
• There are several types of programming constructs in
JAVA.
- If-else construct or ternary operator
- while
- do-while
- for loops
- switch statements
7/29/2017
2
if - else construct . . .
• The if-else construct executes some operation based on successful completion of
a condition, that returns only Boolean values. If the condition is not satisfied, then
the set of instructions under ‘else’ are executed.
Begin
If
(condition)
True
Result When
condition is true
7/29/2017
False
Result When
condition is false
if(condition)
{
//Statements if condition is true
}
else
{
//Statements if condition is false
}
3
if - else construct . . .
• The if-else construct executes some operation based on successful
completion of a condition, that returns only Boolean values. If the
condition is not satisfied, then the set of instructions under ‘else’ are
executed.
if(condition)
{
//Statements if condition is true
Begin
If
(condition)
}
else
{
//Statements if condition is false
Begin
}
Begin
7/29/2017
4
Example: if-else . . .
class Constructs
{
public static void main(String arg[])
{
int a=10;
if(a<10)
{
System.out.println(“a is greater than 10”);
}
else
{
System.out.println(“a is less than 10”);
}
}
}
02/10/10
5
Ternary (conditional) operator . . .
The ternary operator is considered to be an alternative to the ifelse construct.
test ? Pass : Fail ;
• test – the condition to be tested
• Pass - value given here is returned when the test is satisfied
• Fail – value given here is returned when the test is failed.
7/29/2017
6
Example: ternary operator . . .
class Constructs
{
public static void main(String arg[])
{
int avg=75;
char result;
result=avg>50 ? ‘A’ : ‘F’
System.out.println(result);
}
}
02/10/10
7
Nested if - else construct . . .
.dfssdf dsdfsdf
Begin
If
(condition)
Begin
if(condition)
{
//Statements if condition is true
}
else
{
//Statements if condition is false
}
Begin
7/29/2017
8
Example: Nested if-else . . .
Example 2 (cont): Store information of students
Modify your program, so that it is able to find the grade of each
student based on the average. You must use following criteria to
obtain the grades.
7/29/2017
Range of Average
85 to 100
75 to 84
65 to 74
55 to 64
45 to 54
<45
Grade
A
B
C
S
w
F
9
While loop . . .
The while loop executes some operation repeatedly, until the
condition returns false.
while(condition)
{
//body of the loop
}
When the condition fails, then the loop terminates.
02/10/10
10
Example: While loop . . .
Write a JAVA program to display consecutive numbers from 1 to 10,
using while loops.
class Constructs
{
public static void main(String arg[])
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
02/10/10
11
Do-While loop . . .
The do-while loop is similar to the while loop except the condition
to be evaluated is given at the end.
do
{
//body of the loop
} while(condition);
The loop is executed at least once even when the condition is
false.
02/10/10
12
Example: Do-While loop . . .
Write a JAVA program to display consecutive numbers from 1 to 10,
using do-while loops.
class Constructs
{
public static void main(String arg[])
{
int i=1;
do
{
System.out.println(i);
i++;
} while(i<=10) ;
}
}
02/10/10
13
For loop . . .
A for loop repeats a set of statements, until a condition is satisfied.
There are three major parts in a for loop namely; initialization, test
(condition) and the expression.
for(initialization; test; expression)
{
//Body of the for loop
}
• Initialization- A variable is initialized to a value.
• Test – This is the condition of the loop and it returns a Boolean
value.
• Expression – The expression is evaluated every time that the loop
14
is 02/10/10
executed.
Example: For loop . . .
Write a JAVA program to display consecutive numbers from 1 to 10,
using for loops.
class Constructs
{
public static void main(String arg[])
{
for(int i=0;i<10;i++)
{
System.out.println(i+1);
}
}
}
02/10/10
15
Switch statement . . .
The switch statement dispatches different parts of the code based
on the value of a single variable or expression. The value of
expression is compared with each of the literal values in the case
statements. If they match, the corresponding instructions are
executed. Otherwise an optional default statement is executed.
switch(test)
{
case value1:
//Statement1
break;
…………………………………………………………………………………………………….
…………………………………………………………………………………………………….
default:
Test is a variable or an
expression that evaluates
to byte, char or int. It
cannot be float, double,
long, string or any other
object
//Statement
}
02/10/10
16
Example: Switch Statement . . .
Write a JAVA program to display the name of the month when the
number is given.
int num=2;
switch(num)
{
case 1:
System.out.println("January");
break;
case 2:
System.out.println(“February");
break;
………………………………………………………………………………………………………………………………………………………………….
………………………………………………………………………………………………………………………………………………………………….
default:
System.out.println("Invalid Number");
02/10/10
}
17
Break & Continue. . .
• The break keyword halts the execution of current loop and forces
control out of the loop.
• The continue keyword starts the next iteration of the loop rather
than halting the execution of entire loop .
for(int i=0;i<10;i++)
{
if(i==5)
continue;
for(int i=0;i<10;i++)
{
if(i==5)
break;
System.out.println(i);
}
System.out.println(i);
}
02/10/10
18
Exercises . . .
Write JAVA programs to display following outputs. You must use
programming constructs.
02/10/10
19
© Copyright 2026 Paperzz