Lab Module Chapter 5
Lab Module Chapter 5
2. Suppose I want to check if my variable p is in between the value of -1 and 1. Which of the following
expressions can be used to test p?
A. -1 < p < 1
B. -1 < p && < 1
C. -1 < p && p < 1
D. -1 < p && p > 1
E. -1 < p || p < 1
3. Explain the difference of =, !=, and ==
=
To assign a certain point to a certain variable
Means an expression isn’t equal to other expression
!=
Note that the example here can only hold one single statement. If we want to have multiple statements in one
if statement, we need to use what we call as a compound statement.
Compound Statement: a group of statements inside a set of { and } braces
Example:
if (age <= 17)
{
printf(“You may not apply to this program.”);
continue = 0;
}
//the compiler will treat these two statements as a single statement
Be careful with the logical sequence when using nested if statements. It is always safe to put braces around if
statements to avoid confusion:
if (pass == 1)
{
if (toefl >= 71)
{
printf(“Full Admission”);
}
else
{
printf(“Conditional Admission”);
}
}
else
{
printf(“Unqualified”);
}
4. Multi way selection statement (cascaded)
Flowchart for multi selection statement:
int main(void)
{
int speed;
printf("Input speed:");
scanf("%d", &speed);
if(speed<=65)
printf("You must pay: $0 ");
else if(66<speed&&speed<70)
printf("You must pay: $15.00 ");
else if(71<speed&&speed<75)
printf("You must pay: $30.00 ");
else if(76<speed&&speed<80)
printf("You must pay: $75.00 ");
else if(speed>=80)
printf("You must pay: $100.00 ");
else
printf("Input Error");
return 0;
}
Exercise 3: Selection Statements – Conditional Expressions and Boolean Values
1. Conditional Expressions
Below is the general syntax of conditional expressions:
expression1?expression2:expression3
meaning: if expression1, then expression2, else expression3
Conditional expressions can make the code shorter, but might be harder to understand, so be wise in using
them.
Example of conditional expression in return statement:
return i > j ? i : j;
if (i > j)
printf("%d\n", i);
else
printf("%d\n", j); //The if statement will look like this
------------------------------------------------------------------------------------------------------------------------------------
-
.............................
bool flag; //defining a _Bool type variable flag
.............................
flag = true; //assign the value 1 to flag
.............................
flag = false; // assign the value 0 to flag
.............................
flag = 5;
/*attempting to store a nonzero value will cause the variable to be
assigned 1. Here the value of flag will be 1*/
.............................
if(flag) //check whether the value of flag is 1
.............................
if(!flag) //check whether the value of flag is 0
.............................
/*Notes:
- constant expressions: an expression that has a constant value. It
cannot contain variables or function calls
- the constant expressions must evaluate to integers (characters are
acceptable
- duplicate case labels are not allowed
- the order of the cases doesn’t matter
- default doesn’t need to come last
- if the default case is missing and the controlling expression’s
value doesn’t match any case label, control will pass to the next
statement after switch*/
Several case labels can be put on the same line preceding a group of statements:
switch (grade)
{
case 4: case 3: case 2: case 1: //grade 4,3,2, or 1, is Passing
printf("Passing");
break;
case 0: printf("Failing");
break;
default: printf("Illegal grade");
break;
}
2. Executing break statement causes the program to break out of the switch statement. Execution will
continue to the statement after switch. Take a look at the code below:
#include <stdio.h>
Run the program and record the output of the following input:
Input Output
0 FailingIllegal Grade
1 PoorFailingIllegal Grade
2 AveragePoorFailingIllegal Grade
3 GoodAveragePoorFailingIllegal Grade
4 ExcellentGoodAveragePoorFailingIllegal Grade
A Illegal Grade
Note:
The last case label (or default if placed last) doesn’t need a break statement. However, adding one
makes it easy to add cases in the future.
3. Program: Printing a Date in Legal Form (from ch05 ppt – Norton, 2008)
Contracts and other legal documents are often dated in the following way:
Dated this __________ day of __________ , 20__ .
The date.c program will display a date in this form after the user enters the date in month/day/year form:
Enter date (mm/dd/yy): 7/19/14
Dated this 19th day of July, 2014.
The program uses switch statements to add “th” (or “st” or “nd” or “rd”) to the day, and to print the month
as a word instead of a number:
/* Prints a date in legal form */
#include <stdio.h>
int main(void)
{
int month, day, year;
printf("Enter date (mm/dd/yyyy): ");
scanf("%d /%d /%d", &month, &day, &year);
printf("Dated this %d", day);
switch (day)
{
case 1: case 21: case 31:
printf("st"); break;
case 2: case 22:
printf("nd"); break;
case 3: case 23:
printf("rd"); break;
default: printf("th"); break;
}
printf(" day of ");
switch (month)
{
case 1: printf("January"); break;
case 2: printf("February"); break;
case 3: printf("March"); break;
case 4: printf("April"); break;
case 5: printf("May"); break;
case 6: printf("June"); break;
case 7: printf("July"); break;
case 8: printf("August"); break;
case 9: printf("September"); break;
case 10: printf("October"); break;
case 11: printf("November"); break;
case 12: printf("December"); break;
}
printf(", %d.\n", year);
return 0;
}
Run the program and give a sample output (indicate any input with underlined character/number)
Dated this 23rd day of December, 1999.
Your program will display the characterization of the earthquake based on the scale number provided by user.
a. Write down the flowchart for the program.
b. Can you handle this problem with a switch statement? If so, use a switch statement; if not, explain
why.
c. Write a program based on flowchart in (a).
3. Write a code segment that assigns to the variable lumens the expected brightness of a standard light bulb
whose power has been stored in watts. Use the following table: