Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming

Similar presentations


Presentation on theme: "Introduction to C Programming"— Presentation transcript:

1 Introduction to C Programming
ET2560 Introduction to C Programming Introduction to C Programming Unit 4 “To Be or Not To Be” Programming Decisions Unit 1 Presentations

2 Review of Unit 3 Unit 4: Review of Past Material

3 Review of Past Concepts
#include <stdio.h> #include <math.h> typedef enum { BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET, GRAY, WHITE } colorbands_t; int main(void) { colorbands_t firstBand = ORANGE; colorbands_t secondBand = RED; colorbands_t thirdBand = BROWN; double res; res = (firstBand * 10 + secondBand) * pow(10, thirdBand); printf("Resistance is %.f\n", res); return (0); }

4 Review - Expressions and Assignments
#include <stdio.h> #include <math.h> typedef enum { BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET, GRAY, WHITE } colorbands_t; int main(void) { colorbands_t firstBand = ORANGE; colorbands_t secondBand = RED; colorbands_t thirdBand = BROWN; double res; res = (firstBand * 10 + secondBand) * pow(10, thirdBand); printf("Resistance is %.f\n", res); return (0); }

5 Review - Use of <math.h> Functions
#include <stdio.h> #include <math.h> typedef enum { BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET, GRAY, WHITE } colorbands_t; int main(void) { colorbands_t firstBand = ORANGE; colorbands_t secondBand = RED; colorbands_t thirdBand = BROWN; double res; res = (firstBand * 10 + secondBand) * pow(10, thirdBand); printf("Resistance is %.f\n", res); return (0); }

6 Review - The enum Data Type
#include <stdio.h> #include <math.h> typedef enum { BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET, GRAY, WHITE } colorbands_t; int main(void) { colorbands_t firstBand = ORANGE; colorbands_t secondBand = RED; colorbands_t thirdBand = BROWN; double res; res = (firstBand * 10 + secondBand) * pow(10, thirdBand); printf("Resistance is %.f\n", res); return (0); }

7 Review - Printf formatting
#include <stdio.h> #include <math.h> typedef enum { BLACK, BROWN, RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET, GRAY, WHITE } colorbands_t; int main(void) { colorbands_t firstBand = ORANGE; colorbands_t secondBand = RED; colorbands_t thirdBand = BROWN; double res; res = (firstBand * 10 + secondBand) * pow(10, thirdBand); printf("Resistance is %.f\n", res); return (0); }

8 Conditional Operators & Expressions
Unit 4: Conditional Expressions

9 Conditional Values Another form of expression is a conditional
A conditional results from a comparison of two values The result of the conditional is 'true' or 'false' Boolean values A boolean value is either 'true' or 'false' Many languages have a separate boolean data type However, the C language does not have a boolean type C uses the int type 0 is interpreted as 'false' 1 is interpreted as 'true' (or any other non-zero value)

10 Relational and Equality Operators
Table 4.1

11 Sample Conditions Given these values x = power is MAX_POW is y is 7 item is MIN_ITEM is num is mom_or_dad is 'M' SENTINEL is 999 Table 4.2

12 Logical Operators Used to modify and combine conditional expressions
&& means 'and' (temperature > 90) && (humidity > 0.90) || means 'or' (salary < MIN_SALARY) || (dependents > 5) ! means 'not' !(0 <= n && n <= 100)

13 Operator Precedence Table 4.6

14 Character Comparisons
Characters are encoded as numerical values Therefore, the numerical values can be compared Table 4.8

15 The 'if' Statement Unit 4: Decision Structures

16 Single-Sided Branch (Flowchart Review)

17 The 'if' Statement Begins with keyword if
Followed by conditional expression in parentheses Conditional expression represents the question A statement follows the condition The statement is only executed if the condition is true The affected statement is indented if (taxRate != 0.0) tax = price * taxRate;

18 The Compound Statement
To conditionally execute multiple statements at once Enclose them in braces This forms a compound statement if (stopButtonPressed) { motor = 0; /* Stops motor */ alarmLight = 1; /* Lights alarm lamp */ }

19 Double-sided Branch (Flowchart Review)

20 The 'if-else' Statement Begins with keyword if, followed by condition, and statement or compound statement Next comes keyword else Finally, another statement or compound statement If the first statement is not executed, the statement following else is executed (one or the other) if (x != 0) y = z / x; else printf("Can't divide by zero!\n");

21 The 'if-else' With Compound Statements
if (ctri <= MAX_SAFE_CTRI) { printf("Car #%d: safe\n", auto_id); safe = safe + 1; } else { printf("Car #%d: unsafe\n", auto_id); unsafe = unsafe + 1; } Example 4.13 p 175

22 Alternate Denser Formatting
if (condition) { statement; } else if (condition) { statement; } else { }

23 Nested if Statements (Version 1)
if (x > 0) num_pos = num_pos + 1; else { } if (x < 0) num_neg = num_neg + 1; else /* x equals 0 */ num_zero = num_zero + 1; Example 4.15 p 192

24 Nested if Statements (Version 2)
if (x > 0) { num_pos = num_pos + 1; } else { } if (x < 0) { num_neg = num_neg + 1; } else { /* x equals 0 */ num_zero = num_zero + 1;

25 Cascaded if Statements
if (x > 0) { num_pos = num_pos + 1; } else if (x < 0) { num_neg = num_neg + 1; } else { /* x equals 0 */ num_zero = num_zero + 1; }

26 'if-else' Statement Example (cut-and-paste)
#include <stdio.h> int main(void) { int number; printf("Enter a number: "); scanf("%d", &number); if (number < 0) { printf("%d is negative\n", number); } else printf("%d is non-negative\n", number); return(0);

27 Cascading 'if' Statement Example (cut-and-paste)
#include <stdio.h> int main(void) { int number; printf("Enter a number: "); scanf("%d", &number); if (number > 0) { printf("%d is positive\n", number); } else if (number == 0) { printf("%d is zero\n", number); } else { printf("%d is negative\n", number); } return(0);

28 The 'switch' Statement Unit 4: Decision Structures

29 Selection Structure Selection structure is an extension of the branch
All branches must converge together at the end

30 The 'switch' Statement Begins with keyword switch
Followed by parenthesized integer value and compound statement Compound statement, called switch body, contains cases Each case keyword is followed by a constant value and a colon Two or more cases cannot have the same value Each case contains statement(s) and ends with keyword break If a case matches, its statements will be executed If no case matches The default case will be executed If no default case, nothing is executed

31 Switch Statement Example (cut-and-paste)
#include <stdio.h> int main(void) { int selection; printf("Enter selection: "); scanf("%d", &selection); switch (selection) { case 1: printf("ONE\n"); break; case 2: printf("TWO"\n"); default: printf("Not one or two\n"); } return (0);

32 Switch Statement Example (cut-and-paste)
#include <stdio.h> int main(void) { int selection; printf("Enter selection: "); scanf("%d", &selection); switch (selection) { case 1: printf("ONE\n"); break; case 2: printf("TWO"\n"); default: printf("Not one or two\n"); } return (0);

33 Tips for Creating Good Programs
Unit 4: Programming Practices

34 Garbage In, Garbage Out This well-known phrase captures the fact that programs do not produce good results when the input is “bad” “Bad” input is input that does not meet the assumptions of the programmer To counter this phenomenon, a program must perform input validation (checking of assumptions)

35 Input Validation Input validation means checking input data for “correctness” Examples: A circle’s radius cannot be negative Numbers cannot be arbitrarily large A voltage may be negative, but a resistance cannot be negative The action taken for invalid data varies with the situation Don’t perform calculations with invalid data Provide an error message that helps user correct the problem

36 Example of Input Validation (cut-and-paste)
#include <stdio.h> int main(void) { double dist_in_miles, dist_in_kms; printf("Convert miles to kilometers\n"); printf("Please enter the distance in miles: "); scanf("%lf", &dist_in_miles); if (dist_in_miles < 0) { printf("A distance cannot be negative.\n"); } else { dist_in_kms = * dist_in_miles; printf("The distance in kilometers is: "); printf("%f\n", dist_in_kms); } return (0);

37 Using the C Language Wisely
Although C permits an if-statement with optional braces if (condition) statement1; next_statement; It is wiser to always use the braces if (condition) { statement1; } next_statement; That way, you will avoid …

38 A Common Programming Error
When you add a statement and forget to add the braces if (condition) statement1; statement2; /* Added */ next_statement; The statement2 looks like it is part of the if statement … But, it is not! There are no braces! Programs are changed all the time. Plan for maintenance!

39 Learning to Avoid and Correct Programming Errors
Unit 4: Programming Errors

40 Syntax Errors Code violates a grammar rule of C
Compiler locates syntax errors during translation Examples: Missing semicolon Undeclared or misspelled variables Unclosed comments Mismatched open and close braces, quotation marks, or parentheses

41 Runtime Error Detected during program execution
Caused by an attempt to perform an illegal operation Using invalid data Sending invalid data to library functions Examples Divide by 0 a / 0 Remainder with 0 a % 0 Logarithm of negative log(-5)

42 Logic Errors Errors in the algorithm and/or flowchart
Causes program to give incorrect results Avoid by desk-checking algorithm before coding Finding a logic error can require changing the algorithm and the program!

43 Beware These Common Errors!!
Using int constants when double is intended C = (5/9) * (F - 32); Since (5/9) is calculated as an int, it results in 0 Using assignment when equality is intended if (x = 4) … Equality is two equal signs, not one! The single equal sign causes assignment, x is changed to 4!


Download ppt "Introduction to C Programming"

Similar presentations


Ads by Google