= 60) printf("You Passed \n"); return 0; } C:\Windows\System32\cmd.exe Type in your degree: 95 You Passed"> = 60) printf("You Passed \n"); return 0; } C:\Windows\System32\cmd.exe Type in your degree: 95 You Passed">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.

Similar presentations


Presentation on theme: "Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C."— Presentation transcript:

1 Making Decisions in c

2 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C language. this might be “written” as follows: if ( it is not raining ) I will go swimming if ( expression ) program statement Program statement expression yes no

3 Example 1.1 int main(void) { int degree; printf("Type in your degree: "); scanf("%i", &degree); if (degree >= 60) printf("You Passed \n"); return 0; } C:\Windows\System32\cmd.exe Type in your degree: 95 You Passed

4 2. if – else statement if ( expression ) program statement 1 else program statement 2 Program statement 1 expression yes no Program statement 2

5 Example 2.1 int main(void) { int degree; printf("Type in your degree: "); scanf("%i", &degree); if (degree >= 60) printf("You Passed \n"); else printf("You Failed \n"); return 0; } C:\Windows\System32\cmd.exe Type in your degree: 55 You Failed C:\Windows\System32\cmd.exe Type in your degree: 80 You Passed

6 Common Error A common error is to add a semicolon (;) at the end of the if statement, as you usually do with the most statements. int x = −3; if(x > 0); printf("x is positive\n"); C:\Windows\System32\cmd.exe x is positive the ; terminates the if statement and the program continues with the printf() call. Therefore, the output is always x is positive regardless of the value of x.

7 4.Nested if statements C:\Windows\System32\cmd.exe Enter a number: 11 C:\Windows\System32\cmd.exe Enter a number: 11 Output 2 int x ; printf("Enter a number: "); scanf("%i", &x); if(x < 10) if(x > 5) printf("output 1\n"); else printf(" output 2\n \n"); int x ; printf("Enter a number: "); scanf("%i", &x); if(x < 10) { if(x > 5) printf("output 1\n"); } else printf(" output 2\n \n"); In a program with nested if statements, each else statement is associated with the nearest if statement that does not contain an else

8 5. Else if int main(void) { int degree; printf("Type in your degree: "); scanf_s("%i", &degree); if (degree >= 90) printf("You Passed with A grade \n"); else if (degree >= 80) printf("You Passed with B grade \n"); else if (degree >= 70) printf("You Passed with C grade \n"); else if (degree >= 60) printf("You Passed with D grade \n"); else printf("You Failed \n"); return 0; } C:\Windows\System32\cmd.exe Type in your degree: 82 You Passed with B grade C:\Windows\System32\cmd.exe Type in your degree: 75 You Passed with C grade

9 6.Logical operators – Combining conditions int main(void) { int degree; printf("Type in your degree: "); scanf_s("%i", &degree); if (degree > 100 || degree < 0) printf("degree is not accepted \n"); else if (degree >= 90) printf("You Passed with A grade \n"); else if (degree >= 80) printf("You Passed with B grade \n"); else if (degree >= 70) printf("You Passed with C grade \n"); else if (degree >= 60) printf("You Passed with D grade \n"); else printf("You Failed \n"); return 0; } C:\Windows\System32\cmd.exe Type in your degree: 200 degree is not accepted C:\Windows\System32\cmd.exe Type in your degree: -3 degree is not accepted

10 6. Logical operators OperatorSymbolMeaning AND&&X && y is true if BOTH x and y are true OR||X || y is true if at least one of x and y is true NOT!!x is true if x is false Logical values as operands or in tests: true = non-zero, false= zero Logical values returned as results of expressions: true = 1, false= zero Example: ( 5 || 0 ) = 1

11 Precedence of operators Example for operator precedence: a > b && b > c || b > d Is equivalent to: ((a > b) && (b > c)) || (b > d) !, ++, --, (type) *, /, % +, -, >=, ==, != && || = Precedence

12 Testing for ranges if(x >= 5 && x <= 10) printf(“in range"); if (5 <= x <= 10) printf(“in range");

13 Testing for ranges if (x >= 5 && x <= 10) printf(“in range"); if (5 <= x <= 10) printf(“in range"); Syntactically correct, but semantically an error !!! Because the order of evaluation for the <= operator is left-to-right, the test expression is interpreted as follows: (5<= x) <= 10 The subexpression 5 <= x either has the value 1 (for true) or 0 (for false). Either value is less than 10, so the whole expression is always true, regardless of x !

14 7.Conditional operator The conditional operator ?: allows a program to perform one of two actions depending on the value of an expression. General format: condition ? expression1 : expression 2. condition is an expression that is evaluated first. If the result of the evaluation of condition is TRUE (nonzero), then expression1 is evaluated and the result of the evaluation becomes the result of the operation. If condition is FALSE (zero), then expression2 is evaluated and its result becomes the result of the operation

15 7.Conditional operator Example 2: int x = -2; (x > 0) ? printf("Positive \n") : printf(“Negative \n"); Example 1: maxValue = ( a > b ) ? a : b; Equivalent to: if ( a > b ) maxValue = a; else maxValue = b;

16 8.Switch () switch ( expression ) { case value1: program statement... break; case value2: program statement... break;... case valuen: program statement... break; default: program statement... break; } The switch statement can be used instead of if-else-if statements when we want to test the value of an expression against a series of values and handle each case differently. Remember to include the break statement at the end of every case. The switch test expression must be one with an integer value (including type char) (No float !). The case values must be integer-type constants or integer constant expressions (You can't use a variable for a case label !)

17 Example: Multiple choices int main(void) { float value1, value2; char op; printf("Type in your expression.\n"); scanf("%f %c %f", &value1, &op, &value2); if (op == '+') printf("%.2f\n", value1 + value2); else if (op == '-') printf("%.2f\n", value1 - value2); else if (op == '*') printf("%.2f\n", value1 * value2); else if (op == '/') printf("%.2f\n", value1 / value2); else printf("Unknown operator.\n"); return 0; } To execute this code correctly in Visual Studio, replace the following: Scanf ("%f %c %f", &value1, &op, &value2 ) with scanf_s ("%f %c %f", &value1, &op, 1, &value2 ) C:\Windows\System32\cmd.exe Type in your expression. 3 * 4 12.00

18 Example: Switch() int main(void) { float value1, value2; char op; printf("Type in your expression.\n"); scanf("%f %c %f", &value1, &op,&value2); switch (op) { case '+': printf("%.2f\n", value1 + value2); break; case '-': printf("%.2f\n", value1 - value2); break; case '*': printf("%.2f\n", value1 * value2); break; case '/': if (value2 == 0) printf("Division by zero.\n"); else printf("%.2f\n", value1 / value2); break; default: printf("Unknown operator.\n"); break; } return 0; }

19 Switch() switch (op) { case 'x': case '*': printf("%.2f\n", value1 * value2); break; …… } you can associate more than one case value with a particular set of program statements. printf() is executed if op is equal to an asterisk or to the lowercase letter x.


Download ppt "Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C."

Similar presentations


Ads by Google