C++ lecture 02
Click here to load reader
-
Upload
hnde-labuduwa-galle -
Category
Engineering
-
view
16 -
download
0
Transcript of C++ lecture 02
Information Technology III
Lecture 02 - Control Structures
It is used to execute an instruction or block of instructions only if a condition is fulfilled
if (condition) statement If this condition is true, statement is executed
If it is false, statement is ignored (not executed) the program continues next instruction after the conditional
structure
Example if (x == 100)
cout << "x is 100" ;
Conditional structure: if and else
if (x == 100) { // block of
instcout << "x is
" ; cout << x;
}
If – else Structure if (condition) statement1 else statement2
Example 1: if (x == 100)
cout << "x is 100" ; // true
else
cout << "x is not 100" ; // false
Example 2 if (x > 0) // outer if
cout << "x is positive" ;
else if (x < 0) // inner if ( false of outer if)
cout << "x is negative" ;
else cout << "x is 0" ; // false of inner if
The selective Structure: switch Its objective is to check several possible constant values for an
expression
Its form is the following:switch (expression) {
case constant1:
block of instructions 1
break;
case constant2:
block of instructions 2
break;
. . .
default: default block of instructions
}
Example switch example switch (x) {
case 1: cout << "x is 1"; break; case 2: cout << "x is 2"; break; default: cout << "value of x
unknown"; }
if-else equivalentif (x == 1)
{
cout << "x is 1";
}
else if (x == 2)
{
cout << "x is 2";
}
else
{
cout << "value of x unknown"; }
Iteration structures (loops) Loops have as purpose to repeat a statement a certain number
of times or while a condition is fulfilled
1. while loop while (expression) statement
its functionality is simply to repeat statement while the condition set in expression is true.
Example #include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > " ;
cin >> n;
while (n>0) // LOOP STARTS{
cout << n << ", " ; --n;
} // LOOP ENDSreturn 0;
}
2. do-while loop
Format do statement/s while (condition);
Its functionality is repeat the statements
condition in the do-while is evaluated after the execution of statement/s instead of before
granting at least one execution of statement even if condition is never fulfilled.
Example #include <iostream.h>
Using namespace std;
int main ()
{
unsigned long n;
do // LOOP START{ cout << "Enter number (0 to end): "; cin >> n; cout << "You entered: " << n << "\n"; } while (n != 0); // LOOP ENDSreturn 0;
}
3. For loop Its format is:
for (initialization; condition; increase) statement;
Its main function is to repeat statement while condition remains true
For provides places to specify an initialization instruction and an increase instruction
So this loop is specially designed to perform a repetitive action with a counter.
Example
#include <iostream.h>
Using namespace std;
int main ()
{
for (int n=10; n>0; n--) {
cout << n << ", "; } cout << “LOOP ENDS !";
return 0;
}
initialization; condition; increase
The break instruction Using break we can leave a loop even if the condition for its end is not fulfilled
It can be used to end an infinite loop, or to force it to end before its natural end
Examplefor (n=10; n>0; n--)
{ cout << n << ", "; if (n==3) {
cout << "countdown aborted!"; break;
} }
The continue instruction Continue instruction causes the program to skip the rest of
the loop in the present iteration
for (int n=10; n>0; n--)
{
if (n==5) continue;
cout << n << ", ";
}
cout << “it continues!";
return 0;
}