Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decision Making.

Similar presentations


Presentation on theme: "Decision Making."— Presentation transcript:

1 Decision Making

2 Decision Making: Equality and Relational Operators
Executable C statements either perform actions (such as calculations or input or output of data) or make decisions (we’ll soon see several examples of these). We might make a decision in a program, for example, to determine if a person’s grade on an exam is greater than or equal to 60 and if it is to print the message “Congratulations! You passed.” This section introduces a simple version of C’s if statement that allows a program to make a decision based on the truth or falsity of a statement of fact called a condition.

3 Decision Making (Cont.)
If the condition is met (i.e., the condition is true) the statement in the body of the if statement is executed. If the condition is not met (i.e., the condition is false) the body statement is not executed. Whether the body statement is executed or not, after the if statement completes, execution proceeds with the next statement after the if statement. Conditions in if statements are formed by using the equality operators and relational operators summarized in Fig. 2.12.

4 Equality operators and relational operators

5 if structure-Method1 if (condition) { statement 1; statement 2; …
statement n; } else

6 if structure-Method2 if (condition) statement ; else

7 In C, a condition may actually be any expression that generates a zero (false) or nonzero (true) value! #include <stdio.h> int main() { int num1,num2; printf("Enter the 1st number >> "); scanf("%d",&num1); printf("Enter the 2nd number >> "); scanf("%d",&num2); //incorrect use of operation!, check the program with zero value if(num1=num2) { printf("num1=%d is equal to num2=%d",num1,num2); } else { printf("num1=%d is NOT equal to num2=%d",num1,num2); return 0;

8 Tip ; is a null statement! #include <stdio.h> int main() {
int num; printf("enter a number>>"); scanf("%d",&num); if (num>100); printf("This statement always is displayed!"); return 0; }

9 Nested if structure if (condition #1) { some statements; }
else if(condition #2) else if(condition #N) else

10 Example: Get grade and display the score
17≤ grade ≤ 20 A 15≤grade<17 B 12≤grade<15 C grade<12 D

11 Example: Get grade and display the score (cont.)
#include <stdio.h> int main() { float grade; printf("Enter the student\'s grade >> "); scanf("%f",&grade); if(grade>=17 && grade<=20) printf("grade =%5.2f, score=%c",grade,'A'); else if(grade>=15 && grade<17) printf("grade =%5.2f, score=%c",grade,'B'); else if(grade>=12 && grade<15) printf("grade =%5.2f, score=%c",grade,'C'); else printf("grade =%5.2f, score=%c",grade,'D'); return 0; }

12 switch Multiple-Selection Statement
Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each of the constant integral values it may assume, and different actions are taken. This is called multiple selection. C provides the switch multiple-selection statement to handle such decision making. The switch statement consists of a series of case labels, an optional default case and statements to execute for each case.

13 switch-case decision switch (expression){ case value#1: some statements; break; case value#2: … case value#n: default: }

14 switch-case decision(cont.)
The expression’s Types: any expression of integral or enumeration type The default section is optional, if the expression is equal to none of the values, none of the statements is done! When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

15 Nested switch-case We can use nested switch-case! switch (expression){
case value#1: some statements; break; default: }

16 switch-case vs. if if considers the logical and relational statements but switch-case considers the equality!

17 Example: A simple calculator!
Get the operation (contains +,-,*,/,\) as a character (variable name: op)! Get two input numbers (variable names: num1, num2 ). Compute and display the expression of: num1 (op) num2 + - * /or\

18 A simple calculator! (cont.)
float num1,num2;char op; printf("Enter the operation>>");scanf("%c",&op); printf("Enter the 1st number>>");scanf("%f",&num1); printf("Enter the 2nd number>>");scanf("%f",&num2); switch(op) { case'+': printf("%f %c %f = %f",num1,op,num2,num1+num2); break; case'*': printf("%f %c %f = %f",num1,op,num2,num1*num2); case '-': printf("%f %c %f = %f",num1,op,num2,num1-num2); case '/': case '\\': printf("%f %c %f = %f",num1,op,num2,num1/num2); default: printf("The operation is not defined!"); }

19 Example: color detector using nested switch
Get a character Check if the char is alphabetic, using isalpha() Detect the color, colors are: r, g, b, y The result should be case-insensitive!

20 Function isalpha() Header file: <ctype.h> Function
Work Of Function isalnum Tests whether a character is alphanumeric or not isalpha Tests whether a character is aplhabetic or not iscntrl Tests whether a character is control or not isdigit Tests whether a character is digit or not isgraph Tests whether a character is grahic or not islower Tests whether a character is lowercase or not isprint Tests whether a character is printable or not ispunct Tests whether a character is punctuation or not isspace Tests whether a character is white space or not isupper Tests whether a character is uppercase or not isxdigit Tests whether a character is hexadecimal or not tolower Converts to lowercase if the character is in uppercase toupper Converts to uppercase if the character is in lowercase

21 char color; printf("Enter the color char>>"); scanf("%c",&color); switch(isalpha(color)){ case 0: printf("Not alphabetic!"); break; default: if(color<=‘Z’) color+= 'a'-'A'; switch (color) { case 'r': printf("The color is red"); case 'b': printf("The color is blue"); case 'y': printf("The color is yellow"); case 'g': printf("The color is green"); printf("No color is defined!"); }


Download ppt "Decision Making."

Similar presentations


Ads by Google