0% found this document useful (0 votes)
56 views32 pages

EEE 121 Structured Programming Language: Instructor: Moonmoon Shanta Fall 2013

The document contains lecture notes on conditional statements, loops, and other structured programming concepts from a course titled EEE 121 Structured Programming Language. It provides examples of using if/else statements, switch statements, while loops, do-while loops, and the goto statement to perform tasks like range checking, reading input, and repetition. Key examples include checking if a number is within a given range, getting letter grades as input and printing descriptions, and summing grades entered by a user until a sentinel value is read.

Uploaded by

Assasinator Faz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views32 pages

EEE 121 Structured Programming Language: Instructor: Moonmoon Shanta Fall 2013

The document contains lecture notes on conditional statements, loops, and other structured programming concepts from a course titled EEE 121 Structured Programming Language. It provides examples of using if/else statements, switch statements, while loops, do-while loops, and the goto statement to perform tasks like range checking, reading input, and repetition. Key examples include checking if a number is within a given range, getting letter grades as input and printing descriptions, and summing grades entered by a user until a sentinel value is read.

Uploaded by

Assasinator Faz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

EEE 121 Structured Programming Language

Instructor: Moonmoon Shanta Fall 2013 Lecture 4: Conditional statements: switch Loops: while, do-while

if (range checking - take 1)


int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); if (n > 10) { printf(Thats not in range!"); } else if (n < 1) { printf(Thats not in range!"); } else { printf("Good job!"); }

12/29/2013

Structured Programming Language: Lecture 5

if (range checking - take 2)


If there is only one statement needed for the true and/or false condition, the {} are not needed

int n;
printf("Enter a number 1 to 10: "); scanf("%d",&n); if (n > 10) printf(Thats not in range!"); else if (n < 1) printf(Thats not in range!"); else printf("Good job!");

12/29/2013

Structured Programming Language: Lecture 5

if (range checking - take 3)


Use the && or || as needed to check for multiple conditions int n;

printf("Enter a number 1 to 10: "); scanf("%d",&n);


if ( (n > 10) || (n < 1) ) printf(Thats not in range!"); else printf("Good job!");

12/29/2013

Structured Programming Language: Lecture 5

if (range checking - take 4)


Use the && or || as needed to check for multiple conditions int n;

printf("Enter a number 1 to 10: "); scanf("%d",&n);


if ( (1 <= n) && (n <= 10) ) printf(Good job!"); else printf(Thats not in range!");

12/29/2013

Structured Programming Language: Lecture 5

if (range checking) (The WRONG WAY)


int n;

printf("Enter a number 1 to 10: "); scanf("%d",&n);


if (1 <= n <= 10 ) // THIS WILL PRODUCE WRONG OUTPUT printf("Good job!"); else printf(Thats not in range!");

12/29/2013

Structured Programming Language: Lecture 5

Example solution

Given int x, check its value

If x is greater than 5 and less than or equal to 10, print x

if ((x > 5) && (x <= 10)) printf(%d\n, x);

12/29/2013

Structured Programming Language: Lecture 5

Example solution (cont.)

Prompt for and read temperature as input (type double)


If temp is 90 or higher, print Its too hot! If temp is 32 or lower, print Its freezing! In all other cases, print Its okay

int main() { double temp; printf(Enter temperature: ); scanf(%lf, &temp); if (temp >= 90) printf(Its too hot!\n); else if (temp <= 32) printf(Its too cold!\n); else printf(Its okay\n); return 0; }
12/29/2013

Structured Programming Language: Lecture 5

switch statements

Nesting several if/else if statements can get tedious If each condition is simply checking equality of same variable or expression, can use switch

12/29/2013

Structured Programming Language: Lecture 5

switch/case statement - General form


switch ( <expression> ) { case <value1> : <statements> [ break; ] case <value2> : <statements> [ break; ] . . . [ default: <statements> [ break; ] ] }

12/29/2013

Structured Programming Language: Lecture 5

switch/case statement

Check if <expression> matches any value in case statements

If <expression> == <value1>, execute <statements> in that case If <expression> == <value2>, execute <statements> in that case

If <expression> does not equal any of the values, go to default case (if present)

12/29/2013

Structured Programming Language: Lecture 5

Switch statements and break


Each case is just a starting pointswitch does not automatically skip other cases! Example: switch (x) { case 0: x = 3; case 1: x = x * 4; default: x = x 1; } If x == 0:

Start at case 0 x = 3; Then, go to case 1 x = x * 4 = 3 * 4 = 12 Then, go to default: x = x 1 = 12 1 = 11

12/29/2013

Structured Programming Language: Lecture 5

Switch statements and break


Use break to exit at end of case

You may not always want to use breakwill see examples later

Rewriting previous example: switch (x) { case 0: x = 3; break; case 1: x = x * 4; break; default: x = x 1; }

12/29/2013

Structured Programming Language: Lecture 5

switch/case statement - example


#include <stdio.h> int main() { char grd; printf("Enter Letter Grade: "); scanf("%c",&grd); printf(You are "); // continued next slide

12/29/2013

Structured Programming Language: Lecture 5

switch/case statement - example


switch (grd) { case 'A' : printf("excellent"); break; case 'B' : printf("good"); break; case 'C' : printf("average"); break; case 'D' : printf("poor"); break; case 'F' : printf("failing"); break; default : printf(incapable of reading directions"); break; } return 0;

12/29/2013

Structured Programming Language: Lecture 5

Example: switch statement

What does the program on the previous slides print if the user enters:

A B+ c X

Recognize, of course, that it always prints: Enter Letter Grade:

12/29/2013

Structured Programming Language: Lecture 5

Example solution

What does the program on the previous slides print if the user enters:

You are excellent Only first character is readB You are good This program is case-sensitiveC and c are two different characters!

B+

Will go to default case

You are incapable of reading directions No case for Xgoes to default case You are incapable of reading directions
Structured Programming Language: Lecture 5

12/29/2013

switch/case statement - Alt example


switch (grd) { case 'A' : case 'a': case 'B' : case 'b': printf("doing very well"); break; case 'C' : case 'c': case 'D' : case 'd': printf("not doing too well"); break; case 'F' : case f': printf("failing"); break; default : printf("incapable of reading directions"); break; } return 0; }
12/29/2013

Structured Programming Language: Lecture 5

The goto Statement

It is capable of jumping to any statement in a function, provided that the statement has a label. Useful when jumping out of nested loops as break only jumps out of a single loop.

12/29/2013

Structured Programming Language: Lecture 6

19

Example: goto
for (d = 2; d < n; d++) if(n % d == 0) goto done; .... .... done: if(d < n) printf (%d is divisible by %d\n, n, d); else printf (%d is prime\n, n);

12/29/2013

Structured Programming Language: Lecture 6

20

while loops

Previous program does same thing 11 times Repetitive code can be captured in a loop
Much less code to do same amount of work Simplest form: while loop while (<expression>) <statement> loop body

Loop body will repeat as long as <expression> is true

Loop body must therefore change expression If multiple lines, need { } to denote block

<statement> may be one or more lines

12/29/2013

Structured Programming Language: Lecture 5

while loops - example


x = 7; while ( x < 10 ) { printf("%d ",x); x = x + 1; } OUTPUT:

7 8 9

12/29/2013

Structured Programming Language: Lecture 5

while loops - example


x = 7; while ( x < 3 ) { printf("%d ",x); x = x + 1; } OUTPUT:

(no output)

Possible to have while loop body that never executes!

12/29/2013

Structured Programming Language: Lecture 5

Repetition with while loop

Rewriting previous program with loop

int main() { int i; int iSquared; printf(" i

// Number to square // Square of the number i^2\n"); // Column headings

// Compute and display the squares of numbers 0 to 10 i = 0; // Initialize i while (i <= 10) { // Loop until i > 10 iSquared = i * i; printf("%2d%10d\n", i, iSquared); i = i + 1; // Increment i } return 0; }
12/29/2013

Structured Programming Language: Lecture 5

Application: loop with flexible limit

Could determine loop limit based on variable

Result of calculation Input value

while (gradeCount < numGrades) { scanf("%lf", &grade); // Read grade gradeSum = gradeSum + grade; // Add to sum gradeCount = gradeCount + 1; // Inc. count }

12/29/2013

Structured Programming Language: Lecture 5

Application: sentinel value

Common to read input until a certain value(sentinel) is entered


May be predetermined (i.e., run program until user enters q for quit) Run until invalid value entered In file input, will often run until end of file

// Prompt for and read first grade printf("Enter grade: "); scanf("%lf", &grade);
/* Continue reading/accumulating grades until invalid value entered */ while ((grade >= 0.0) && (grade <= 100.0)) { gradeSum = gradeSum + grade; // Accumulate grade gradeCount = gradeCount + 1; // Increment grade count printf("Enter grade: "); // Prompt for and scanf("%lf", &grade); // read next grade }

12/29/2013

Structured Programming Language: Lecture 5

do-while loops

while loop is pre-tested

Check condition at start; if false, dont enter loop

To guarantee at least one iteration, use posttested loop: do-while

Checks condition at end of loop

do { <statements> } while ( <expression>

);

Dont forget semicolon!


12/29/2013

Structured Programming Language: Lecture 5

While vs. do-while: flowcharts

while: pre-tested loop

do-while: post-tested loop

Check condition, then execute loop body


FALSE

Execute loop body, then check condition


Loop body
TRUE

A = 0?
TRUE

A = 0?
FALSE

Loop body

All loops characterized by conditional test, backwards arrows indicating repetition of code
Structured Programming Language: Lecture 5

12/29/2013

Comparison while vs do-while


x = 7; do { printf("%d ",x); x = x + 1; } while ( x < 10 ); x = 7; while ( x < 10 ) { printf("%d",x); x = x + 1; } OUTPUT: 789

OUTPUT: 789

12/29/2013

Structured Programming Language: Lecture 5

Comparison while vs do-while


x = 7; do { printf("%d",x); x = x + 1; } while ( x < 3 ); x = 7; while ( x < 3 ) { printf("%d",x); x = x + 1; } OUTPUT: (no output)

OUTPUT: 7

12/29/2013

Structured Programming Language: Lecture 5

Application: sentinel value

Core of program demonstrating while loop

// Prompt for and read first grade printf("Enter grade: "); scanf("%lf", &grade); /* Continue reading/accumulating grades until invalid value entered */ while ((grade >= 0.0) && (grade <= 100.0)) { gradeSum = gradeSum + grade; // Accumulate grade gradeCount = gradeCount + 1; // Increment grade count printf("Enter grade: "); // Prompt for and scanf("%lf", &grade); // read next grade }

12/29/2013

Structured Programming Language: Lecture 5

Application: sentinel value


Rewrite grade average program to ensure at least one grade is read Change core of program (shown previously):

/* Prompt for and read grades until invalid value entered */ do { printf("Enter grade: "); // Prompt for and scanf("%lf", &grade); // read grade
if ((grade >= 0.0) && (grade <= 100.0)) { gradeSum = gradeSum + grade; // Accumulate grade gradeCount = gradeCount + 1; // Inc. grade count } } while ((grade >= 0.0) && (grade <= 100.0));

12/29/2013

Structured Programming Language: Lecture 5

You might also like