0% found this document useful (0 votes)
65 views

Introduction To Programming: Author of The Course: Associate Professor, Candidate of Technical Science Pachshenko G.N

This document discusses various iteration and selection statements in C++ programming. It begins by introducing loops like the while, do-while and for loops. It then discusses jump statements like break and continue, and how they alter the flow of loops. Finally, it covers the switch statement for selecting between multiple constant cases.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Introduction To Programming: Author of The Course: Associate Professor, Candidate of Technical Science Pachshenko G.N

This document discusses various iteration and selection statements in C++ programming. It begins by introducing loops like the while, do-while and for loops. It then discusses jump statements like break and continue, and how they alter the flow of loops. Finally, it covers the switch statement for selecting between multiple constant cases.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

INTRODUCTION TO

PROGRAMMING
Lecture 3
Author of the course:
associate professor,
candidate of technical science
Pachshenko G.N.
[email protected]
Topics

• Iteration statements (loops)


• The while loop
• The do-while loop
• The for loop
• Jump statements
• The continue statement
• The goto statement
• Selection statement: switch
C++ language

• Loops repeat a statement a certain number of times, or while a


condition is fulfilled. They are introduced by the keywords
while, do, and for.
The while loop

• The simplest kind of loop is the while-loop.

• Syntax:

while (expression) statement


The while loop

• The while-loop simply


repeats statement while expression is true.
• If, after any execution of statement, expression is no longer
true, the loop ends, and the program continues right after
the loop.
The while loop
The while loop

#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 first statement in main sets n to a value of 10. This is the


first number in the countdown.

• Then the while-loop begins: if this value fulfills the


condition n>0 (that n is greater than zero), then the block that
follows the condition is executed, and repeated for as long as
the condition (n>0) remains being true.
The while loop

• The whole process of the previous program can be interpreted


according to the following script (beginning in main):
The while loop
1. n is assigned a value
2. The while condition is checked (n>0). At this point there are
two possibilities:
• condition is true: the statement is executed (to step 3)
• condition is false: ignore statement and continue (to step 5)
3. Execute statement:
cout << n << ", "; --n;
(prints the value of n and decreases n by 1)
4. End of block. Return automatically to step 2.
5. Continue the program right after the block:
print liftoff! and end the program.
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.

• Otherwise, the loop will continue looping forever.


The while loop

#include <iostream>
using namespace std;
int main ()
{ int n = 10;
while (n>0)
{
cout << n << ", ";
++n; }
cout << "liftoff!\n"; }
The do-while loop

• Syntax:

do statement while (condition);


The do-while loop

• It behaves like a while-loop, except that condition is


evaluated after the execution of statement instead of before,
guaranteeing at least one execution of statement, even
if condition is never fulfilled.
The do-while loop
The do-while loop

• The following example program echoes any text the user


introduces until the user enters goodbye:
The do-while loop

int main ()
{ string str;
do
{ cout << "Enter text: ";
getline (cin,str);
cout << "You entered: " << str << '\n';
}
while (str != "goodbye");
}
The do-while loop

• The do-while loop is usually preferred over a while-loop


when the statement needs to be executed at least once.
The for loop

• The for loop is designed to iterate a number of times.

• Syntax:

for (initialization; condition; increase) statement;


The for loop

• Like the while-loop, this loop repeats statement while condition is


true.
• But, in addition, the for loop provides specific locations to contain
an initialization and an increase expression, executed before the
loop begins the first time, and after each iteration, respectively.
The for loop

It works in the following way:


1. initialization is executed. Generally, this declares a counter
variable, and sets it to some initial value. This is executed a single
time, at the beginning of the loop.
2. condition is checked. If it is true, the loop continues; otherwise,
the loop ends, and statement is skipped, going directly to step 5.
3. statement is executed. As usual, it can be either a single statement
or a block enclosed in curly braces { }.
4. increase is executed, and the loop gets back to step 2.
5. the loop ends: execution continues by the next statement after it.
The for loop
The for loop

• The countdown example using a for loop:

#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

• The three fields in a for-loop are optional. They can be left


empty, but in all cases the semicolon signs between them are
required.

• For example, for (;n<10;) is a loop


without initialization or increase (equivalent to a while-loop).
The for loop

• And for (;n<10;++n) is a loop with increase, but


no initialization (maybe because the variable was already
initialized before the loop).
Jump statements

• Jump statements allow altering the flow of a program by


performing jumps to specific locations.

• The break statement


• Break leaves a loop, even if the condition for its end is not
fulfilled. It can be used to end an infinite loop.
The break statement

for (int n=10; n>0; n--)


{ cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!"; break;
}
}
The break statement

• Result:

10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!


The continue statement

• The continue statement causes the program to skip the rest of


the loop in the current iteration, as if the end of the statement
block had been reached, causing it to jump to the start of the
following iteration.
The continue statement

• For example, let's skip number 5 in our countdown:

for (int n=10; n>0; n--)


{
if (n==5) continue;
cout << n << ", ";
}
cout << "liftoff!\n";
The continue statement

• Result:

10, 9, 8, 7, 6, 4, 3, 2, 1, liftoff!
The goto statement C++

• goto allows to make an absolute jump to another point in the


program.

• The destination point is identified by a label, which is then


used as an argument for the goto statement. A label is made of
a valid identifier followed by a colon (:).
The goto statement C++

int n=10;
mylabel:
cout << n << ", ";
n--;
if (n>0)
goto mylabel;
cout << "liftoff!\n";
Selection statement: switch

• Its purpose is to check for a value among a number of


possible constant expressions.

• It is something similar to concatenating if-else statements,


but limited to constant expressions.
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

• It works in the following


way: switch evaluates expression and checks if it is
equivalent to constant1; if it is, it executes group-of-
statements-1 until it finds the break statement.

• When it finds this break statement, the program jumps to the


end of the entire switch statement (the closing brace).
Selection statement: switch

• Finally, if the value of expression did not match any of the


previously specified constants (there may be any number of
these), the program executes the statements included after
the default: label, if it exists (since it is optional).
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!

You might also like