meljun cortes c++ chapter 3 control statements
TRANSCRIPT
More C Program Control More C Program Control StatementsStatements
C Programming C Programming LanguageLanguage
MELJUN CORTESMELJUN CORTES
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 2
ObjectivesObjectives
CC’’s most important program control statements s most important program control statements while, do~while while, do~while Determine your programDetermine your program’’s flow of execution.s flow of execution. Form the backbone of your programs.Form the backbone of your programs.
WeWe’’ll learnll learn Nested loopsNested loops More of control statements More of control statements break, continuebreak, continue Other selection statement Other selection statement switchswitch Unconditional jump statement Unconditional jump statement gotogoto
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 3
1.1. Input CharactersInput Characters
Using Library FunctionsUsing Library Functions getchar() getchar() : returns a single character typed on the : returns a single character typed on the
keyboard. keyboard. <stdio.h><stdio.h> waits for a key to be pressed. waits for a key to be pressed. (line buffers input)(line buffers input) waits until you enter a carriage return.waits until you enter a carriage return.
getche()getche() : returns a single character immediately after : returns a single character immediately after a key is pressed. a key is pressed. <conio.h><conio.h> Interactive inputInteractive input
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 4
1.1. Input CharactersInput Characters
#include <stdio.h>void main(void) { char ch; ch = getchar(); /* read a char */ printf(" you typed: %c", ch);}
#include <stdio.h>void main(void) { char ch; ch = getchar(); /* read a char */ printf(" you typed: %c", ch);}
getchar() functiongetchar() function
getche() functiongetche() function#include <stdio.h>#include <conio.h>void main(void) { char ch; printf(“Enter a character : “); ch = getche(); /* read a char */ printf(“\nIts ASCII code is %d”, ch);}
#include <stdio.h>#include <conio.h>void main(void) { char ch; printf(“Enter a character : “); ch = getche(); /* read a char */ printf(“\nIts ASCII code is %d”, ch);}
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 5
1.1. Input CharactersInput Characters
ExampleExample
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 6
1.1. Input CharactersInput Characters
ExercisesExercises
Write a program that reads ten letters, After the
Letters have been read, display the one that comes
earliest in the alphabet.
(Hint, The one with the smallest value comes first.)
Write a program that reads ten letters, After the
Letters have been read, display the one that comes
earliest in the alphabet.
(Hint, The one with the smallest value comes first.)
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 7
1.1. Input CharactersInput Characters
ExercisesExercises
Write a program that displays the ASCII codes for the
characters A through Z. and a through z. How do the
codes differ between the upper- and lowercase
characters?
Write a program that displays the ASCII codes for the
characters A through Z. and a through z. How do the
codes differ between the upper- and lowercase
characters?
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 8
if (count > max) // outer ifif(error) printf(“Error, try again.") ; // nested if
if (count > max) // outer ifif(error) printf(“Error, try again.") ; // nested if
2. Nest if statements2. Nest if statements
When anWhen an if if statement is the target of another statement is the target of another ifif or or elseelse nested ifnested if
• using indent of a nested if increase readability
• allow nested ifs at least 15 levels deep.
• using indent of a nested if increase readability
• allow nested ifs at least 15 levels deep.
IndentIndent
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 9
if (p) if(q) printf(“a and b are true") ;
else printf(“To which statement does this else apply?”);
if (p) if(q) printf(“a and b are true") ;
else printf(“To which statement does this else apply?”);
2. Nest if statements2. Nest if statements
One confusing aspect of nested ifOne confusing aspect of nested if
Dangling elseDangling else
An else always associates with the nearest ifin the same block that does not already have an else associated with it.
An else always associates with the nearest ifin the same block that does not already have an else associated with it.
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 10
if ( i != 0)
if ( i > 0 )
printf(“positive!\n”);
else
printf(“necative!\n”);
TF
F T
2. Nest if statements2. Nest if statements
Dangling else - ExampleDangling else - Example
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 11
2. Nest if statements2. Nest if statements
F T
TF
Dangling else solutionDangling else solution
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 12
2. Nest if statements2. Nest if statements
ExamplesExamples
if (expression) statement ; else if (expression) statement ; else if (expression) statement ; . . else statement ;
if (expression) statement ; else if (expression) statement ; else if (expression) statement ; . . else statement ;
if (expression) statement ; else if (expression) statement ; else if (expression) statement ; . . else statement ;
if (expression) statement ; else if (expression) statement ; else if (expression) statement ; . . else statement ;
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 13
2. Nest if statements2. Nest if statements
ExamplesExamples
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 14
2. Nest if statements2. Nest if statements
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 15
2. Nest if statements2. Nest if statements
ExamplesExamples
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 16
2. Nest if statements2. Nest if statements
ExercisesExercises
Write a program that computes the area of either a circle, rectangle, or triangle.Use an if-else-if ladder.
Write a program that computes the area of either a circle, rectangle, or triangle.Use an if-else-if ladder.
============ Area computation program =========Choice shape ( 1 : circle, 2 : rectangle, 3 : triangle ) : 1Input radius : 10Area of the circle is 31.40
Choice shape ( 1 : circle, 2 : rectangle, 3 : triangle ) : 2Input width : 10Input height : 20Area of the rectangle is 200.00
============ Area computation program =========Choice shape ( 1 : circle, 2 : rectangle, 3 : triangle ) : 1Input radius : 10Area of the circle is 31.40
Choice shape ( 1 : circle, 2 : rectangle, 3 : triangle ) : 2Input width : 10Input height : 20Area of the rectangle is 200.00
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 17
#include <stdio.h>#include <conio.h> int main(void){
int i;char ch;ch = 'a'; /* give ch an initial value */
for(i=0; ch!='q'; i++) { /* if ch is ‘q‘, stop loop */ printf("pass: %d\n", i); ch = getche();} return 0;
}
#include <stdio.h>#include <conio.h> int main(void){
int i;char ch;ch = 'a'; /* give ch an initial value */
for(i=0; ch!='q'; i++) { /* if ch is ‘q‘, stop loop */ printf("pass: %d\n", i); ch = getche();} return 0;
}
3. Examine for loop variations3. Examine for loop variations
Flexibility of for loop Flexibility of for loop one or more of the expressions inside for may be empty.one or more of the expressions inside for may be empty.
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 18
#include <stdio.h> int main(void) {
int i;printf("Enter an integer: ");scanf("%d", &i);
for( ; i; i--) printf("%d ", i); /* i times excute */
return 0; }
#include <stdio.h> int main(void) {
int i;printf("Enter an integer: ");scanf("%d", &i);
for( ; i; i--) printf("%d ", i); /* i times excute */
return 0; }
3. Examine for loop variations3. Examine for loop variations
Examples Examples –– expression empty expression empty
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 19
#include <stdio.h>
#include <conio.h>
int main(void) {
char ch;
for(ch=getche(); ch!='q'; ch=getche()) ;
printf(“\nfound the q\n");
return 0;
}
#include <stdio.h>
#include <conio.h>
int main(void) {
char ch;
for(ch=getche(); ch!='q'; ch=getche()) ;
printf(“\nfound the q\n");
return 0;
}
3. Examine for loop variations3. Examine for loop variations
Examples Examples –– target emptytarget empty
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 20
for( ; ; ) {
……
}
for( ; ; ) {
……
}
3. Examine for loop variations3. Examine for loop variations
Examples Examples –– infinite loopinfinite loop
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 21
#include <stdio.h>
int main(void) {
int i;
for(i=0; i<10; ) {
printf("%d ", i);
i++;
}
return 0;
}
#include <stdio.h>
int main(void) {
int i;
for(i=0; i<10; ) {
printf("%d ", i);
i++;
}
return 0;
}
3. Examine for loop variations3. Examine for loop variations
Examples Examples –– outside the increment sectionoutside the increment section
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 22
3. Examine for loop variations3. Examine for loop variations
ExercisesExercises
Write a program that computes driving time when given the distance and the average speed.Let the user specify the number of drive time computations he or she wants to perform.
Write a program that computes driving time when given the distance and the average speed.Let the user specify the number of drive time computations he or she wants to perform.
============ Driving time computation program =========How many compute driving time : 2Enter the distance and average speed : 50 30Driving time : 1.67Enter the distance and average speed : 100 80Driving time : 1.25
============ Driving time computation program =========How many compute driving time : 2Enter the distance and average speed : 50 30Driving time : 1.67Enter the distance and average speed : 100 80Driving time : 1.25
▶ Formula : dist = speed * time time = dist / speed▶ Formula : dist = speed * time time = dist / speed
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 23
3. Examine for loop variations3. Examine for loop variations
ExercisesExercises
To create time-delay loops, for loops with empty targets are
often used. Create a program that asks the user for a number
and then iterates until zero is reached.
Once the countdown is done, sound the bell, but don’t display
anything on the screen.
To create time-delay loops, for loops with empty targets are
often used. Create a program that asks the user for a number
and then iterates until zero is reached.
Once the countdown is done, sound the bell, but don’t display
anything on the screen.
============ Time-Delay program =========Enter a number : 1000000Good bye~~!!
============ Time-Delay program =========Enter a number : 1000000Good bye~~!!
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 24
3. Examine for loop variations3. Examine for loop variations
ExercisesExercises
Write a program that begins at 1 and runs to 1000.
Have the program add the loop-control variable to itself inside
the increment expression.
This is an easy way to produce the arithmetic progression 1 2 4
8 16, and so on.
Write a program that begins at 1 and runs to 1000.
Have the program add the loop-control variable to itself inside
the increment expression.
This is an easy way to produce the arithmetic progression 1 2 4
8 16, and so on.
1 2 4 8 16 32 ……1 2 4 8 16 32 ……
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 25
4. Understand C4. Understand C’’s s whilewhile Loop Loop
General form of General form of whilewhile loop loop
while (expression) statement;
while (expression) statement;
;
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 26
4. Understand C4. Understand C’’s s whilewhile Loop Loop
Compound statements of Compound statements of whilewhile loop loop
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 27
4. Understand C4. Understand C’’s s whilewhile Loop Loop
#include <stdio.h>
#include <conio.h>
int main(void) {
char ch;
ch=getche();
while(ch!='q‘)
ch=getche() ;
printf("found the q");
return 0;
}
#include <stdio.h>
#include <conio.h>
int main(void) {
char ch;
ch=getche();
while(ch!='q‘)
ch=getche() ;
printf("found the q");
return 0;
}
Examples Examples –– while loopwhile loop
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 28
4. Understand C4. Understand C’’s s whilewhile Loop Loop
#include <stdio.h>#include <conio.h> int main(void) {
char ch;printf(“Enter your message. \n”);ch=getche();while(ch!=‘\r‘) {
printf(“%c”, ch+1);ch=getche() ;
}return 0;
}
#include <stdio.h>#include <conio.h> int main(void) {
char ch;printf(“Enter your message. \n”);ch=getche();while(ch!=‘\r‘) {
printf(“%c”, ch+1);ch=getche() ;
}return 0;
}
Examples Examples –– simple code machinesimple code machine
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 29
4. Understand C4. Understand C’’s s whilewhile Loop Loop
ExercisesExercises
Exercise 1 of Section 3.3 use a while loopExercise 1 of Section 3.3 use a while loop
============ Driving time computation program =========How many compute driving time : 2Enter the distance and average speed : 50 30Driving time : 1.67Enter the distance and average speed : 100 80Driving time : 1.25
============ Driving time computation program =========How many compute driving time : 2Enter the distance and average speed : 50 30Driving time : 1.67Enter the distance and average speed : 100 80Driving time : 1.25
▶ Formula : dist = speed * time time = dist / speed▶ Formula : dist = speed * time time = dist / speed
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 30
4. Understand C4. Understand C’’s s whilewhile Loop Loop
ExercisesExercises
Write a program that converts centigrade to fahrenheit. Use awhile loop and compute from 5 to 50 of centigrade. Increasing value is 5.
Centigrade Fahrenheit
Write a program that converts centigrade to fahrenheit. Use awhile loop and compute from 5 to 50 of centigrade. Increasing value is 5.
Centigrade Fahrenheit
============ Fahrenheit computation program ========= 5 Centigrade is 41.00 Fahrenheit10 Centigrade is 50.00 Fahrenheit 15 Centigrade is 59.00 Fahrenheit : :50 Centigrade is 122.00 Fahrenheit
============ Fahrenheit computation program ========= 5 Centigrade is 41.00 Fahrenheit10 Centigrade is 50.00 Fahrenheit 15 Centigrade is 59.00 Fahrenheit : :50 Centigrade is 122.00 Fahrenheit
▶ Formula : F = 9/5 * C + 32 ;▶ Formula : F = 9/5 * C + 32 ;
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 31
5. Use the 5. Use the dodo Loop Loop
General form of General form of do_whiledo_while loop loop
do {statements
} while (expression) ;
do {statements
} while (expression) ;
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 32
5. Use the 5. Use the dodo Loop Loop
Compound statements of Compound statements of dodo loop loop
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 33
5. Use the 5. Use the dodo Loop Loop
Examples Examples –– reprompt the user until a valid inputreprompt the user until a valid input
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 34
5. Use the 5. Use the dodo Loop Loop
#include <stdio.h>
#include <conio.h>
int main(void) {
char ch;
do {
ch=getche();
} while(ch!='q‘);
printf(“Found the q. ");
return 0;
}
#include <stdio.h>
#include <conio.h>
int main(void) {
char ch;
do {
ch=getche();
} while(ch!='q‘);
printf(“Found the q. ");
return 0;
}
Examples Examples –– do loopdo loop
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 35
5. Use the 5. Use the dodo Loop Loop
ExercisesExercises
Write a program that converts gallons to liters. Using a do loop,allow the user to repeat the conversion.
Gallon Liter
Write a program that converts gallons to liters. Using a do loop,allow the user to repeat the conversion.
Gallon Liter
============ Gallons to Liters program =========Enter the Gallons : 100100 gallons is 378.5400 liters. Enter the Gallons : 10001000 gallons is 3785.4000 liters.: :
============ Gallons to Liters program =========Enter the Gallons : 100100 gallons is 378.5400 liters. Enter the Gallons : 10001000 gallons is 3785.4000 liters.: :
▶ Formula : L = 3.7854 * G ;▶ Formula : L = 3.7854 * G ;
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 36
5. Use the 5. Use the dodo Loop Loop
ExercisesExercises
Menu displayMenu display
============ Mailing list menu ========= 1. Enter address 2. Delete address 3. Search the list 4. Print the list 5. Quit Enter the number of your choice (1-5) : 3
Search the list
Good Bye~~!!
============ Mailing list menu ========= 1. Enter address 2. Delete address 3. Search the list 4. Print the list 5. Quit Enter the number of your choice (1-5) : 3
Search the list
Good Bye~~!!
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 37
6. Create nested Loops6. Create nested Loops
When the body of one loop contains anotherWhen the body of one loop contains another
for(i=0; i<10; i++) {for(j=1; j<11; j++) printf(“%d ”, j); // nested loopprintf(“\n”);
}
for(i=0; i<10; i++) {for(j=1; j<11; j++) printf(“%d ”, j); // nested loopprintf(“\n”);
}
1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 38
6. Create nested Loops6. Create nested Loops
Example 1Example 1
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 39
6. Create nested Loops6. Create nested Loops
Example 2Example 2
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 40
6. Create nested Loops6. Create nested Loops
ExercisesExercises
Write a program that finds all the prime numbers between 2 and 1000.Write a program that finds all the prime numbers between 2 and 1000.
============ The Prime Numbers ============The Prime Numbers : 2 3 5 7 11 13 17 19 ..........============ The Prime Numbers ============The Prime Numbers : 2 3 5 7 11 13 17 19 ..........
▶ Prime number checking of a number(num) for(i=2; i<num; i++)for(i=2; i<num; i++)
if(num % i==0) if(num % i==0) prime_number = 0 ;prime_number = 0 ;
▶ Prime number checking of a number(num) for(i=2; i<num; i++)for(i=2; i<num; i++)
if(num % i==0) if(num % i==0) prime_number = 0 ;prime_number = 0 ;
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 41
6. Create nested Loops6. Create nested Loops
ExercisesExercises
Write a program that reads ten characters from the keyboard.Each time a character is read, use its ASCII code value to output a string of periods equal in number to this code. For example, given the letter ‘A’, whose code is 65, your program would output65 periods.
Write a program that reads ten characters from the keyboard.Each time a character is read, use its ASCII code value to output a string of periods equal in number to this code. For example, given the letter ‘A’, whose code is 65, your program would output65 periods.
============ Periods Printing Program ============A : ………………………………………………………..D : …………………………………………………………..: :: :N : ……………………………………………………………………
============ Periods Printing Program ============A : ………………………………………………………..D : …………………………………………………………..: :: :N : ……………………………………………………………………
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 42
6. Create nested Loops6. Create nested Loops
Exercises Exercises –– star linestar line
Write a program that prints star lines when given thenumber of star.Write a program that prints star lines when given thenumber of star.
============ Stars Printing Program ============How many lines do you want to print : 5★★★★★★★★★★★★★★★★★★★★★★★★★
============ Stars Printing Program ============How many lines do you want to print : 5★★★★★★★★★★★★★★★★★★★★★★★★★
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 43
6. Create nested Loops6. Create nested Loops
Exercises Exercises –– star linestar line
Write a program that prints star lines when given thenumber of star.Write a program that prints star lines when given thenumber of star.
============ Stars Printing Program ============How many lines do you want to print : 5★★★★★★★★★★★★★★★
============ Stars Printing Program ============How many lines do you want to print : 5★★★★★★★★★★★★★★★
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 44
6. Create nested Loops6. Create nested Loops
Exercises Exercises –– star linestar line
Write a program that prints star lines when given thenumber of star.Write a program that prints star lines when given thenumber of star.
============ Stars Printing Program ============How many lines do you want to print : 5★★★★★★★★★★★★★★★
============ Stars Printing Program ============How many lines do you want to print : 5★★★★★★★★★★★★★★★
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 45
6. Create nested Loops6. Create nested Loops
Exercises Exercises –– star linestar line
Write a program that prints star lines when given thenumber of star.Write a program that prints star lines when given thenumber of star.
============ Stars Printing Program ============How many lines do you want to print : 5 ★ ★★ ★★★ ★★★★★★★★★
============ Stars Printing Program ============How many lines do you want to print : 5 ★ ★★ ★★★ ★★★★★★★★★
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 46
6. Create nested Loops6. Create nested Loops
Exercises Exercises –– star linestar line
Write a program that prints star lines when given thenumber of star.Write a program that prints star lines when given thenumber of star.
============ Stars Printing Program ============How many lines do you want to print : 5★★★★★ ★★★★ ★★★ ★★ ★
============ Stars Printing Program ============How many lines do you want to print : 5★★★★★ ★★★★ ★★★ ★★ ★
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 47
6. Create nested Loops6. Create nested Loops
Exercises Exercises –– star linestar line
Write a program that prints star lines when given thenumber of star.Write a program that prints star lines when given thenumber of star.
============ Stars Printing Program ============How many lines do you want to print : 5 ★ ★★★ ★★★★★ ★★★★★★★★★★★★★★★★
============ Stars Printing Program ============How many lines do you want to print : 5 ★ ★★★ ★★★★★ ★★★★★★★★★★★★★★★★
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 48
6. Create nested Loops6. Create nested Loops
Exercises Exercises –– star linestar line
Write a program that prints star lines when given thenumber of star.Write a program that prints star lines when given thenumber of star.
============ Stars Printing Program ============How many lines do you want to print : 5★★★★★★★★★ ★★★★★★★ ★★★★★ ★★★ ★
============ Stars Printing Program ============How many lines do you want to print : 5★★★★★★★★★ ★★★★★★★ ★★★★★ ★★★ ★
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 49
7. Use 7. Use breakbreak to exit a loop to exit a loop
allows you to exit a loopallows you to exit a loop
for(i=1; i<100; i++) {printf(“%d ”, i); if(i==10) break;break; // exit the loop
}
for(i=1; i<100; i++) {printf(“%d ”, i); if(i==10) break;break; // exit the loop
}
1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 50
7. Use 7. Use breakbreak to exit a loop to exit a loop
Using break in inner loopsUsing break in inner loops
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 51
7. Use 7. Use breakbreak to exit a loop to exit a loop
Example 1Example 1
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 52
7. Use 7. Use breakbreak to exit a loop to exit a loop
Example 2 Example 2 –– exit from only the innermost loopexit from only the innermost loop
#include <stdio.h>int main(void) {
int i, j ; for(i=0; i<5; i++) {
for(j=0; j<=100; j++) {printf(“%d ”, j);if(j==5) break;
}printf(“\n”);
}return 0;
}
#include <stdio.h>int main(void) {
int i, j ; for(i=0; i<5; i++) {
for(j=0; j<=100; j++) {printf(“%d ”, j);if(j==5) break;
}printf(“\n”);
}return 0;
}
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 53
7. Use 7. Use breakbreak to exit a loop to exit a loop
Exercises Exercises –– tip amounttip amount
Write a program that prints a table showing the proper amountof tip to leave. Start the table at $1 and stop at $100, using increments of $1. Compute three tip percentages : 10%, 15%, and20%. After each line, ask the user if he or she wants to continue.If not, use break to stop the loop and end the program.
Write a program that prints a table showing the proper amountof tip to leave. Start the table at $1 and stop at $100, using increments of $1. Compute three tip percentages : 10%, 15%, and20%. After each line, ask the user if he or she wants to continue.If not, use break to stop the loop and end the program.
============ Tip amount Program ============total Tip : 10% 15% 20%$1 0.10 0.15 0.20 continue (y/n) ? y$2 0.20 0.30 0.40 continue (y/n) ? y$3 0.30 0.45 0.60 continue (y/n) ? nThank you ~~!!
============ Tip amount Program ============total Tip : 10% 15% 20%$1 0.10 0.15 0.20 continue (y/n) ? y$2 0.20 0.30 0.40 continue (y/n) ? y$3 0.30 0.45 0.60 continue (y/n) ? nThank you ~~!!
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 54
8. The 8. The continuecontinue statement statement
Forces the next iteration of the loop to take place, Forces the next iteration of the loop to take place, skipping any code in between itself and the test condition skipping any code in between itself and the test condition of loop.of loop.
#include <stdio.h>int main(void) { int x; for(x=0; x<100; x++) { continue; printf("%d ", x); /* This is never executed. */ }
}
#include <stdio.h>int main(void) { int x; for(x=0; x<100; x++) { continue; printf("%d ", x); /* This is never executed. */ }
}
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 55
8. The 8. The continuecontinue statement statement
Using continue in loopsUsing continue in loops
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 56
8. The 8. The continuecontinue statement statement
Example 1Example 1
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 57
8. The 8. The continuecontinue statement statement
Exercises Exercises –– Using continueUsing continue
Write a program that prints only the odd numbers between 1
and 100. Use a for loop that looks like this :
for(i=1; i<101; i++) ……
Use a continue statement to avoid printing even numbers.
Write a program that prints only the odd numbers between 1
and 100. Use a for loop that looks like this :
for(i=1; i<101; i++) ……
Use a continue statement to avoid printing even numbers.
============ Odd Number Printing Program ============
1 3 5 7 9 11 13 15 17 19 …… 95 97 99
============ Odd Number Printing Program ============
1 3 5 7 9 11 13 15 17 19 …… 95 97 99
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 58
9. The 9. The switchswitch statement statement
Multiple selection statement.Multiple selection statement. The logic of switch statementThe logic of switch statement
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 59
9. The 9. The switchswitch statement statement
The general form of the switch statementThe general form of the switch statement
FlowFlow
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 60
Demonstrate the switch statementDemonstrate the switch statement
9. The 9. The switchswitch statement statement
#include <stdio.h>int main (void) { int printFlag; printf(“Please enter a integer:”); scanf(“%d”, &printFlag);
switch (printFlag) { case 1: printf(“This is case 1\n”); case 2: printf(“This is case 2\n”); default: printf(“This is default\n”); }} /* main */
#include <stdio.h>int main (void) { int printFlag; printf(“Please enter a integer:”); scanf(“%d”, &printFlag);
switch (printFlag) { case 1: printf(“This is case 1\n”); case 2: printf(“This is case 2\n”); default: printf(“This is default\n”); }} /* main */
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 61
Results of the demonstrationResults of the demonstration
9. The 9. The switchswitch statement statement
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 62
A switch with break statementsA switch with break statements
9. The 9. The switchswitch statement statement
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 63
Demonstrate the switch statementDemonstrate the switch statement
9. The 9. The switchswitch statement statement
#include <stdio.h> int main(void) { int i; printf("Enter a number between 1 and 4: "); scanf("%d", &i);
switch(i) { case 1: printf("one");
break; case 2: printf("two");
break;
#include <stdio.h> int main(void) { int i; printf("Enter a number between 1 and 4: "); scanf("%d", &i);
switch(i) { case 1: printf("one");
break; case 2: printf("two");
break;
case 3: printf("three"); break; case 4: printf("four"); break; default: printf("unrecognized number"); } return 0; }
case 3: printf("three"); break; case 4: printf("four"); break; default: printf("unrecognized number"); } return 0; }
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 64
Switch will work with only Switch will work with only intint or or charchar types. types. It is possible to have a switch as part of the statement It is possible to have a switch as part of the statement
sequence of an outer switchsequence of an outer switch
9. The 9. The switchswitch statement statement
switch(a) { case 1: switch(b) { case 0: printf("b is false"); break; case 1: printf("b is true"); } break; case 2: :
switch(a) { case 1: switch(b) { case 0: printf("b is false"); break; case 1: printf("b is true"); } break; case 2: :
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 65
Example 1Example 1
9. The 9. The switchswitch statement statement
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 66
Example 2Example 2
9. The 9. The switchswitch statement statement
MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS
School of Computer InformationSchool of Computer Information 67
Example 3Example 3
9. The 9. The switchswitch statement statement
#include <stdio.h>#include <conio.h>int main(void) { char ch; printf("Enter the letter: "); ch = getche(); switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'y': printf(" is a vowel\n");
break; default: printf(" is a consonant"); }
return 0;}
#include <stdio.h>#include <conio.h>int main(void) { char ch; printf("Enter the letter: "); ch = getche(); switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'y': printf(" is a vowel\n");
break; default: printf(" is a consonant"); }
return 0;}