Introduction To
Programming
Contents
• Increment/Decrement
• Loops
While
Do/While
For
• Break
• Continue
Increment/Decrement
#include<iostream> #include<iostream> int main()
using namespace std; using namespace std; {
int main() int a = 3, b, c, d, e ;
{ int main() b = ++a;
int x = 3, y, z; { c = --a;
y = x++; int x = 3, y, z; d = a++;
z = ++x; y = x--; e = a--;
cout << x << ", " << y << ", " z = --x; cout << a << ", " << b << ", "
<< z; cout << x << ", " << y << ", << c << ", " << d <<", "<< e;
return 0; " << z; return 0;
} return 0; }
}
Loops
• Loops can execute a block of code as long as a specified condition is
reached.
• Loops are handy because they save time, reduce errors, and they
make code more readable.
While Loop
• The while loop loops through a block of code as long as a specified
condition is true.
• Syntax-
while (condition)
{
// code block to be executed
}
While Loop...
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int i = 0; int i = 0;
while (i < 5) while (i <= 5)
{ {
cout << i << "\n"; cout << i << "\n";
i++; i++;
} }
return 0; return 0;
} }
Do/While Loop
• The do/while loop is a variant of the while loop. This loop will
execute the code block once, before checking if the condition is true,
then it will repeat the loop as long as the condition is true.
• Syntax-
do
{
// code block to be executed
}
while (condition);
Do/While Loop...
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() { int main() {
int i = 0; int i = 0;
do { do {
cout << i << "\n"; cout << i << "\n";
i++; i++;
} }
while (i < 5); while (i < 0);
return 0; return 0;
} }
For Loop
• When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop.
• Syntax-
for (statement 1; statement 2; statement 3)
{
// code block to be executed
}
For Loop...
• Statement 1 is executed (one time) before the execution of the code
block.
• Statement 2 defines the condition for executing the code block.
• Statement 3 is executed (every time) after the code block has been
executed.
For Loop...
#include <iostream> #include <iostream> #include <iostream>
using namespace std; using namespace std; using namespace std;
int main() { int main() { int main() {
for (int i = 0; i < 5; i++) for (int i = 0; i <= 5; i++) for (int i = 0; i <= 10; i = i + 2)
{ { {
cout << i << "\n"; cout << i << "\n"; cout << i << "\n";
} } }
return 0; return 0; return 0;
} } }
Break
• The break statement can also be used to jump out of a loop.
int main() {
int main() {
for (int i = 0; i < 10; i++)
for (int i = 0; i < 10; i++)
{
{
if (i > 5)
if (i == 5)
{
{
break;
break;
}
}
cout << i << "\n";
cout << i << "\n";
}
}
return 0;
return 0;
}
}
Continue
• The continue statement breaks one iteration (in the loop), if a
specified condition occurs, and continues with the next iteration in
the loop.
int main() { int main() {
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
if (i == 5) if (i < 5)
{ {
continue; continue;
} }
cout << i << "\n"; cout << i << "\n";
} }
return 0; return 0;
} }