Universität Duisburg-Essen
PRACTICAL TRAINING TO THE LECTURE
Fundamentals of Programming & Procedural Programming
Session Two: Boolean expressions, Conditional Executions: Chains and Nested
Name:
Matriculation-Number:
First Name:
Group-Number:
Tutor:
Date:
Prof. Dr.Ing. Axel Hunger Dipl.-Ing. Joachim Zumbrägel Universität Duisburg-Essen Faculty of Engineering, Department Electrical Engineering and Information Technology Computer Engineering
Procedural Programming/Lab2
1
Decision Making Calculations and expressions are merely a small fraction of computer programming. Till now, we concentrated on the linear programming, in which programs were being executed in a straight line, statement after statement, or as it is called officially as “sequential”. But as programs become more and more complex, they need to be equipped with decision making (selection) in order to specify which statements are to be executed in which order. In this session, we will focus on the control flow of a program with the help of branching statements. Branching statements allow a particular section of the code to be executed or not, depending on the given conditional clauses.
Relational Operators Programs compare values by using relational operators. In C, supported relational operators are: Operator > = %d is %d", x, y, x > y); >= %d is %d", x, y, x >= y); 4; ‘x’ = 13 && gender == 1) femaleTeenager = femaleTeenager + 1;
In the above example, the combined condition becomes true only and if only both conditions are true and the combined condition is false if either or both of the conditions are false. Expression 1
Expression 2
Expression 1 && Expression 2
False
False
False
False
True
False
True
False
False
True
True
True
Truth table for the && operator The || Operator Also known as the Or Operator, the || Operator connects two relational expressions. If either value of the expressions is true, the overall expression returns a true. For example: if (termAverage >= 80 || finalAverage >= 80) printf("Grade is A");
In the above example, the combined condition becomes true if either or both of the conditions are true and the combined condition is false only if both of the conditions are false.
Expression 1
Expression 2
Expression 1 || Expression 2
False
False
False
False
True
True
True
False
True
True
True
True
Truth table for the || operator
The ! Operator Also known as the Not Operator, the ! Operator reverses the value of an expression, making the value of a true expression false and the value of a false expression true.
Procedural Programming/Lab2
4
Example: /* using the not operator */ #include int main() { int marks; printf("Please enter your marks:"); scanf("%d",&marks); if ( ! (marks c) { printf("%d \t is } else { printf("%d \t is } if(b > a && b > c) { printf("%d \t is } else { printf("%d \t is } if(c > a && c > b) { printf("%d \t is } else { printf("%d \t is } return 0;
greater\n", a);
smaller\n", a);
greater\n", b);
smaller\n", b);
greater\n", c);
smaller\n", c);
}
Exercise 2.8: Please trace the result of each of the output statements in the above program and write down the results below.
Procedural Programming/Lab2
13
Exercise 2.9: The following flowchart is an example of the if‐else‐if‐else statement. Translate the flowchart into C code: Start Prompt user to input number input number Display the True num > 0 number is positive False True Display the num < 0 End number is negative False Display the
number is a zero
Procedural Programming/Lab2
14
Place here the C‐Code for Exercise 2.9!
Nested if Statements A nested if statement is an if statement which appears inside another if statement. They are used to resolve if either one of them of the expressions is true or whether both of the relational expressions hold true. Example: /* example: using the nested if-else statement */ #include int main() { int num; printf("Please enter a positive integer number: "); scanf("%d",&num); if (num > 0) { if (num%2 ==0) printf("The entered number is positive and even"); else printf("The entered number is positive and odd"); } else printf("The entered number is negative. Please try again."); return 0; }
Procedural Programming/Lab2
15
Exercise 2.10: Rewrite the above code without using the nested if‐else statement. Modify it so that it shows whether an entered number is negative, and whether it is even or odd in all cases.
Procedural Programming/Lab2
16
Mistakes to be Avoided •
An ‘else’ keyword always comes with the ‘if’ keyword. You can’t use an ‘else’ without the ‘if’ keyword.
•
A Relational Expression is not supposed to be used after the ‘else’ keyword.
•
A Semicolon is not used after the ‘else’ keyword
Exercise 2.11: Find and correct the errors in the following program segments: a) if (code == 'y') tax = pay * 0.07; else; tax = pay * 0.045; }
b) if (percentage >= 75); printf("Percentage is greater than or equal to 75"); else printf("Percentage is less than 75");
Procedural Programming/Lab2
17
Exercise 2.12: State whether the following nested‐if‐else statement is correct or not. If not, then rewrite it using proper indentation. if (a > b) if (j == k) {x = y; t = s;} else u = t; else if (f > e) h=h+1;
Exercise 2.13: Translate the following pseudocode into a valid C program: Set count to 1 If (balance >= 0) begin Set current_balance to balance Display balance end Else Set count to 0 End if
Procedural Programming/Lab2
18
The Switch Statement The switch statement allows a particular set of statements to be selected from several available choices. The selection is done based upon the current value of an expression, which is included within the switch statement. Syntax: The general form of a switch statement is Switch (expression) statement
where expression results in an integer value. Note that expression can also be of type char, since individual characters have equivalent integer values. The switch statement body is made up of a set of cases. Each case is identified with a case label. A case label starts with a case keyword, then a case value, followed with a colon. The case labels identify the different group of the statement (i.e. the different alternatives) and distinguish them from one another. The case label is followed by the body for that case. If the expression equals the case value that is in the case label, the program then executes the statements in the statement body. Example: /* example: using the switch statement */ #include int main() { int a, b; int choice; printf("Please enter first number: "); scanf("%d",&a); printf("Please enter second number: "); scanf("%d",&b); printf("Please enter your choice: "); printf("\n1 printf("\n2 printf("\n3 printf("\n4
-
Addition: "); Multiplication: "); Subtraction: "); Division: ");
scanf("%d", &choice);
Procedural Programming/Lab2
19
switch(choice) { case (1): printf("The sum is of the two numbers %d", a + b); break; case (2): printf("The multiplication of the two numbers is %d", a * b); break; case (3): printf("The subtraction of the two numbers is %d", a - b); break; case (4): printf("The division is of the two numbers %d", a / b); break; default: printf("Incorrect choice! Please try again."); return 0; }
Note that each of the groups is end with the break statement. The break statement causes control to be transferred out of the switch statement, thus preventing more than one group of statement from being executed. One of the labeled groups of the statement within the switch statement may be labeled default. This is executed if none of the case labels match the value of the expression. The default case label is optional and doesn’t need to be included at the end of the body of the switch statement.
Procedural Programming/Lab2
20
Exercise 2.14: What output would the following piece of code give? int const choice = 1; switch(choice + 1) { case '1': printf("Tea\n"); break; case '2': printf("Coffee\n"); break; case '3': printf("Milkshake\n"); break; default: printf("Bon appétit!\n"); return 0; }
Note: bon appétit, originated in French, basically means good appetite or enjoy your meal. .
Procedural Programming/Lab2
21