Professional Documents
Culture Documents
1. Recap of Session-3
2. QUIZ-1 Discussion
3. Control Statements
4. Program Demonstration on Control Statements
Session -4 Assignments
1. Generate a total electricity bill paid by the customer based on the units
consumed. (Verify Your Home Bill).
2. List the Amstrong numbers from m to n.
3. Program to Generate sum of the serirs
1+x+x2+x3+……. Upto n terms.
4. Use the nested loop to display a series
1
2 3
4 5 6
7 8 9 10
1. Control Statements
1. Conditional / Selection Statements
if(condition)
{
//code to be executed
}
o if-else statement (two way-selection)
if(condition)
{
//code to be executed
}
else
{
//code to be executed
}
o nested if statement ( if inside another if)
if(condition1)
{
if(condition2)
{
//code to be executed
}
//code to be executed
}
o if-else-if ladder (Multi way selection)
if(condition1)
{
//code to be executed
}
else if(condition2)
{
//code to be executed
}
-----------------------
else
{
//code to be executed
}
switch(expression)
{
case value1: //code to be executed;
break; //optional
case value2: //code to be executed;
break; //optional
......
default:
// code to be executed if all cases are not matched;
}
{
public static void main(String[] args)
{
int number=20;
switch(number)
{
case 10: System.out.println("10");
case 20: System.out.println("20");
case 30: System.out.println("30");
default:System.out.println("Hello");
}
}
}
2. Loop / Repetitive / Iterative Statements
Java while Loop
The Java while loop is used to iterate a part of the program several
times. If the number of iteration is not fixed, it is recommended to use
while loop.
Syntax:
while(condition)
{
//code to be executed
}
If you pass true in the while loop, it will be infinitive while loop.
while(true)
{
System.out.println("never ends");
}
The Java do-while loop is used to iterate a part of the program several
times. If the number of iteration is not fixed and you must have to
execute the loop at least once, it is recommended to use do-while
loop.
Syntax:
do
{
//code to be executed
}while(condition);
If you pass true in the do-while loop, it will be infinitive do-while loop.
do{
System.out.println("Never Ends");
}while(true);
The Java for loop is used to iterate a part of the program several
times. If the number of iteration is fixed, it is recommended to use for
loop.
The simple for loop is same as C/C++. We can initialize variable, check
condition and increment/decrement value.
Syntax: for(initialization;condition;incr/decr)
{
//code to be executed
}
Example: for(int i=1;i<=10;i++)
{
System.out.println(i);
}
Syntax:
for( ; ; )
{
System.out.println("Never Ends");
}
3. Jump Statements
for(int i=1;i<=10;i++)
{
if(i==5)
{
break;
}
}
System.out.println(i);