CSCE 206 Lab Structured Programming in C FALL
CSCE 206 Lab Structured Programming in C FALL 2018 LEC TU RE 4 EHSANU L HAQU E NIRJHAR
Today’s objective §Iteration §for loop §while loop §do-while loop §break and continue §Including library §math. h
for loop
Sample Code-1 §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 i from 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.
while loop
Sample Code-2 §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
do-while loop
Sample Code-3 §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
break and continue Output: 0 1 2 3 5 6 7 8
Built-in Math Library
- Slides: 10