Professional Documents
Culture Documents
Lecture 8
by
Imran Kazmi
> In the previous lecture we have been working
on “ for” loop
> In this lecture we will work on:
import java.io.*;
public class factorial
{
public static void main(String[] args)throws Exception
{
int n,f,x;
BufferedReader str=new BufferedReader(new InputStreamReader(System.in));
String s;
System.out.println("enter the value of n ");
s =str.readLine();
n=Integer.parseInt(s);
f=1;
for(x=n; x>=1; x--)
{
f=f*x;
}
System.out.println("factorial of n="+f);
}
}
Write down the program to print the table of any number N using “ for” loop
using JAVA
import java.io.*;
public class factorial
{
public static void main(String[] args)throws Exception
{
int n,t,x;
BufferedReader str=new BufferedReader(new InputStreamReader(System.in));
String s;
System.out.println("enter the value of n ");
s =str.readLine();
n=Integer.parseInt(s);
do
{
statements ;
}
while ( condition ) ;
“do-while” loop
Note:
> Do while loop used rarely when we want to execute
a loop at least once.
Nested Loop
ii) 1 2 3 4 5
1234
123
12
1
iii) 5 4 3 2 1
4321
321
21
1
iii) 54321
4321
321
21
1
• Code for the above mentioned output
Note: please produce code for above given (i) & (ii)
“ break ” statement
> Sometimes it becomes necessary to come out
of the loop even before loop condition becomes
false then break statement is used. Break statement
is used inside loop and switch statements.