COMP 2511: C Programming Lesson 2: Review of Control Structures
COMP 2511: C Programming Lesson 2: Review of Control Structures
C PROGRAMMING
• Repetition Essentials
• Counter-Controlled Repetition
AGENDA Section 2
• for Loop
• do-while Loop
• Logical Operators
Section 3
• switch statement
• break and continue statement
REPETITION ESSENTIALS
A loop is a group of instructions the computer executes repeatedly while some loop-
continuation condition remains true. We have discussed two means of repetition:
• 1. Counter-controlled repetition
• 2. Sentinel-controlled repetition
≥1
𝑡𝑖𝑚𝑒
≥ 0𝑡𝑖𝑚𝑒
while loop
do-while loop
ASSIGNMENT 1 PART 2
Task 4: To make a program which keep reading in integers from user, and then print double
the value, until user put in a non-positive integer (Eg, if reads in 5, print 10; stop reading
only if got 0 or negative integer), using a do-while loop
Task 5: To make a program which keep reading in integers from user, and then print the
running sum, until the running sum is bigger than 100 (Eg, if reads in 5, print 5; after that,
reads in 10, print 15, stop reading only if the sum is over 100), using a do-while loop
LOGICAL OPERATORS
The logical operators are:
• && (logical AND)
• || (logical OR)
• ! (logical NOT)
ASSIGNMENT 1 PART 2
Task 6: make a program prints out numbers between 1 and 100: which are multiples of 3, or
multiples of 5, but NOT multiple of 15, using only ONE if statement
10 MINS BREAK TIME
SWITCH STATEMENT
SWITCH
EXAMPLE
CODE
Outputs:
ASSIGNMENT 1 PART 2
Task 7: make a program reads in one char, then print out the capital letter of it if the input
char is a vowel, otherwise print ‘X’, using a switch statement
Task 8: make a program reads in one integer, then print out “even number”, “positive odd
number” or “negative odd number” accordingly, using a switch statement
BREAK AND CONTINUE
BREAK STATEMENT
• In a loop:
• break statement will end the current loop
• No more execution
• Only the current level of the loop, if in a nested loop
• In a switch-statement
CONTINUE STATEMENT
In a loop,
• skip remaining steps, directly continue the loop for the next iteration
ASSIGNMENT 1 PART 2
Task 9: make a program reads in an integer, then print out all positive even numbers smaller
than this user input value, using a break statement to stop printing when the even value is
bigger than 15
Task 10: make a program reads in one integer, then print out all positive even numbers
smaller than this user input value, using a continue statement to skip those numbers which
are multiples of 6
== V.S. =
Do you see the difference?
UPDATED OPERATOR PRECEDENCE
OUR PROGRESS