-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.cpp
More file actions
64 lines (53 loc) · 1.61 KB
/
Copy pathloops.cpp
File metadata and controls
64 lines (53 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
using namespace std;
/*Loops:
Loops are iterative statements . they are block of statements are repeatedly executed as long as condition is true.
condition must be false to come out of the loop.
Four types of loops:
• pre-tested loop while()
• post-tested loop do..while()
• counter controlled loop for()
• for each loop for Collections for()
*/
/*
loops usage :
1. While and doWhile are similar, they are used when we don’t know
number of times of repetitions
2. For is used when we know the number of iterations.
3. For each is used with array or STL. */
int main()
{
//while loop
int a=0;
while(a<10)
{
cout<<"a "<<a;//condition is checked and then process is done
a++;}
//do while loop
int b=0;
do
{
cout<<"b "<<b;
b++;
}while(b<10);//process is done and then condition is checked
//for loop
for(int i=0;i<10;i++)
{
cout<<"i "<<i;//for(initialization ,condition , updation) all the things related to counter are written in first parenthesis
}
//infinite loop
return 0;
}
/*
a program for counting factorial of a given number :
int fact (){
int n,i;
int fact=1;
cout<<"enter a number :"<<endl;
cin>>n;
for (i=n;i>0;i--){
fact=fact*i;
}
cout<<"factorial of the number is :"<<fact;
}
*/