35
LOOPING http://eglobiotraining.com RIVERA, BERNARD D.C.

Looping and Switchcase BDCR

  • Upload
    beriver

  • View
    210

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Looping and Switchcase BDCR

LOOPING

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Page 2: Looping and Switchcase BDCR

DEFINITION

Loops are used to repeat a block of code.Being able to have your program repeatedlyexecute a block of code is one of the most basic butuseful tasks in programming -- many programs orwebsites that produce extremely complex output(such as a message board) are really only executinga single task many times. (They may be executing asmall number of tasks, but in principle, to produce alist of messages only requires repeating the operationof reading in some data and displaying it.) Now, thinkabout what this means: a loop lets you write a verysimple statement to produce a significantly greaterresult simply by repetition.

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://www.cprogramming.com/tutorial/lesson3.html

Page 3: Looping and Switchcase BDCR

LOOPING

There are three different loop constructs that

can be used depending on whether the number of

repetitions is known and also (where the number of

repetitions is not known and is dependent on a

condition) whether the loop is allowed to be

bypassed if the termination condition is met before

the loop is first executed.

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://www.cprogramming.com/tutorial/lesson3.html

Page 4: Looping and Switchcase BDCR

“FOR” LOOP

A for loop in programming

allows a statement to beexecuted a specified number of

times.

The for loop begins with a

loop control variable assigned aspecific initial value. This control

variable in then incremented (or

decremented) by a specified

amount each time around the

loop until a specified terminatingvalue is reached at which time

the statement following the loop

is then executed.http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://javascript.about.com/library/blstruc3.htm

Page 5: Looping and Switchcase BDCR

“FOR” LOOP

The syntax for a for loop is:

for ( variable initialization; condition; variable update ) { Code to execute while

the condition is true }

The variable initialization 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 program that while the conditional expression 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. It is possible to do things like x++, x = x +

10, or even x = random ( 5 ), and if you really wanted to, you could call other

functions that do nothing to the variable but still have a useful effect on the

code. Notice that a semicolon separates each of these sections, that is

important. Also note that every single one of the sections may be empty, though

the semicolons still have to be there. If the condition is empty, it is evaluated as

true and the loop will repeat until something else stops it.

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://www.cprogramming.com/tutorial/lesson3.html

Page 6: Looping and Switchcase BDCR

“WHILE” LOOP

A while loop inprogramming allows a statement

to be executed until a given

condition is met. If the condition

is met prior to executing the loop

then the loop will not beexecuted. As soon as the

condition is met, execution

continues with the statement

following the loop.

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://javascript.about.com/library/blstruc3.htm

Page 7: Looping and Switchcase BDCR

“WHILE” LOOP

The basic structure is:

while ( condition ) { Code to execute while the condition is true }

The true represents a boolean expression which could be x == 1 or

while ( x != 7 ) (x does not equal 7). It can be any combination of

boolean statements that are legal. Even, (while x ==5 || v == 7) which

says execute the code while x equals five or while v equals 7. Notice

that a while loop is the same as a for loop without the initialization and

update sections. However, an empty condition is not legal for a while

loop as it is with a for loop.

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://www.cprogramming.com/tutorial/lesson3.html

Page 8: Looping and Switchcase BDCR

“DO WHILE” LOOP

A form of programming loop in which the condition for termination

(continuation) is computed each time around the loop. There are

several variants on this basic idea.

The basic structure is:

do { } while ( condition );

Notice that the condition is tested at the end of the block instead of

the beginning, so the block will be executed at least once. If the

condition is true, we jump back to the beginning of the block and

execute it again. A do..while loop is basically a reversed while loop. A

while loop says "Loop while the condition is true, and execute this block

of code", a do..while loop says "Execute this block of code, and loop

while the condition is true".

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://www.cprogramming.com/tutorial/lesson3.html

Page 9: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLES

Page 10: Looping and Switchcase BDCR

EXAMPLE 1

#include <stdio.h>

main()

{

float price;

short quantity;

char answer;

printf("Do you wish to enter a purchase (Y/N)? ");

scanf(" %c", &answer);

while(answer =='Y'||answer=='y')

{ printf("Enter 'price quantity': ");

scanf("%f %hi", &price, &quantity);

printf("The total for this item is $%6.2f.\n", price * quantity);

printf("Another (Y/N)? ");

scanf(" %c", &answer);

}

printf("Thank you for your patronage.\n");

}

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Page 11: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLE 1T H I S I S A P R O G R A M M I N G O F A P U R C H A S E O R D E R T H A T A

C U S T O M E R M A Y F I L L U P .

Page 12: Looping and Switchcase BDCR

EXAMPLE 2

http://eglobiotraining.com

RIVERA, BERNARD D.C.

// - Show all odd numbers using a for loop

#include <iostream>

using namespace std;

int main()

{

for (int count = 1; count <= 41; count += 2)

{

cout << count << ", ";

}

cout << endl;

return 0;

}

Source: http://www.codeobsessed.com/c++loops.html

Page 13: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLE 2T H I S I S A P R O G R A M M I N G L O O P T H A T WO U L D S H O W A L L

O D D N U M B E R S F R O M 1 T O 4 1 .

Page 14: Looping and Switchcase BDCR

EXAMPLE 3

// - Count backwards using a for loop.

#include <iostream>

using namespace std;

int main()

{

for (int count = 15; count > -1; count--)

{

cout << count << ", ";

}

cout << endl;

return 0;

}

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://www.codeobsessed.com/c++loops.html

Page 15: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLE 3T H I S I S A P R O G R A M M I N G L O O P T H A T C O U N T S

B A C K WA R D S F R O M 1 5 T O 1 .

Page 16: Looping and Switchcase BDCR

EXAMPLE 4

// - Count by fives using a for loop.

#include <iostream>

using namespace std;

int main()

{

for (int count = 0; count <= 100; count += 5)

{

cout << count << ", ";

}

cout << endl;

return 0;

}

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://www.codeobsessed.com/c++loops.html

Page 17: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLE 4T H I S P R O G R A M M I N G L O O P T H A T WO U L D C O U N T B Y 5

F R O M 0 T O 1 0 0 .

Page 18: Looping and Switchcase BDCR

EXAMPLE 5

#include <stdio.h>

int main()

{

int a;

a = 0;

while (a <= 100)

{

printf("%4d degrees F = %4d degrees C\n",

a, (a - 32) * 5 / 9);

a = a + 10;

}

return 0;

}

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://computer.howstuffworks.com/c9.htm

Page 19: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLE 5T H I S P R O G R A M M I N G L O O P T H A T WO U L D C O N V E R T

F A H R E N H E I T T O C E L S I U S .

Page 20: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

SWITCHCASE

Page 21: Looping and Switchcase BDCR

DEFINITION

Switch case statements are a substitute for long

if statements that compare a variable to several

"integral" values ("integral" values are simply values

that can be expressed as an integer, such as the

value of a char). The basic format for using switch

case is outlined below. The value of the variable

given into switch is compared to the value following

each of the cases, and when one value matches the

value of the variable, the computer continues

executing the program from that point.

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_028.htm

Page 22: Looping and Switchcase BDCR

SWITCHCASE

The switch case statement is a better way of writing a program when a series of if elses occurs. The general format for this is,

switch ( expression ) {

case value1:

program statement;

program statement;

......

break;

case valuen:

program statement;

.......

break;

default:

.......

.......

break;

The keyword break must be included at the end of each case statement. The default clause is optional, and is executed if the cases are not met. The right brace at the end signifies the end of the case selections. http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_028.htm

Page 23: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLES

Page 24: Looping and Switchcase BDCR

EXAMPLE 1

#include <stdio.h>

void main(void)

{ float price;

short quantity;

char answer;

do

{ printf("Enter 'price quantity': ");

scanf("%f %hi", &price, &quantity);

printf("The total for this item is $%6.2f.\n", price * quantity);

printf("Another (Y/N)? ");

scanf(" %c", &answer);

}while (answer == 'Y' | | answer == 'y');

printf("Thank you for your patronage.\n");

}

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Page 25: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLE 1T H I S I S P R O G R A M M I N G O F A B A S I C I N V E N T O RY S Y S T E M

T H A T WO U L D A D D , E D I T , D E L E T E A N D A C C E S S P R O D U C T S .

Page 26: Looping and Switchcase BDCR

EXAMPLE 2

#include <stdio.h>

main()

{

int menu, numb1, numb2, total;

printf("enter in two numbers -->");

scanf("%d %d", &numb1, &numb2 );

printf("enter in choice\n");

printf("1=addition\n");

printf("2=subtraction\n");

scanf("%d", &menu );

switch( menu ) {

case 1: total = numb1 + numb2; break;

case 2: total = numb1 - numb2; break;

default: printf("Invalid option selected\n");

}

if( menu == 1 )

printf("%d plus %d is %d\n", numb1, numb2, total );

else if( menu == 2 )

printf("%d minus %d is %d\n", numb1, numb2, total );

}

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_028.htm

Page 27: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLE 2T H I S I S A P R O G R A M M I N G O F A S O R T - O F A C A L C U L A T O R

T H A T A D D S A N D S U B T R A C T S 2 N U M B E R S .

Page 28: Looping and Switchcase BDCR

EXAMPLE 3

#include <stdio.h>

#include <stdlib.h>

int main()

{

int input;

do

{

printf( "1. Play game\n" );

printf( "2. Load game\n" );

printf( "3. Play multiplayer\n" );

printf( "4. Exit\n" );

printf( "Selection: " );

scanf( "%d", &input );

switch ( input )

{

case 1: /* Note the colon, not a semicolon */

printf("Playing the game\n");

break;

case 2:

printf("Loading the game\n");

break;

case 3:

printf("Playing multiplayer\n");

break;

case 4:

printf( "Thanks for playing!\n" );

break;

default:

printf( "Bad input!\n" );

break;

}

}while(input != 4);

}

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://programmingexamples.wikidot.com/c-switch-case

Page 29: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLE 3T H I S I S A P R O G R A M M I N G T H A T E N A B L E S Y O U T O P L A Y

G A M E , L O A D G A M E A N D P L A Y M U L T I P LA Y E R .

Page 30: Looping and Switchcase BDCR

EXAMPLE 4

#include <iostream.h>

int main()

{

unsigned short int number;

cout << "Enter a number between 1 and 5: ";

cin >> number;

switch (number)

{

case 0: cout << "Too small, sorry!";

break;

case 5: cout << "Good job!\n"; // fall through

case 4: cout << "Nice Pick!\n"; // fall through

case 3: cout << "Excellent!\n"; // fall through

case 2: cout << "Masterful!\n"; // fall through

case 1: cout << "Incredible!\n";

break;

default: cout << "Too large!\n";

break;

}

cout << "\n\n";

return 0;

}

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Source: http://www.java-samples.com/showtutorial.php?tutorialid=327

Page 31: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLE 4T H I S I S A P R O G R A M M I N G T H A T E N A B L E S T O E N C O D E

WI T H T H E P R O P E R V A L U E .

Page 32: Looping and Switchcase BDCR

EXAMPLE 5

#include <stdlib.h>

#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

RIVERA, BERNARD D.C.

Source: http://www.codingmagic.com/CPPTutorials/CPPTutorial04.html

Page 33: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

EXAMPLE 5T H I S I S A P R O G R A M M I N G T H A T E N A B L E S Y O U T O C H E C K

T H E V A L U E Y O U E N C O D E .

Page 34: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

Page 35: Looping and Switchcase BDCR

http://eglobiotraining.com

RIVERA, BERNARD D.C.

H T T P : / / E G L O B I O T R A I N I N G . C O M /

PROF. ERWIN GLOBIO