C Program Control Switch Case Break 1
C Program Control Switch Case Break 1
C LAB WORKSHEET 8b
C & C++ Selection: The Conditional Operator And switch-case-break Part 5
Items in this page:
1. Write a complete working program that will ask for a #include <stdio.h>
person’s name and his/her game score. Then it will ask for a
second person’s name and score. The program will print the int main(void)
winner’s name and also print by how many points that {
person won. // variables to store the scores
int k, k1;
The following is a sample of a pseudocode. It is edited // strings to store names
many times to achieve the desired program output. char name[20] = "Dummy", name1[20] =
"Dummy";
Declare int variables to store the scores // read & store the first name and score
int k, k1; printf("Enter first name: ");
Declare string variables to store the names scanf_s("%s", name, 19);
char name[20] = "Dummy", name1[20] = printf("Enter first score: ");
"Dummy"; scanf_s("%d", &k, 1);
// read & store the second name and score
Read & store the first name and score
printf("Enter second name: ");
printf("Enter first name: ");
scanf_s("%s", name1, 19);
scanf_s("%s", name, 19); printf("Enter second score: ");
printf("Enter first score: "); scanf_s("%d", &k1, 1);
scanf_s("%d", &k, 1); // if k > k1
Read & store the second name and score if(k > k1)
printf("Enter second name: "); // then
scanf_s("%s", name1, 19); printf("The winner is %s with %d point.
printf("Enter second score: "); \n", name, k-k1);
scanf_s("%d", &k1, 1); // else
Do the comparison, which one is larger else
if(k > k1) printf("The winner is %s with %d point.
Print the winner name and the point \n", name1, k1-k);
else return 0;
}
Print the winner name and the point
Program stop.
2. Write a program that will read a float and a character for #include <stdio.h>
each scanf_s(). The character could be d for deposit or w for
withdrawal. Starting with a balance of zero, add the deposits int main(void)
and subtract the withdrawals until the balance becomes {
negative. Then print by how much the balance went float amount, balance=0.0;
negative. The sample input and output are given below. char transaction;
for(;man_score[i] != 0.0;)
{
if(man_score[i] < lowest_man)
lowest_man = man_score[i];
if(women_score[i] > highest_women)
highest_women = women_score[i];
i++;
scanf_s("%f", &man_score[i], sizeof
(man_score));
if(man_score[i] == 0.0)
break;
scanf_s("%f", &women_score[i], sizeof
(women_score));
}
#include <stdio.h>
int main(void)
{
int i;
for(i = 1; 5 - i; i = i + 1)
printf("%d\n", i);
printf("You are coming to the end of the loop\n");
return 0;
}
The loop runs only 4 times and then stops. What is really
happened here in the loop is that 5 – i is not a condition at all, it
is arithmetic expression. How is that interpreted? Whenever
an arithmetic expression evaluates to zero, that expression is
considered to have a false value. Conversely, whenever an
expression is non-zero, it is considered to be true. Hence, for
the first four values of i, 5 – i has a non-zero or a true value.
When i becomes 5, then 5 – i becomes zero, the expression
becomes false and the loop stops.
Writing condition like this takes fewer instruction cycles from
CPU/processor and the code runs faster than the if conditional
operators were used.
The Conditional Operator (?:), ternary
■ Whenever one value has to be assigned to a variable if a certain condition is true and another has to be
assigned to that same variable if it is false, then the conditional operator can be used. Using the
conditional operator will simplify your coding. The following if-else example is converted to using ?: operator.
#include <stdio.h>
int main(void)
{
// initialize with dummy value...
char x = 'n';
printf("Enter 'y' or non-'y': \n");
scanf_s(" %c", &x);
// the following codes can be converted using ?: operator
if(x == 'y')
printf("If 'y', answer is = 1\n");
else
printf("Else, answer is = 0\n");
return 0;
}
int main(void)
{
// initialize with dummy value...
char x = 'n';
printf("Enter 'y' or non-'y': \n");
scanf_s(" %c", &x);
// using ?: operator
(x == 'y') ? printf("If 'y', answer is = 1\n") : printf("Else, answer is
= 0\n");
return 0;
}
From the program example, if x == 'y' is true, the following printf() will be executed.
■ An organized way to write if statements that depend on the value of one variable is to use the switch-case-
break statement. The following example shows how the if program is converted to using the switch statement.
The breaks are required for all the cases except for the last one. Also the default case is optional.
#include <stdio.h> #include <stdio.h>
1. In the following practice, we will use the data in Table 3. We will use the rate in the Table to construct the logic
for determining the correct rate. ‘r’ will stand for “residential”, ‘c’ will stand for “commercial” and ‘u’ stand for “unit”.
Table 3
Let us first consider the condition for the case where type is
equal to “residential” only. If the type is residential and unit
of consumption are less than or equal to 200, then the rate
is 0.8, according to the Table. If the units are not less than
or equal to 200, then the rate is made equal to 0.7.
Complete the coding for this part of the logic.
| Main |< C & C++ if and if-else 4 | C & C++ switch-case-break 6 >| Site Index | Download |
The C Selection if, if-else, if-else-if, break, conditional/ternary operator and switch-case-break: Part 1 |
Part 2 | Part 3 | Part 4 | Part 5 | Part 6
tenouk.com, 2007