Introduction To Programming: Author of The Course: Associate Professor, Candidate of Technical Science Pachshenko G.N
Introduction To Programming: Author of The Course: Associate Professor, Candidate of Technical Science Pachshenko G.N
PROGRAMMING
Lecture 3
Author of the course:
associate professor,
candidate of technical science
Pachshenko G.N.
[email protected]
Topics
• Syntax:
#include <iostream>
using namespace std;
int main ()
{ int n = 10;
while (n>0)
{
cout << n << ", ";
--n; }
cout << "liftoff!\n";}
The while loop
• Result:
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!
The while loop
• The loop should end at some point, and thus the statement
shall alter values checked in the condition in some way, so as
to force it to become false at some point.
#include <iostream>
using namespace std;
int main ()
{ int n = 10;
while (n>0)
{
cout << n << ", ";
++n; }
cout << "liftoff!\n"; }
The do-while loop
• Syntax:
int main ()
{ string str;
do
{ cout << "Enter text: ";
getline (cin,str);
cout << "You entered: " << str << '\n';
}
while (str != "goodbye");
}
The do-while loop
• Syntax:
#include <iostream>
using namespace std;
int main ()
{
for (int n=10; n>0; n--)
{ cout << n << ", "; }
cout << "liftoff!\n"; }
The for loop
• Result:
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!
The for loop
• Result:
• Result:
10, 9, 8, 7, 6, 4, 3, 2, 1, liftoff!
The goto statement C++
int n=10;
mylabel:
cout << n << ", ";
n--;
if (n>0)
goto mylabel;
cout << "liftoff!\n";
Selection statement: switch
• Syntax:
switch (expression)
{
case constant1: group-of-statements-1; break;
case constant2: group-of-statements-2; break;
. . .
default: default-group-of-statements
}
Selection statement: switch
• Example:
switch (x)
{
case 1: cout << "x is 1";
break;
case 2: cout << "x is 2";
break;
default: cout << "value of x unknown"; }
Thank you for your attention!