// Native_For-Loop.cpp : Defines the entry point for the console application. /* _____________________________________________ Learning C++: Lesson 4 Counting Things with For Loops Ron Kessler Created 5/29/2016 Native Version This little program shows how to use for loops to repeat blocks of code when you know how many times you need to repeat. This is called a "counted loop". Run with CTL-F5 _____________________________________________ */ #include "stdafx.h" #include #include using namespace std; int main () { cout << "Let's square some numbers...." << endl; // 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 << " squared is " << x * x << endl; } cout << "\n\nPress 1 to see a loop that counts to 5."; string input; cin >> input; system ("cls"); cin.ignore (); //---make a loop that starts with 1 and runs 5 times for (int counter = 1; counter <= 5; counter++) //we use an int to manage the loop. We are incrementing by 1 { cout << counter << endl; //print out the value of our LCV each iteration } cout << "\n\nPress 1 to see a loop that counts by 2's."; cin >> input; system ("cls"); cin.ignore (); //---now count by 2's for (int counter = 0; counter <= 20; counter += 2) //we use an int to manage the loop. We are incrementing by 1 { cout << counter << endl; //print out the value of our LCV each iteration } cout << "\n\nNow let's count backwards from 30 by 5's." << endl; cin >> input; system ("cls"); cin.ignore (); //---now count backwards by 5's for (int counter = 30; counter >= 0; counter -= 5) //we use an int to manage the loop. We are incrementing by 1 { cout << counter << endl; //print out the value of our LCV each iteration } }