-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops.cpp
126 lines (107 loc) · 3.75 KB
/
loops.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Loops can execute a block of code as long as a specified condition is reached.
// Loops are handy because they save time, reduce errors, and they make code more readable.
// while loop
// #include <iostream>
// using namespace std;
// int main() {
// int num;
// cout<<"Enter number: ";cin>>num;
// while (num<10)
// {
// cout<<"Given number are smaller than 10!"<<endl;
// num++;
// // code will stop when user giving number greater than 10
// // if user give number smaller than 10 then loop are work and it run (10-given number) times
// }
// }
// do while loop
// #include <iostream>
// using namespace std;
// int main() {
// // it's code represent where you want to start a number where you want end
// int num;
// cout<<"what is your choice: /n 1. Number 2. Alphabate /n";
// cout<<"select your choice: ";cin>>num;
// // it's code represent where you want to start a alphabate where you want end
// if (num == 1)
// {
// int where_number;
// cout<<"Enter Where number will start: ";cin>>where_number;
// int here_number;
// cout<<"Enter number where you want to end: ";cin>>here_number;
// do
// {
// cout<< where_number <<endl;
// where_number++;
// } while (where_number <= here_number);
// }
// // char where_number; // here we are using char data type beacause we want to print only charcter!
// else if(num == 2)
// {
// char where_alphabate;
// cout<<"Enter alphabate from where you start: ";cin>>where_alphabate;
// char here_alphabte;
// cout<<"Enter alphabate from where you want to end: ";cin>>here_alphabte;
// do
// {
// cout<< where_alphabate <<endl;
// where_alphabate++;
// } while (where_alphabate <= here_alphabte);
// }
// // if condition or cghoice are not valid then else satemwnt will assicute
// else
// {
// cout<<"Enter valid choice!";
// }
// }
// for loops
// When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
// time conducting while loop is more than compare to for loop
// #include <iostream>
// using namespace std;
// int main() {
// int num;
// cout << "Select your choice: \n 1. numerical 2. alphabte"<<endl;
// cout<<"Enter your choice: ";cin>>num;
// if (num == 1)
// {
// int where_number;
// cout<<"Enter number where you want to start: ";cin>>where_number;
// int here_number;
// cout<<"Enter number where you want to end: ";cin>>here_number;
// for (int i = where_number; i <= here_number; i++)
// {
// cout<<i<<endl;
// }
// } else if (num == 2)
// {
// int where_alphabhate;
// cout<<"Enter alphabte where you want to strat: ";cin>>where_alphabhate;
// int here_alphabte;
// cout<<"Enter Number where you want to end: ";cin>>here_alphabte;
// for (int j = where_alphabhate; j <= here_alphabte; j++)
// {
// cout<<j<<endl;
// }
// } else
// {
// cout<<"Enter valid choice!";
// }
// }
// continue or brake statement
// if contion is true or loop whill end then we can use break statemnt for no ferther update
#include <iostream>
using namespace std;
int main() {
int i;
cout<<"Enter a number: ";cin>>i;
while (i<=10)
{
if (i == 4)
{
break; // brake statemnet are use to jump out from loops
}
cout<<i<<endl;
i ++;
}
}