Professional Documents
Culture Documents
Programming Fundamentals: by Imran Kazmi
Programming Fundamentals: by Imran Kazmi
Lecture 7
By
Imran Kazmi
> In the previous lecture , we have started loop control
instructions and started working on while loop .
i) “ while ” loop
ii) Programming practice
iii) “ for ” loop
iv) Programming Practice
v) Introduction of “ do while “ loop
While Loop
• Parts of while loop:
1) Initial value
2) Test Condition
3) Increment / Decrement
(Change in value of the variable, testing the
condition)
Loop flow chart
start
Initialize
False
test
True
stop
Body of loop
Increment/decrement
Initialization;
increment/decrement;
}
Programming Practice
• Write Down the program that generate even numbers from 0
to 50.
• Write down the program the generate the table of any number
N
• i) ++ increment operator
• ii) -- decrement operator
e.g
(Java)
import java.io.*;
public class loopwhile
{
public static void main(String[] args)throws Exception
{
int n,t,x; output
BufferedReader str=new BufferedReader(new InputStreamReader(System.in));
String s;
enter the value
System.out.println("enter the value of n"); of n
s =str.readLine(); 2
n=Integer.parseInt(s); 2*1=2
x=1; 2*2=4
while(x<=10) 2*3=6
{ 2*4=8
t=n*x; 2*5=10
System.out.println(n+"*"+x+"="+t);
2*6=12
x=x+1;
2*7=14
}
}
2*8=16
} 2*9=18
2*10=20
Write down the program that calculate the factorial of any number N ( in Java)
import java.io.*;
public class factorial
{
public static void main(String[] args)throws Exception
{
int n, f;
BufferedReader str=new BufferedReader(new InputStreamReader(System.in));
String s;
System.out.println("enter the value of n ");
s =str.readLine();
output
n=Integer.parseInt(s);
f=1;
while(n>=1)
{ enter the value of n
f=f*n ; 5
n-- ;
} factorial of n=120
System.out.println("factorial of n="+f);
}
Arithmetic Assignment Operators
i) += counter += 3 ; same as
counter = counter + 3 ;
statement 1 ;
statement 2 ;
statement 3 ;
}
Example
int counter ;
for( counter = 0 ; counter < 10 ; counter = counter + 1 )
{
cout << counter;
}
Output
0123456789
Assignment
All the program we have done through “while” loop, please develop them
using “ for “ loop.
Thank you