Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':

Similar presentations


Presentation on theme: "Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':"— Presentation transcript:

1 Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k': printf("The prefix is equal to 1E3.\n"); break; case 'm': printf("The prefix is equal to 1E-3.\n"); break; case 'u': printf("The prefix is equal to 1E-6.\n"); break; case 'n': printf("The prefix is equal to 1E-9.\n"); break; default: printf("Error! You must enter a valid amount.\n"); }

2 UNIT 5 ET156 Introduction to C Programming

3

4 Sample Conditions Table 4-2

5 Figure 5.1 Flow Diagram of Loop Choice Process

6 Types of Loops Table 5.1

7 Counter-Controlled Loops Used when you know the number of times the loop must execute General steps: 1. Set loop control variable to 0 2. Loop control variable < final value Execute statements Increase loop control variable by 1 For loop is best choice, but while loop will work!

8 Compound Assignment Operators Increment: i = i + 1; i += 1; ++I; i++; Decrement: i = i -1; i -= 1; --I; i--;

9 Figure 5.6 Comparison of Prefix and Postfix Increments

10 while Loop Style while (condition is true) { Statements to be repeated; Counter updated; } while (count <= 10){ Statements to be repeated; count++; }

11 Sum & Average with a while Loop count=0, sum=0; while (count < 10){ printf(Enter score: ); scanf(%d, &quiz); sum = sum + quiz; count++; } ave = sum / count;

12 Perform a known number actions with a while Loop product=0; multiplier=5; multiplicand=3; ctr=0; while (ctr < multiplier){ product += multiplicand; count++; }

13 for Loop Style for ( counter start status; condition is true; update counter; ) { Statements to be repeated; } for (i=0 ;i <= 10; i++){ Statements to be repeated; }

14 Sum & Average with a for Loop sum=0; for (i=0; i < 10; i++;){ printf(Enter score: ); scanf(%d, &quiz); sum = sum + quiz; } ave = sum / count;

15 Perform a known number actions with a for Loop product=0; multiplier=5; multiplicand=3; for (i=0; i < multiplier; i++;){ product += multiplicand; }

16 Off by One Errors This loop executes n+1 times: for (count = 0; count <= n; ++count) sum += count; Always test at the loop boundaries to verify the loop does the right thing.

17 Debugger Debugger allows: Single-step execution Setting breakpoints on a statement Variable inspections

18 Diagnostic Calls Use printf to output intermediate results. Define a constant named DEBUG and use a conditional: #define DEBUG 1 if (DEBUG) printf("*** score is %d, sum is %d\n", score, sum);

19 Common Errors Forgetting to use curly braces around multiple steps Termination condition never met (infinite loop) Mistyping an equality operator (==) as an assignment operator (=) Confusing do-while and while Mistakes related to operator precedence Avoid increment, decrement, and compound assignment operators in complex expressions. Use parentheses to control evaluation order.


Download ppt "Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':"

Similar presentations


Ads by Google