You are on page 1of 17

Programming Fundamentals

Lecture 8
by
Imran Kazmi
> In the previous lecture we have been working
on “ for” loop
> In this lecture we will work on:

1) Programming Practice using “ for” loop


2) Do- while loop
3) Difference of do while loop with “ for” and “while”
loop
4) Programming Practice using “do-while” loop
5) Nested Loop
Write down the program to calculate the factorial of any number
N, using “ for” loop.
Write down the program to print the table of any number N
using “ for” loop
Write down the program to calculate the factorial 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,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);

for(x=1; x<=10; x++)


{
t=x*n;
System.out.println(n+"*"+x+"="+t);
}
}
}
“ do-while ” loop
Do while loop execute one or more times

Syntax of do-while loop

do
{
statements ;
}
while ( condition ) ;
“do-while” loop

Here firstly statement inside body is executed


then condition is checked. If the condition is true
again body of loop is executed and this process
continue until the condition becomes false.
Unlike while loop semicolon is placed at the end
of while
Difference between “ while and do-while loop”

There is minor difference between while and do


while loop, while loop test the condition before
executing any of the statement of loop. Whereas
do while loop test condition after having
executed the statement at least one within the
loop.
Difference between “ while and do-while loop”

 If initial condition is false while loop would not


executed it’s statement on other hand do while loop
execute it’s statement at least once even If condition
fails for first time.
 It means do while loop always executes at least once.

Note:
> Do while loop used rarely when we want to execute
a loop at least once.
Nested Loop

When a loop written inside the body of


another loop then, it is known as nesting of
loop. Any type of loop can be nested in any type
such as while, do while, for.
Nesting Loops
for( i=1; i<=3; i++)
{
for( j=1; j<=4; j++)
{
cout<<“ Pakistan”<<endl;
}
}
Write down the code that generate the
following output using nested loop
i) 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

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

for( i=5 ; i>=1 ; i--)


{
for(j=i ; j>=1 ; j--)
{
cout<<j;
}
cout<<endl;
}

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.

> It cause immediate exit from that loop in which


it appears and it is generally written with condition.
It is written with the keyword as break .
break statement
> When break statement is encountered loop
is terminated and control is transferred to the
statement, immediately after loop or situation
where we want to jump out of the loop instantly
without waiting to get back to conditional state.
> When break is encountered inside any loop,
control automatically passes to the first
statement after the loop. This break statement is
usually associated with “ if ” statement.
Thanks

You might also like