1 / 78

Selection Statements

Selection Statements. Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection statements: if statement switch statement. Selection statements: if.

thane
Download Presentation

Selection Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Selection Statements • Selects statements to execute based on the value of an expression • The expression is sometimes called the controlling expression • Selection statements: • if statement • switch statement

  2. Selection statements: if • used to execute conditionally a statement or block of code. if (expression) statement • If expression is true, statement is executed (what is true?). • statement can be replaced by a block of statements, enclosed in curly braces.

  3. An example /* This program displays the absolute value of a number given by the user */ #include <stdio.h> int main(void) { double num; printf("Please enter a real number: "); scanf("%lf", &num); if (num<0) num = -num; printf("The absolute value is %g\n", num); return 0; }

  4. If-else statement if (expression) statement1 else statement2 • if expression is true, statement1 is executed. • if expression is false, statement2 is executed • both statements can be (and very often are) replaced by blocks of statements (“compound statements”)

  5. An example (fragment) int first, second, min; /* … */ if (first < second) { min = first; printf ("The first number is smaller than the second.\n"); } else { min = second; printf ("The second number is smaller than the first\n"); } printf("The smaller number is equal to %d\n", min);

  6. True or false • In C, every expression has a numeric value • An expression is ‘true’ when its non-zero • If it is zero, it is false • Therefore, in the following – if (expression) statementstatement is executed if expression is non zero.

  7. More about operators • In C, every expression has a numeric value • When using arithmetical operators (+, -, *, /) this is straight forward • The value of A+B is the sum of A and B • And so on…

  8. More about operators • Expressions with relational operators (<, <=, etc’) have values as well (intuitively, we are used to thinking about them as ‘true’ or ‘false’) • A < B equals zero if A is larger than or equal to B (false), and some non-zero value if A is smaller than B (true) • The exact non-zero value varies (and is not important for that matter)

  9. Relational operators • They are – • A == B (Note the difference from A = B) • A != B • A < B • A > B • A <= B • A >= B • The value of the expression is non-zero if it’s true, zero if it’s false

  10. An example int a, b; printf("Enter two Numbers\n"); scanf("%d%d", &a, &b); if (a == b) { printf("The numbers equal %d\n", a); printf("The expression a == b is %d\n", a == b); } else { printf("The numbers are not equal\n"); printf("The expression a == b is %d\n", a == b); }

  11. The assignment operator = • The assignment operator is also an operator. Hence, expressions involving it have a numeric value • This value equals to whatever appears on the right of the assignment operator • For example – • (x = 4) equals 4 • (y = 0) equals 0

  12. A very common mistake • Very often a programmer might confuse between the equality operator and the assignment operator - • if (x==4) … • if (x=4) … • The second is usually a mistake, but legal in C so the compiler doesn’t call it!

  13. Examples • val.c, eqn_sign.c

  14. Logical operators • Allows to evaluate two or more expressions - • !A – ‘not’ - True when A is not, and vice versa. • A && B – ‘and’ - True when both A and B are true • A || B – ‘or’ (inclusive or) - True when either A or B (or both) are true

  15. A silly example #include <stdio.h> int main(void) { int grade; printf("Please enter your grade: "); scanf("%d", &grade); if (grade<0 || grade>100) printf("This is not a valid grade!\n"); else printf("This is indeed a grade.\n"); return 0; }

  16. Else-if • if statements distinguish between exactly 2 cases and execute different code in each case • The else-if construction allows for a multi-way decision

  17. Else-if if (expression) statement else if (expression) statement elseif (expression) statement else statement

  18. An example if (grade >= 90) printf ("A\n"); else if (grade >= 80) printf ("B\n"); else if (grade >= 70) printf ("C\n"); else if (grade >= 60) printf ("D\n"); else printf ("F\n");

  19. Validating input • When getting input from the user, it is highly recommended to check whether it is valid. • If it’s not, you should display an appropriate message and return a non-zero value. • For example – if (grade<0 || grade>100) { printf(“Invalid input!\n”); return 1; }

  20. The return keyword • For now, used to terminate the program and return a value to the operating system • If the program is successful the return value should be zero, non-zero otherwise • The exact nature of this keyword will become clear in the future

  21. Exercise • Input – • An English letter • Output – • If input is a lowercase letter – the corresponding uppercase letter • If input is an uppercase letter - corresponding lowercase letter • Note – • Remember to check for input validity!

  22. Solution • switch_case.c

  23. Exercise (difficult!) • Write a program such that – • Input – a 3-digit number • Output – the same number with digits sorted • Example – if input is 132, output should be 123 • Note – if input is not a 3-digit number, display an error message and exit!

  24. Solution • Sort_digits.c

  25. Loops • Used to repeat the same instruction(s) over and over again. • C provides some flexible ways of deciding how many times to loop, or when to exit a loop. • for, while, do-while loops.

  26. While loops while (condition) { statement(s); } The statements are executed as long as condition is true When the condition is no longer true, the loop is exited.

  27. Example - factorial #include <stdio.h> int main(void) { int i,n,fact = 1; printf("Enter a number\n"); scanf("%d", &n); i=1; while (i<=n) { fact = fact*i; i++; } printf("the factorial is %d\n", fact); return 0; } This is a counter Every iteration i is incremented by 1. Equivalent to i=i+1.

  28. Example – fibonacci series fibonacci.c

  29. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 0 1 --- 5 Screen 0

  30. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 0 1 --- 5 Screen 0

  31. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 0 1 --- 5 Screen 0 1

  32. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 0 1 1 5 Screen 0 1

  33. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 1 5 Screen 0 1

  34. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 1 5 Screen 0 1

  35. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 1 5 Screen 0 1

  36. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 1 5 Screen 0 1 1

  37. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 2 5 Screen 0 1 1

  38. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 1 2 5 Screen 0 1 1

  39. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 2 2 5 Screen 0 1 1

  40. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 2 2 5 Screen 0 1 1

  41. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 2 2 5 Screen 0 1 1 2

  42. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 1 2 3 5 Screen 0 1 1 2

  43. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 2 2 3 5 Screen 0 1 1 2

  44. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 2 3 3 5 Screen 0 1 1 2

  45. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 2 3 3 5 Screen 0 1 1 2

  46. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 2 3 3 5 Screen 0 1 1 2 3

  47. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 2 3 5 5 Screen 0 1 1 23

  48. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 3 3 5 5 Screen 0 1 1 23

  49. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 3 5 5 5 Screen 0 1 1 23

  50. Fibonacci – step by step fib1 fib2 fib_next lim fib1 = 0; fib2 = 1; printf("%d ", fib1); while(fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; } printf("\n"); 3 5 5 5 Screen 0 1 1 23

More Related