Dev C++: Programming
Dev C++: Programming
Dev C++: Programming
Looping in Programming
Loops in programming are used to repeat a block of code. Being
able to have your programming repeatedly execute a block of code
is one of the most basic but useful tasks in programming -- many
programming programs or programming websites that produce
extremely complex output are really only executing a single task
many times. A loop in programming lets you write a very simple
statement to produce a significantly greater result simply by
repetition.
For Loop
For Loop is the most useful type in loop programming.
The syntax for for loop is :
The variable initialization in for loop allows you to either declare a variable and give it a
value or give a value to an already existing variable. Second, the condition tells the
programming that while the conditional expression in programming is true the loop
should continue to repeat itself. The variable update section is the easiest way for a
for loop to handle changing of the variable in programming.
Example of For Loop
The code for the example of for loop is:
#include <iostream>
int main()
{
intmain() {
doublef; // holds the length in feet
doublem; // holds the conversion to meters
intcounter;
counter = 0;
counter++;
return0;
}
Output of For Loop
Output of For Loop
Output of For Loop
http://eglobiotraining.com.
Output of For Loop
http://eglobiotraining.com.
Output of For Loop
http://eglobiotraining.com.
Explanation of For Loop
The output of the above example showed
tha conversion table of length (feet to
meters).
While Loop
WHILE loops programming are very simple. The basic structure of the
while loop is:
int main()
{
int x = 0;
while ( x < 10 ) {
cout<< x <<endl;
x++;
}
cin.get();
}
Output of the While Loop
Expalanation for While :
This While is simple, but it is longer than the
above FOR loop programming. The easiest
way to think of the loop programming is that
when it reaches the brace at the end it jumps
back up to the beginning of the loop
programming, which checks the condition again
and decides whether to repeat the block
another time, or stop and move to the next
statement after the block.
Do..While
DO..WHILE loops are useful for programming that want to loop at least
once. The structure is:
do {
} while ( condition );
Notice that the condition in the programming is tested at the end of the
block instead of the beginning, so the block will be executed in the
programming at least once. If the condition is true, we jump back to
the beginning of the block in the programming and execute it again. A
do..while loop is basically a reversed while loop programming. A while
loop programming says "Loop while the condition is true, and execute
this block of code", a do..while loop programming says "Execute this
block of code, and loop while the condition is true".
Example of Do..While
#include <iostream>
int main()
{
int x;
x = 0;
do {
cout<<Programming is Fun\n";
} while ( x != 0 );
cin.get();
}
http://eglobiotraining.com.
http://eglobiotraining.com.
Output of the Do..While
http://eglobiotraining.com.
Explanation of Do..While
http://eglobiotraining.com.
Infinite loop
A loop becomes infinite loop in C++
programming if a condition never
becomes false. The for loop is traditionally
used for this purpose. Since none of the
three expressions that form the for loop
are required, you can make an endless
loop by leaving the conditional expression
empty.
http://eglobiotraining.com.
Example of Infinite Loop
#include <iostream>
using namespace std;
int main ()
{
for( ; ; )
{ printf("This loop will run forever.\n"); }
return 0;
}
http://eglobiotraining.com.
http://eglobiotraining.com.
Output of the Infinite Loop
http://eglobiotraining.com.
Switch Case programming
The switch statement is used in C++
programming for testing if a variable is
equal to one of a set of values. The
variable must be an integer, i.e. integral or
non-fractional. The programmer can
specify the actions taken for each case of
the possible values the variable can have.
http://eglobiotraining.com.
Example of Switch Case
#include <stdlib.h>
statement
#include <stdio.h>
int main(void) {
int n;
printf("Please enter a number: ");
scanf("%d", &n);
switch (n) {
case 1: {
printf("n is equal to 1!\n");
break;
}
case 2: {
printf("n is equal to 2!\n");
break;
}
case 3: {
printf("n is equal to 3!\n");
break;
}
default: {
printf("n isn't equal to 1, 2, or 3.\n");
break;
}
}
system("PAUSE");
return 0;
}
http://eglobiotraining.com.
http://eglobiotraining.com.
Output of Switch case using no.1
http://eglobiotraining.com.
Output of Switch case using no.2
http://eglobiotraining.com.
Output of Switch case using no.3
http://eglobiotraining.com.
Output of Switch case using other
numbers
http://eglobiotraining.com.
Example of Switch Case
Statement
http://eglobiotraining.com.
Output of the switch Case
http://eglobiotraining.com.
Example of Switch Case
http://eglobiotraining.com.
http://eglobiotraining.com.
Output of Switch Case
http://eglobiotraining.com.
Explanation
The Switch case program shown above
will ask for a number and a letter an
distinguish if it is available in the program.
http://eglobiotraining.com.
DEV C++ PROGRAMMING
http://eglobiotraining.com.