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

Post on 27-Mar-2015

214 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

}

UNIT 5ET156

Introduction to C Programming

Sample Conditions

Table 4-2

Figure 5.1

Flow Diagram of Loop Choice Process

Types of Loops

Table 5.1

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!

Compound Assignment Operators

Increment: i = i + 1;

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

Decrement: i = i -1;

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

Figure 5.6

Comparison of Prefix and Postfix Increments

while Loop Stylewhile (condition is true) {

Statements to be repeated;Counter updated;

}

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

count++;

}

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;

Perform a known number actions with a while Loop

product=0; multiplier=5;

multiplicand=3; ctr=0;

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

count++;

}

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;

}

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;

Perform a known number actions with a for Loop

product=0; multiplier=5;

multiplicand=3;

for (i=0;

i < multiplier;

i++;){ product += multiplicand;

}

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.

Debugger

Debugger allows:Single-step executionSetting breakpoints on a

statementVariable inspections

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

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.

top related