Fixed several typos. (#21699)
This commit is contained in:
@ -6,7 +6,7 @@ title: Loops
|
|||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
Now lets discuss something known as loop. Suppose you want to print the even numbers from 1 to 1000 on the screen. One way
|
Now let's discuss something known as loop. Suppose you want to print the even numbers from 1 to 1000 on the screen. One way
|
||||||
to do this is to write the following lines
|
to do this is to write the following lines
|
||||||
|
|
||||||
``` c++
|
``` c++
|
||||||
@ -50,7 +50,7 @@ for ( initialize a variable; check a condition; increment the initialized variab
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Lets write a program which will print numbers from 0 to 1000 including 1000 on the screen using a for loop.
|
Let's write a program which will print numbers from 0 to 1000 including 1000 on the screen using a for loop.
|
||||||
|
|
||||||
``` c++
|
``` c++
|
||||||
for (int i = 0;i<=1000;i++)
|
for (int i = 0;i<=1000;i++)
|
||||||
@ -60,13 +60,13 @@ for (int i = 0;i<=1000;i++)
|
|||||||
```
|
```
|
||||||
|
|
||||||
When you execute this code in a c++ program numbers from 1 to 1000 will be printed.
|
When you execute this code in a c++ program numbers from 1 to 1000 will be printed.
|
||||||
Now lets discuss how the for loop works.
|
Now let's discuss how the for loop works.
|
||||||
|
|
||||||
* You start a for loop by typing the keyword 'for'. It means you are starting a for loop
|
* You start a for loop by typing the keyword 'for'. It means you are starting a for loop
|
||||||
` for `
|
` for `
|
||||||
* Next you open and close a round bracket. In this brackets you write some conditions which will be discussed later
|
* Next you open and close a round bracket. In this brackets you write some conditions which will be discussed later
|
||||||
` for()`
|
` for()`
|
||||||
* Inside the brackets first you write the initial condition i.e the value from where the loop will start. Like in the
|
* Inside the brackets first you write the initial condition i.e. the value from where the loop will start. Like in the
|
||||||
above program we write int i = 0
|
above program we write int i = 0
|
||||||
` for(int i = 0)`
|
` for(int i = 0)`
|
||||||
* Then you write the semicolon and then condition until when the loop will be executed. In the above code you define
|
* Then you write the semicolon and then condition until when the loop will be executed. In the above code you define
|
||||||
@ -96,7 +96,7 @@ for (int i = 0;i<=1000;i=i+2)
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
* The difference in first program and second is the increment part. Rest of code is same. This program will print 0 and
|
* The difference between the first program and second is the increment part. The rest of the code is the same. This program will print 0 and
|
||||||
then add 2 to it and print 2 on console and so on upto value of i becomes equal to 1000.
|
then add 2 to it and print 2 on console and so on upto value of i becomes equal to 1000.
|
||||||
|
|
||||||
Our final program to print even numbers from 0 to 1000 will look like this.
|
Our final program to print even numbers from 0 to 1000 will look like this.
|
||||||
|
Reference in New Issue
Block a user