0% found this document useful (0 votes)
65 views13 pages

Aramina de Ocampo It122 While Statement Example For While Statement

The document discusses different types of loops in C++ including for, while, and do-while loops. It provides examples of each loop type and explains their basic structures and uses. For loops iterate over a range of values using an initialization statement, condition check, and increment/decrement portion. While loops repeatedly execute while a condition is true. Do-while loops are similar but execute the block once before checking the condition. The break and continue statements can alter normal loop flow by skipping the rest of the current iteration or exiting the loop entirely.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views13 pages

Aramina de Ocampo It122 While Statement Example For While Statement

The document discusses different types of loops in C++ including for, while, and do-while loops. It provides examples of each loop type and explains their basic structures and uses. For loops iterate over a range of values using an initialization statement, condition check, and increment/decrement portion. While loops repeatedly execute while a condition is true. Do-while loops are similar but execute the block once before checking the condition. The break and continue statements can alter normal loop flow by skipping the rest of the current iteration or exiting the loop entirely.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

ARAMINA DE OCAMPO IT122 While statement example For while statement

#include <iostream> using namespace std; // So the program can see cout and endl int main() { // The loop goes while x < 10, and x increases by one every loop for ( int x = 0; x < 10; x++ ) { // Keep in mind that the loop condition checks // the conditional statement before it loops again. // consequently, when x equals 10 the loop breaks. // x is updated before the condition is checked. cout<< x <<endl; } cin.get(); This program is a very simple example of a for loop. x is set to zero, while x is less than 10 it calls cout<< x <<endl; and it adds 1 to x until the condition is met. Keep in mind also that the variable is incremented after the code in the loop is run for the first time.

WHILE - WHILE loops are very simple. The basic structure is


while ( condition ) { Code to execute while the condition is true } The true represents a boolean expression which could be x == 1 or while ( x != 7 ) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x ==5 || v == 7) which says execute the code while x equals five or while v equals 7. Notice that a while loop is the same as a for loop without the initialization and update sections. However, an empty condition is not legal for a while loop as it is with a for loop. #include <iostream> using namespace std; // So we can see cout and endl int main() { int x = 0; // Don't forget to declare variables while ( x < 10 ) { // While x is less than 10 cout<< x <<endl; x++; // Update x so the condition can be met eventually } cin.get(); } This was another simple example, but it is longer than the above FOR loop. The easiest way to think of the loop is that when it reaches the brace at the end it jumps back up to the beginning of the loop, which checks the condition again and decides whether to repeat the block another time, or stop and move to the next statement after the block.

DO..WHILE - DO..WHILE loops are useful for things that want to loop at least once.
The structure isdo { } while ( condition ); Notice that the condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. If the condition is true, we jump back to the beginning of the block and execute it again. A do..while loop is basically a reversed while loop. A while loop says "Loop while the condition is true, and execute this block of code", a do..while loop says "Execute this block of code, and loop while the condition is true". Example: #include <iostream> using namespace std; int main() { int x; x = 0; do { // "Hello, world!" is printed at least one time // even though the condition is false cout<<"Hello, world!\n"; } while ( x != 0 ); cin.get(); } Keep in mind that you must include a trailing semi-colon after the while in the above example. A common error is to forget that a do..while loop must be terminated with a semicolon (the other loops should not be terminated with a semicolon, adding to the confusion). Notice that this loop will execute once, because it automatically executes before checking the condition.

MEA JOAN VALIENTE IT122 While statement example For while statement
#include <iostream> using namespace std; // So the program can see cout and endl int main() { // The loop goes while x < 10, and x increases by one every loop for ( int x = 0; x < 10; x++ ) { // Keep in mind that the loop condition checks // the conditional statement before it loops again. // consequently, when x equals 10 the loop breaks. // x is updated before the condition is checked. cout<< x <<endl; } cin.get(); This program is a very simple example of a for loop. x is set to zero, while x is less than 10 it calls cout<< x <<endl; and it adds 1 to x until the condition is met. Keep in mind also that the variable is incremented after the code in the loop is run for the first time.

WHILE - WHILE loops are very simple. The basic structure is


while ( condition ) { Code to execute while the condition is true } The true represents a boolean expression which could be x == 1 or while ( x != 7 ) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x ==5 || v == 7) which says execute the code while x equals five or while v equals 7. Notice that a while loop is the same as a for loop without the initialization and update sections. However, an empty condition is not legal for a while loop as it is with a for loop. #include <iostream> using namespace std; // So we can see cout and endl int main() { int x = 0; // Don't forget to declare variables while ( x < 10 ) { // While x is less than 10 cout<< x <<endl; x++; // Update x so the condition can be met eventually } cin.get(); } This was another simple example, but it is longer than the above FOR loop. The easiest way to think of the loop is that when it reaches the brace at the end it jumps back up to the beginning of the loop, which checks the condition again and decides whether to repeat the block another

time, or stop and move to the next statement after the block.

DO..WHILE - DO..WHILE loops are useful for things that want to loop at least once.
The structure isdo { } while ( condition ); Notice that the condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. If the condition is true, we jump back to the beginning of the block and execute it again. A do..while loop is basically a reversed while loop. A while loop says "Loop while the condition is true, and execute this block of code", a do..while loop says "Execute this block of code, and loop while the condition is true". Example: #include <iostream> using namespace std; int main() { int x; x = 0; do { // "Hello, world!" is printed at least one time // even though the condition is false cout<<"Hello, world!\n"; } while ( x != 0 ); cin.get(); } Keep in mind that you must include a trailing semi-colon after the while in the above example. A common error is to forget that a do..while loop must be terminated with a semicolon (the other loops should not be terminated with a semicolon, adding to the confusion). Notice that this loop will execute once, because it automatically executes before checking the condition.

MARGARITA BONDOC IT122 C++ FOR LOOPS WHILE LOOPS


In this C++ programming tutorial we will look at loops. There are circumstances were you want to do the same thing many times. For instance you want to print the same words ten times. You could type ten cout statements, but it is easier to use a loop, such as a for loop or a while loop. The only thing you have to do is to setup a loop that execute the same cout statement ten times. There are three basic types of loops which are: for loop while loop do while loop

The for loop


The for loop loops from one number to another number and increases by a specified value each time. The for loop uses the following structure:

for (Start value; end condition; increase value) statement(s);

Look at the example below:

#include<iostream> using namespace std; int main() { int i; for (i = 0; i < 10; i++) { cout << "Hello" << "\n"; cout << "There" << "\n"; } return 0;

Note: A single instruction can be placed behind the for loop without the curly brackets. Lets look at the for loop from the example: We first start by setting the variable i to 0. This is where we start to count. Then we say that the for loop must run if the counter i is smaller then ten. Last we say that every cycle i must be increased by one (i++).

In the example we used i++ which is the same as using i = i + 1. This is called incrementing. The instruction i++ adds 1 to i. If you want to subtract 1 from i you can use i. It is also possible to use ++i or -i. The difference is is that with ++i the one is added before the for loop tests if i < 10. With i++ the one is added after the test i < 10.

The while loop


The while loop can be used if you dont know how many times a loop must run. Here is an example:

#include<iostream> using namespace std; int main() { int counter, howmuch; cin >> howmuch; counter = 0; while ( counter < howmuch) { counter++; cout << counter << '\n'; } return 0;

Lets take a look at the example: First you must always initialize the counter before the while loop starts ( counter = 1). Then the while loop will run if the variable counter is smaller then the variable howmuch. If the input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is to increment the counter inside the loop (counter++). If you forget this the loop becomes infinitive. The do while loop The do while loop is almost the same as the while loop. The do while loop has the following form:

do { do something; } while (expression);

Do something first and then test if we have to continue. The result is that the loop always runs once. (Because the expression test comes afterward). Take a look at an example:

#include <iostream> using namespace std;

int main() { int counter, howmuch; cin >> howmuch; counter = 0; do { counter++; cout << counter << '\n'; } while ( counter < howmuch); return 0;

Note: There is a semi-colon behind the while line. Break and continue To exit a loop you can use the break statement at any time. This can be very useful if you want to stop running a loop because a condition has been met other than the loop end condition. Take a look at the following example:

#include <iostream> using namespace std; int main() { int i; i = 0; while ( i < 20 ) { i++; cout << "Hello\n"; if ( i == 10) break; } return 0;

In the example above, the while loop will run, as long i is smaller then twenty. In the while loop there is an if statement that states that if i equals ten the while loop must stop (break). The result is that only ten Hello will be printed. With continue; it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented). Take a look at the example below:

#include <iostream> using namespace std;

int main() { int i; i = 0; while ( i < 20 ) { i++; continue; cout << "Hello\n"; if ( i == 10) break; } return 0; }

In the example above, the cout function is never called because of the continue;. The exit function The exit function is defined in the cstdlib library. The purpose of exit is to terminate the current program with a specific exit code. Its prototype is:

void exit (int exitcode);

The exitcode is used by some operating systems (UNIX / Linux) and may be used by calling programs. An exit code of 0 means that the program finished normally and any other value means that some error occurred. That was all for now. Dont forget to make some example programs of your own, just for practice.

MARY JOYCE TOLENTINO IT122

C++ FOR LOOPS WHILE LOOPS

In this C++ programming tutorial we will look at loops. There are circumstances were you want to do the same thing many times. For instance you want to print the same words ten times. You could type ten cout statements, but it is easier to use a loop, such as a for loop or a while loop. The only thing you have to do is to setup a loop that execute the same cout statement ten times. There are three basic types of loops which are: for loop while loop do while loop

The for loop


The for loop loops from one number to another number and increases by a specified value each time. The for loop uses the following structure:

for (Start value; end condition; increase value) statement(s);

Look at the example below:

#include<iostream> using namespace std; int main() { int i; for (i = 0; i < 10; i++) { cout << "Hello" << "\n"; cout << "There" << "\n"; } return 0; }

Note: A single instruction can be placed behind the for loop without the curly brackets.

Lets look at the for loop from the example: We first start by setting the variable i to 0. This is where we start to count. Then we say that the for loop must run if the counter i is smaller then ten. Last we say that every cycle i must be increased by one (i++). In the example we used i++ which is the same as using i = i + 1. This is called incrementing. The instruction i++ adds 1 to i. If you want to subtract 1 from i you can use i. It is also possible to use ++i or -i. The difference is is that with ++i the one is added before the for loop tests if i < 10. With i++ the one is added after the test i < 10.

The while loop


The while loop can be used if you dont know how many times a loop must run. Here is an example:

#include<iostream> using namespace std; int main() { int counter, howmuch; cin >> howmuch; counter = 0; while ( counter < howmuch) { counter++; cout << counter << '\n'; } return 0; }

Lets take a look at the example: First you must always initialize the counter before the while loop starts ( counter = 1). Then the while loop will run if the variable counter is smaller then the variable howmuch. If the input is ten, then 1 through 10 will be printed on the screen. A last thing you have to remember is to increment the counter inside the loop (counter++). If you forget this the loop becomes infinitive. The do while loop The do while loop is almost the same as the while loop. The do while loop has the following form:

do {

do something; } while (expression);

Do something first and then test if we have to continue. The result is that the loop always runs once. (Because the expression test comes afterward). Take a look at an example:

#include <iostream> using namespace std; int main() { int counter, howmuch; cin >> howmuch; counter = 0; do { counter++; cout << counter << '\n'; } while ( counter < howmuch); return 0;

Note: There is a semi-colon behind the while line. Break and continue To exit a loop you can use the break statement at any time. This can be very useful if you want to stop running a loop because a condition has been met other than the loop end condition. Take a look at the following example:

#include <iostream> using namespace std; int main() { int i; i = 0; while ( i < 20 ) { i++; cout << "Hello\n"; if ( i == 10) break; } return 0;

In the example above, the while loop will run, as long i is smaller then twenty. In the while loop there is an if statement that states that if i equals ten the while loop must stop (break). The result is that only ten Hello will be printed. With continue; it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented). Take a look at the example below:

#include <iostream> using namespace std; int main() { int i; i = 0; while ( i < 20 ) { i++; continue; cout << "Hello\n"; if ( i == 10) break; } return 0; }

In the example above, the cout function is never called because of the continue;. The exit function The exit function is defined in the cstdlib library. The purpose of exit is to terminate the current program with a specific exit code. Its prototype is:

void exit (int exitcode);

The exitcode is used by some operating systems (UNIX / Linux) and may be used by calling programs. An exit code of 0 means that the program finished normally and any other value means that some error occurred. That was all for now. Dont forget to make some example programs of your own, just for practice.

You might also like