2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 3 Conditional Statements ALQUDS University Department of Computer Engineering Objective: Our objective for today s lab session is to introduce you to the C programing language. The materials supplied during this period will teach you to: 1. Study the componed if statements. 2. Learn how to use the switch construct. 3. Study about the type of assignments. Experiment 3.1 The else if ladder There is another way of putting ifs together when multipath decisions are involved. A multipath decision is a chain of ifs in which the statement associated with each else is an if. It takes the following general form: if (condition 1) statement 1 ; else if (condition 2) statement 2; else if (condition 3) statement 3; else if (condition n) statement n; else default statement; statement x; This construct is known as the else if ladder. The conditions are evaluated from the top (of the ladder), downwards. As soon as a true condition is found, the statement associated with is executed and the control is transferred to the statement x (skipping the rest of the ladder). 1
When all the n conditions become false, then the final else containing the default statement will be executed. Step 1: Let us consider an example of grading the students in an academic institution. The grading is done according to the following rules: 90-100 Excellent 80-89 Very good 70-79 Good 60-69 Bad Mark 00-59 Fail. Write a program using the else if ladder according the last rules. - - - - - - - - - - - - - - - - Step 2: C program that asks the user to enter his/her birth of day as follows. Your output should take the form: Today Date is: DD/MM/YYYY Your Birth of Date is DD/MM/YYYY Note: Suppose 1 Month=30 days. Your Age in Years is xx years, yy months, zz days Ex: Enter Today date in the format DD/MM/YYYY: 15/3/2009 Enter Your Birth of Date in the format DD/MM/YYYY: 10/2/1990 2
Output : Today Date is: 15/3/1990. Your Birth of Date is: 10/2/1990 Your Age is 19 years, 1 month(s), 5 days Algorithm C program --------- -------------------------------------------- 3
Experiment 3.2 The C switch construct is similar to Pascal's case statement and it allows multiple choice of a selection of items at one level of a conditional where it is a far neater way of writing multiple if statements: switch (expression) { case item 1 : statement 1 ; case item 2 : statement 2 ; } case item n : statement n ; default: statement; In each case the value of item i must be a constant, variables are not allowed. The break is needed if you want to terminate the switch after execution of one choice. Otherwise the next case would get evaluated. Note: This is unlike most other languages. The default case is optional and catches any other cases. Switch(x){ case 1: case 2: case 3: printf("pass") case 4: printf("not Pass") default: printf("kl") break } 4
Step 1: Compile and execute the program. Record the results and comment the results. Switch(x){ case 1: printf("the value of X is One ") case 2: printf("the value of X is Two ") case 3: printf("the value of X is Three ") default: printf("x not equal one or two or three.") } - - - - Step 2: Delete the break statement after the first case, record the results and comments the results. - -. Step 3: Write break after default statements in the last line, then record the results. - - 5
Experiment 3.3 Write a C program that asks the user to enter an integer number from 20 to 70, then prints the English words of the number. Ex. if the user enters 35. You enter the number: thirty five. Algorithm C program --------- -------------------------------------------- 6
Experiment 3.4 Conditional expressions Ternary Operators. exp0 = exp1? exp2 : exp3 This is equivalent to.. if (Exp1) exp0 = exp2; else exp0 = exp3; Step 1: Compile and execute the program. Record the results and comment the results. int main() { } int i; printf("insert the value of i: "); scanf("%d",&i); if (i==0) printf("i=0"); else printf("other value"); return 0; - - Step 2: Rewrite the last program using Ternary operation. - - - -. 7
Post lab #3: 1- Write a concise interactive and effecient C program that calculates a worker's weekly pay determined by the number of hours worked and the hourly rate. Any hour after the first 40 are paid at 1.5 times the regular hourly rate. The user should enter the number of hours worked and the hourly rate. 2-Write a program that asks the user for three integers and then responds by printing the integers in ascending order. 3- Write a C program that asks the user for a 12-hour-format time, then displays the time in 24- hour time. Ex. if the user enters 5:10 P the program responds with: the time in 24 time is: 17:10 if the user enters 5:10 A the program responds with: the time in 24 time is: 5:10 4- Using the switch statements write a c program that convert numerical grade into a letter grade. Ex. Enter the grade in numbers: 84 Grading: B Hint: Use the following grade scale: A: 90-100, B: 80-89, C: 70-79, D: 60-69, F:0-59, otherwise print an error massage. Note: Validate user input. 5- (Bonus question) Write a C program that determines the number of digits in a number. Ex. Enter a number: 4532 The number 4532 has 4 digits. Hint.you may assume that the number no more than four digits 8