
|
Switches and LoopsToday we will cover switch statements. The use of switch statements
is basically to simplify overly long and tedious if-else statements. For example,
if you had the following code in your program:
...
printf("Enter a month in digits:");
month = GetInteger();
if (month == 1) {
printf("January\n");
} else if (month == 2) {
printf("February\n");
} else if (month == 3) {
printf("March\n");
...
} else if (month == 12) {
printf("December\n");
} else {
printf("No such month!\n");
}
...
A switch statement can simplify the above code into something like below:
...
printf("Enter a month in digits:");
month = GetInteger();
switch (month) {
case 1: printf("January\n");
break;
case 2: printf("February\n");
break;
...
case 12: printf("December\n");
break;
default: printf("No such month!\n");
break;
}
...
Remember the 3 rules of switch statements from the lecture. In short,
they are:
Classwork program #1
Your boss tells you one day that he wants you to make a
program that reminds him of his job duties. He tells you
that on Mondays he goes to New York and has a meeting
at 6 pm. On Tuesdays he goes to Taiwan and has a meeting
on Wednesday at 3 pm. On Thursdays he has to visit his
Mother in law and takes phone calls all day. And on Friday
he needs to transfer money from his Swiss accounts to his
Paypal so that he could shop at Ebay. (Hint: use a
switch statement!!)
for loops
Loops allow you to repeat sections of code in your program. There are three ways
to produce a loop in C.
The general syntax of a for loop:
for (begin point; end point; incrementation) {
statements to be repeated;
}
A working example of this is:
for (i = 1; i <= 10; i++){
printf("%d\n", i);
}
This code will print out numbers from 1 to 10.Loops can be nested. One for loop can go into another.
Classwork program #2
Write a program that adds up the numbers 1, 2, 3, ... , 200
and prints out the answer in the end.
Classwork program #3
Write a program that produces 5 sets of multiplication tables.
(Use nested for loops and increment operators) Sample output:
1 x 1 = 1 1 x 2 = 2 ... 1 x 10 = 10 ... 2 x 1 = 2 ... 5 x 10 = 50 while loops
while loops let you repeat statements based on certain
conditions. It means that the number of repetitions you need is dependent
on certain conditions and cannot be determined at the time of developing
the program. The syntax and an example:
while (expression) {
statements;
}
while (x < 99) {
x++;
}
Classwork program #4
Using a while loop write a program that finds the greatest
common divisor of two positive integers. The greatest
common divisor is the largest number that goes into both
numbers evenly. For example, the GCD of 51 and 85 can be found
doing the following:
dividend/divisor 85/51 51/34 34/17 17/0 remainder rem 34 rem 17 rem 0 do while loops
The for and the while loops both perform the conditional
test before the loop is executed. Sometimes, however, you
are going to want to perform the test at the end of the loop.
This means the body of the loop is done at least once. The
do while loop looks like the following:
do {
statements;
} while (expression);
do {
i++;
} while (i < 10);
Classwork program #5
Write a program that prints out a positive integer backwards.
(Use a do while loop) Sample output:
Please enter a positive integer: 146 The integer written backwards is: 641 Homework assignment
Your homework assignment is to finish the classwork programs that
you did not finish in class; and to read Chapter 5: Functions.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||