fix(guide) Replace invalid prism code block names (#35961)
* fix: replace sh with shell fix replace terminal with shell fix replace node with js fix replace output with shell fix replace cs with csharp fix replace c++ with cpp fix replace c# with csharp fix replace javasctipt with js fix replace syntax with js fix replace unix with shell fix replace linux with shell fix replace java 8 with java fix replace swift4 with swift fix replace react.js with jsx fix replace javascriot with js fix replace javacsript with js fix replace c++ - with cpp fix: corrected various typos fix: replace Algorithm with nothing fix: replace xaml with xml fix: replace solidity with nothing fix: replace c++ with cpp fix: replace txt with shell fix: replace code with json and css fix: replace console with shell
This commit is contained in:
committed by
Oliver Eyton-Williams
parent
4b4762716c
commit
0a1eeea424
@@ -15,7 +15,7 @@ For loop is an entry controlled loop unlike do-while loop.
|
||||
|
||||
## Syntax
|
||||
|
||||
```c++
|
||||
```cpp
|
||||
for (init; condition; increment ) {
|
||||
update_statement(s);
|
||||
}
|
||||
@@ -23,7 +23,7 @@ for (init; condition; increment ) {
|
||||
|
||||
The increment can also placed inside the for loop i.e. in its body-
|
||||
|
||||
```c++
|
||||
```cpp
|
||||
for ( init; condition;) {
|
||||
update_statement(s);
|
||||
increment;
|
||||
@@ -32,7 +32,7 @@ for ( init; condition;) {
|
||||
|
||||
It is also allowed to ignore the init variables if and only if they are declared beforehand. For example :
|
||||
|
||||
```c++
|
||||
```cpp
|
||||
int a = 1;
|
||||
for (; a <= 10 ;) {
|
||||
cout << a << '\n';
|
||||
@@ -58,7 +58,7 @@ The update statement is used to alter the loop variable by using simple operatio
|
||||
You will often see an increment operation as the update statement (e.g. i++, count++). This is often seen as one of the distinguishing features and possible name sources for the C++ language.
|
||||
|
||||
## Implementation
|
||||
```c++
|
||||
```cpp
|
||||
#include <iostream>
|
||||
using std::cout; // Here we use the scope resolution operator to define the scope of the standard functions as std
|
||||
using std::endl;
|
||||
@@ -74,7 +74,7 @@ int main () {
|
||||
```
|
||||
|
||||
Output:
|
||||
```output
|
||||
```shell
|
||||
value of a: 10
|
||||
value of a: 11
|
||||
value of a: 12
|
||||
@@ -91,7 +91,7 @@ value of a: 19
|
||||
The body of the for loop need not be enclosed in braces if the loop iterates over only one statement.
|
||||
|
||||
### Example
|
||||
```c++
|
||||
```cpp
|
||||
#include<iostream.h>
|
||||
using std::cout;
|
||||
|
||||
@@ -107,7 +107,7 @@ int main () {
|
||||
This would generate the same output as the previous program.
|
||||
|
||||
Output:
|
||||
```output
|
||||
```shell
|
||||
value of a: 10
|
||||
value of a: 11
|
||||
value of a: 12
|
||||
@@ -128,13 +128,13 @@ C++ also has what we call "range-based" `for` loops which iterate through all th
|
||||
|
||||
### Syntax
|
||||
|
||||
```c++
|
||||
```cpp
|
||||
for ( element: container ) {
|
||||
statement(s);
|
||||
}
|
||||
```
|
||||
|
||||
```c++
|
||||
```cpp
|
||||
int[5] array = { 1, 2, 3, 4, 5 }
|
||||
for ( int i: array ) {
|
||||
cout << i << endl;
|
||||
@@ -142,7 +142,7 @@ for ( int i: array ) {
|
||||
```
|
||||
|
||||
Output:
|
||||
```output
|
||||
```shell
|
||||
1
|
||||
2
|
||||
3
|
||||
@@ -154,7 +154,7 @@ Output:
|
||||
Iterator based for loops are also possible in C++ and functionality for them exists in many of the data structures found within the STL. Unlike for-each loops, iterator based loops allow for mutating the contents of the container during iteration. This is rather useful when one needs to remove or insert values while looping over data.
|
||||
|
||||
### Syntax
|
||||
```c++
|
||||
```cpp
|
||||
// Create a vector
|
||||
std::vector<int> vec;
|
||||
|
||||
@@ -176,7 +176,7 @@ for(std::vector<string>::iterator it = vec.begin(); it != vec.end(); it++) {
|
||||
### Use as infinite loops
|
||||
|
||||
This C-style for-loop is commonly the source of an infinite loop since the fundamental steps of iteration are completely in the control of the programmer. In fact, when infinite loops are intended, this type of for-loop can be used (with empty expressions), such as:
|
||||
```c++
|
||||
```cpp
|
||||
for (;;) {
|
||||
//loop body
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ Traversal in a list is slow as compared to Vectors and Arrays, but once a positi
|
||||
## How to declare a List
|
||||
|
||||
Possible declarations of a list:
|
||||
```c++
|
||||
```cpp
|
||||
#include <list>
|
||||
|
||||
int main()
|
||||
|
@@ -9,7 +9,7 @@ title: Loops
|
||||
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
|
||||
|
||||
``` c++
|
||||
```cpp
|
||||
cout << 0 << endl;
|
||||
cout << 2 << endl;
|
||||
cout << 4 << endl;
|
||||
@@ -30,7 +30,7 @@ While and do while loops allow you to run the loop until a condition finishes.
|
||||
The difference between While and Do while is that Do while loop always executes at least once.
|
||||
The very use of Do while loop can be seen in the scenarios when the number of times that the loop will run depends upon the first iteration of the loop.
|
||||
Here you can see an example:
|
||||
``` c++
|
||||
```cpp
|
||||
while (condition){
|
||||
// Code that will execute while condition is true
|
||||
}
|
||||
@@ -44,7 +44,7 @@ For loops are usually used when you know how many times the code will execute.
|
||||
The flow can be seen in this [graph](https://www.tutorialspoint.com/cplusplus/images/cpp_for_loop.jpg).
|
||||
|
||||
They are declared this way:
|
||||
``` c++
|
||||
```cpp
|
||||
for ( initialize a variable; check a condition; increment the initialized variable ) {
|
||||
//Code to execute
|
||||
}
|
||||
@@ -52,7 +52,7 @@ for ( initialize a variable; check a condition; increment the initialized variab
|
||||
|
||||
Let's write a program which will print numbers from 0 to 1000 including 1000 on the screen using a for loop.
|
||||
|
||||
``` c++
|
||||
```cpp
|
||||
for (int i = 0;i<=1000;i++)
|
||||
{
|
||||
cout << i << endl;
|
||||
@@ -77,7 +77,7 @@ Now let's discuss how the for loop works.
|
||||
` for(int i=0;i<=1000;i++) `
|
||||
* If there is only one statement inside the loop then the curly bracket is optional but its better to write loop code
|
||||
within brackets so that you don't get confused.
|
||||
``` c++
|
||||
```cpp
|
||||
for(int i=0;i<=1000;i++)
|
||||
{
|
||||
}
|
||||
@@ -88,7 +88,7 @@ Now let's discuss how the for loop works.
|
||||
|
||||
If you want to print even numbers from 1 to 1000 then your program will look like this:
|
||||
|
||||
``` c++
|
||||
```cpp
|
||||
for (int i = 0;i<=1000;i=i+2)
|
||||
{
|
||||
cout << i << endl;
|
||||
@@ -100,7 +100,7 @@ for (int i = 0;i<=1000;i=i+2)
|
||||
|
||||
Our next program to print even numbers from 0 to 1000 will look like this:
|
||||
|
||||
``` c++
|
||||
```cpp
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
@@ -114,7 +114,7 @@ int main()
|
||||
```
|
||||
Another type of for loop is the [Range-based for loop](https://en.cppreference.com/w/cpp/language/range-for).
|
||||
|
||||
``` c++
|
||||
```cpp
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
|
@@ -146,7 +146,8 @@ int main ()
|
||||
Returns whether the `queue` is empty ,i.e. whether your queue size is zero.
|
||||
It returns `true` if queue's size 0 else returns `false`
|
||||
|
||||
```cpp//Empty operation in Queue
|
||||
```cpp
|
||||
//Empty operation in Queue
|
||||
#include <iostream> // std::cout
|
||||
#include <queue> // std::stack
|
||||
|
||||
|
@@ -39,7 +39,7 @@ Two case labels cannot have the same value.
|
||||
|
||||
|
||||
Example:
|
||||
```C++
|
||||
```cpp
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
@@ -5,7 +5,7 @@ title: While-loop
|
||||
A while loop statement repeatedly executes a target statement as long as a given condition is true. It is often used when the number of iterations is unknown.
|
||||
|
||||
Syntax:
|
||||
```C++
|
||||
```cpp
|
||||
while(condition) {
|
||||
statement(s);
|
||||
}
|
||||
@@ -18,7 +18,7 @@ Another important point about the while loop is to remember to increment/decreme
|
||||
|
||||
Example:
|
||||
|
||||
```C++
|
||||
```cpp
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
@@ -52,7 +52,7 @@ value of a: 19
|
||||
```
|
||||
|
||||
Example of Skipped Loop Body:
|
||||
```C++
|
||||
```cpp
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
|
Reference in New Issue
Block a user