C++

  1. For loop program
    • for (variable initialization; condition; variable update) {
    • Code to execute while the condition is true
    • }
  2. For: variable initialization
    allows you to either declare and give a value to an already existing variable

    int X=0
  3. For: Condition
    • tells the program that while the conditional expression is true the loop should continue to repeat itself
    • x<10
  4. For: variable update
    • easiest way for a loop to handle changing of the variable
    • x++
  5. For: program
    #include <iostream>

    • using namespace std;
    • cout and endl

    • int main()
    • {
    • (int x=0; x<10; x++) {
    • cout<< x << endl;
    • }
    • cin.get();
    • }
  6. while (condition) { code to execute while the condition is true}
    • true- a boolean expression which could be x==1 or while (x! + 7)
    • (x does not equal 7)

    empty condition is not legal for a while loop as it is for a for loop

    a while loop is the same for loop w/o the intialization and update section
  7. while program
    # include <iostream>

    using namespace std;

    • int main()
    • {
    • int x=0;

    • while (x<10)
    • cout << x << endl;
    • x++;
    • }
    • cin.get();
    • }
  8. do...while : useful for things that want to loop atleast once

    do {
    } while (conditional);

    while loop vs. do..while loop
    If the condition is true we jump back to the beginning of the block and execute it again.

    do...loop is basically a reversed while loop

    do...while loop- "execute this block of code, and loop while the condition is true."

    • vs.
    • while loop- "loop while the conditon is true and execute this block of code"
  9. do...while: program
    #include <iostrea>

    using namespace std;

    • int main()
    • {
    • int x;

    • x=0;
    • do {
    • cout<< "hello, world!\n";
    • } while (x! +0);
    • cin.get();
    • }
Author
phutomike
ID
22631
Card Set
C++
Description
loops
Updated