switch code for lab 4.2 switch (input) { /* input is a variable that we will test. */ case...

19
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"); }

Upload: destiny-jenkins

Post on 27-Mar-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 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':

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");

}

Page 2: 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':

UNIT 5ET156

Introduction to C Programming

Page 3: 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':
Page 4: 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':

Sample Conditions

Table 4-2

Page 5: 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':

Figure 5.1

Flow Diagram of Loop Choice Process

Page 6: 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':

Types of Loops

Table 5.1

Page 7: 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':

Counter-Controlled LoopsUsed when you know the number of times

the loop must executeGeneral steps:

1. Set loop control variable to 02. Loop control variable < final value

Execute statements Increase loop control variable by 1

For loop is best choice, but while loop will work!

Page 8: 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':

Compound Assignment Operators

Increment: i = i + 1;

i += 1;++I;i++;

Decrement: i = i -1;

i -= 1;--I;i--;

Page 9: 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':

Figure 5.6

Comparison of Prefix and Postfix Increments

Page 10: 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':

while Loop Stylewhile (condition is true) {

Statements to be repeated;Counter updated;

}

while (count <= 10){Statements to be repeated;

count++;

}

Page 11: 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':

Sum & Average with awhile Loop

count=0, sum=0;

while (count < 10){ printf(“Enter score: “);

scanf(“%d”, &quiz);

sum = sum + quiz;

count++;

}

ave = sum / count;

Page 12: 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':

Perform a known number actions with a while Loop

product=0; multiplier=5;

multiplicand=3; ctr=0;

while (ctr < multiplier){ product += multiplicand;

count++;

}

Page 13: 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':

for Loop Stylefor ( counter start status;

condition is true;update counter; ) {

Statements to be repeated;

}for (i=0 ;i <= 10; i++){Statements to be repeated;

}

Page 14: 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':

Sum & Average with afor Loop

sum=0;

for (i=0; i < 10; i++;){ printf(“Enter score: “);

scanf(“%d”, &quiz);

sum = sum + quiz;

}

ave = sum / count;

Page 15: 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':

Perform a known number actions with a for Loop

product=0; multiplier=5;

multiplicand=3;

for (i=0;

i < multiplier;

i++;){ product += multiplicand;

}

Page 16: 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':

Off by One ErrorsThis 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.

Page 17: 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':

Debugger

Debugger allows:Single-step executionSetting breakpoints on a

statementVariable inspections

Page 18: 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':

Diagnostic Calls

Use printf to output intermediate results.

Define a constant named DEBUG and use a conditional:#define DEBUG 1if (DEBUG) printf("*** score is %d, sum is %d\n", score, sum);

Page 19: 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':

Common ErrorsForgetting to use curly braces around

multiple stepsTermination condition never met

(infinite loop)Mistyping an equality operator (==) as

an assignment operator (=)Confusing do-while and whileMistakes related to operator precedence

Avoid increment, decrement, and compound assignment operators in complex expressions.

Use parentheses to control evaluation order.