switch code for lab 4.2 switch (input) { /* input is a variable that we will test. */ case...
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/1.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/2.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/3.jpg)
![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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/4.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/5.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/6.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/7.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/8.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/9.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/10.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/11.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/12.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/13.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/14.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/15.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/16.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/17.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/18.jpg)
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':](https://reader035.vdocuments.us/reader035/viewer/2022062618/55150c48550346a80c8b5925/html5/thumbnails/19.jpg)
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.