1 / 18

CSCE 206 Lab Structured Programming in C

Learn about for, while, and do-while loops, break and continue statements, math library usage, and practice problems in C programming.

latonyaj
Download Presentation

CSCE 206 Lab Structured Programming in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCE 206 Lab Structured Programming in C SPRING 2019 Lecture 4 Loops

  2. Today’s objective • Iteration • for loop • while loop • do-while loop • break and continue • Including library • math.h

  3. for loop

  4. Sample Code-1: for • This code will print out 1,2 and 3 in order, each in a new line • i++ refers to increment by 1. It can also be written as i=i+1 (This is an assignment operation, not an equation.So don’t remove ifrom both sides and say “0=1”) • You can perform increment or decrement by any value. • Nested for loops are helpful for complex problems • Look out for infinite loops, although they are useful in some special cases.

  5. while loop

  6. Sample Code-2: while • This code will print out 1,2 and 3 in order, each in a new line. Same as the previous code • Initializations are done outside the loop body unlike for loop • Nested while loops are helpful for complex problems

  7. do-while loop

  8. Sample Code-3: do-while • This code will print out 0,1,2 and 3 in order, each in a new line. Almost same as the previous code • do-while loops executes at least once as it does not check the condition at the beginning

  9. break and continue Output: 0 1 2 3 5 6 7 8 Continue to the next iteration (jump) • Output: 0 1 2 3 • (stop the loop)

  10. Built-in Math Library • There is a built-in math library in C, which has useful functions like pow, sqrt,log10 etc. • Type #include <math.h> before the main function. • Example: pow(base, exponent) calculates the value • Make sure you type –lmat the end of your compiling command. Otherwise it will give error messages.Command should be gccmycode.c –o mycode -lm

  11. Problems: for, while, do-while loops Fill this Google form as you finish each problem: https://goo.gl/forms/GzzWI4eI0tSlJPqz1 Don’t forget to answer the last question!

  12. Practice Problem 1 • Write a C program that gets an integer from the input and outputs the first 10 numbers after that one (separated by a space). • Example • Input: 5 • Output: 6 7 8 9 10 11 12 13 14 15

  13. Practice Problem 2 • Write a C program that gets an integer from the input and outputs the previous 10 numbers after that one (separated by a space). • Example • Input: 22 • Output: 21 20 19 18 17 16 15 14 13 12

  14. Practice Problem 3 • Write a C program that outputs the first 100 even numbers (separated by a space), starting from 1. • Example • Output: 2 4 6 8 …

  15. Practice Problem 4 • Write a C program that outputs the first 100 numbers saying if it is even or odd (separated by a comma), starting from 1. • Example • Output: 1 is odd, 2 is even, 3 is odd, 4 is even, …

  16. Practice Problem 5 • Write a C program that gets an integer from the input and outputs the multiplication table (0 to 10). • Example • Input: 5 • Output: 5 x 0 = 0 • 5 x 1 = 5 • 5 x 2 = 10 …

  17. Practice Problem 6 • Write a C program that gets an integer from the input and outputs the number of digits that the integer has. • Example • Input: 1234 • Output: 4 • Tip: you might need break/continue

  18. Practice Problem 7 (Bonus!) • Write a C program that gets an integer from the input and outputs the multiplication table (0 to 10). Ask if the user wants to input another number, if so, print again the multiplication table for that number. Stop when the user does not want to input more numbers. • Example • Input: 5 • Output: 5 x 0 = 0 • 5 x 1 = 5 • 5 x 2 = 10 …

More Related