0% found this document useful (0 votes)
61 views19 pages

Programming Fundamentals: by Imran Kazmi

The document discusses different loop structures in programming. It covers while loops, for loops, and do-while loops. Examples are provided to illustrate generating even numbers from 0 to 50, a series from 1 to N, calculating a factorial, and generating a number table using while and for loops. Operators like increment/decrement, arithmetic assignment are also explained. The key aspects covered are the parts of a loop, flowchart, initialization, test condition, incrementing/decrementing, and using for loops as an alternative to while loops for the examples provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views19 pages

Programming Fundamentals: by Imran Kazmi

The document discusses different loop structures in programming. It covers while loops, for loops, and do-while loops. Examples are provided to illustrate generating even numbers from 0 to 50, a series from 1 to N, calculating a factorial, and generating a number table using while and for loops. Operators like increment/decrement, arithmetic assignment are also explained. The key aspects covered are the parts of a loop, flowchart, initialization, test condition, incrementing/decrementing, and using for loops as an alternative to while loops for the examples provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Programming Fundamentals

Lecture 7
By
Imran Kazmi
> In the previous lecture , we have started loop control
instructions and started working on while loop .

> In this lecture we will cover

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;

while ( Test condition )


{
statement1;
statement 2;
statement 3 ;

increment/decrement;
}
Programming Practice
• Write Down the program that generate even numbers from 0
to 50.

• Write down the program that generate the following series


1 2 4 5 7 8 10 11 13……….N

• Write down the program the generate the table of any number
N

• Write down the program that calculate the factorial of any


number N
Write Down the program that generate even numbers from 0 to 50.
Write down the program that generate the following series
1 2 4 5 7 8 10 11 13……….N
Write down the program the generate the table of any number N
output
Write down the program that calculate the factorial of any number N
output
Increment/decrement operator

• i) ++ increment operator
• ii) -- decrement operator

e.g

n++; Same As n=n+1


n--; Same As n=n-1
Write down the program the generate the table of any number N

(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 ;

ii) -= counter -= 5 ; same as


counter = counter – 5 ;

iii) *= x*=2 ; same as x = x * 2;

iv) /= x /= 2; same as x=x/2;

V) %= x %=2; same as x=x % 2;


For loop

for ( initialization condition ; termination condition ; increment condition )


{

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

You might also like