These are notes for 30 October.
#include <iostream>
using namespace std;
int main()
{
int limit, skip=1;
cout << "Enter limit: ";
cin >> limit;
for(int i = 1; i <= limit; i += skip, skip++)
{
cout << i << "\n";
}
// Below is the same, but expressed as a while loop
/* int i = 1;
skip = 1;
while(i <= limit)
{
cout << i << "\n";
i += skip;
skip++;
}
*/
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int height;
cout << "Enter height: ";
cin >> height;
int spaces = height - 1;
int dots = -1;
for(int row = 0; row < height;
row++, spaces--, dots += 2)
{
// indent by #spaces
for(int i = 0; i < spaces; i++)
{
cout << " ";
}
if(row > 0)
{
cout << "o";
// print #dots
for(int i = 0; i < dots; i++)
{
if(row == height-1) // last row
cout << "o";
else
cout << ".";
}
}
cout << "o\n";
}
return 0;
}
// Introducing "break" in loops
#include <iostream>
using namespace std;
int main()
{
for(int i = 1; i <= 100; i++)
{
if(i % 5 == 0)
{
cout << i << "\n";
}
if(i % 15 == 0)
{
cout << "blah\n";
break;
}
}
return 0;
}
// Introducing "break" in loops
#include <iostream>
using namespace std;
int main()
{
int i = 5;
while(1) // infinite loop!
{
cout << i << "\n";
i++;
if(i > 8) break; // NOT infinite!
}
int year;
while(1)
{
cout << "Enter year (1900-): ";
cin >> year;
if(year >= 1900) break;
cout << "ERROR\n";
}
return 0;
}
// Characters and strings
#include <iostream>
using namespace std;
int main()
{
char initial = 'A';
cout << initial << "\n";
initial++;
cout << initial << "\n";
initial += 32;
cout << initial << "\n";
for(char c = ' '; c <= 'E'; c++)
{
cout << c;
}
cout << "\n";
// strings are sequences of characters
string me = "Chris";
cout << me << "\n";
if(me == "chris") // false because case-sensitive
{
}
cout << me[0] << "\n";
me = "This is a test of my silly string program.";
cout << me.size() << "\n";
for(int i = 0; i < me.size(); i += 2)
{
cout << me[i] << " ";
}
cout << "\n";
return 0;
}