0% found this document useful (0 votes)
237 views41 pages

COMP 2511: C Programming Lesson 2: Review of Control Structures

The document discusses various C programming control structures including loops, logical operators, and conditional statements. It provides examples and assignments for students to practice using for loops, while loops, do-while loops, if/else statements, switch statements, and break and continue statements. The key topics covered are counter-controlled repetition with for loops, sentinel-controlled repetition with while and do-while loops, logical operators like && and ||, and using break and continue to control loop execution. Ten practice problems are provided to reinforce using these control structures.

Uploaded by

serachguru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
237 views41 pages

COMP 2511: C Programming Lesson 2: Review of Control Structures

The document discusses various C programming control structures including loops, logical operators, and conditional statements. It provides examples and assignments for students to practice using for loops, while loops, do-while loops, if/else statements, switch statements, and break and continue statements. The key topics covered are counter-controlled repetition with for loops, sentinel-controlled repetition with while and do-while loops, logical operators like && and ||, and using break and continue to control loop execution. Ten practice problems are provided to reinforce using these control structures.

Uploaded by

serachguru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

COMP 2511

C PROGRAMMING

LESSON 2: REVIEW OF CONTROL STRUCTURES


At 6:10 PM, you can start Quiz One in D2L
(learn.bcit.ca)
Open our course instance, and then go to:
QUIZ TIME Activities >> Quizzes >> “Quiz 1”
You will have 10 minutes to do the quiz… Good luck~
Section 1

• 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

Counter-controlled repetition is sometimes called definite repetition because we


know in advance exactly how many times the loop will be executed. Sentinel-
controlled repetition is sometimes called indefinite repetition because it’s not
known in advance how many times the loop will be executed.
IN COUNTER-CONTROLLED REPETITION
In counter-controlled repetition, a control variable is used to count the number of
repetitions. The control variable is incremented (usually by one) each time the group of
instructions is performed. When the value of the control variable indicates that the correct
number of repetitions has been performed, the loop terminates, and the computer continues
executing with the statement after the repetition statement.
IN SENTINEL-CONTROLLED REPETITION
Sentinel values are used to control repetition when:

1. The precise number of repetitions is not known in advance, and


2. The loop includes statements that obtain data each time the loop is performed.

The sentinel value indicates “end of data.”


The sentinel is entered after all regular data items have been supplied to the program.
Sentinels must be distinct from regular data items.
COUNTER-CONTROLLED REPETITION
Counter-controlled repetition requires:
1. The name of a control variable (or loop counter).
2. The initial value of the control variable.
3. The increment (or decrement) by which the control variable is modified each time
through the loop.
4. The condition that tests for the final value of the control variable (i.e., whether looping
should continue).
COUNTER-CONTROLLED REPETITION EXAMPLE

Example code Output


READY?
ASSIGNMENT 1 PART 2
Task 1: To make a program which reads in an integer from user, and then print ”hello” that
many times. (Eg, if reads in 5, print “hello” 5 times), using a while loop
BREAK TIME 10 mins break
FOR LOOP
FOR LOOP
Use case #1: Use case #3:
for ( ; ; ) {
statements
}

Use case #2: Use case #4:


for ( expression1; expression2; expression3) { for ( expression1; expression2;
expression3) ;
statements
}
OUR FIRST FOR LOOP
ANATOMY
FOR-LOOP V.S. WHILE-LOOP
SIDE NOTES (1/2)
SIDE NOTES (2/2)
FLOW CHART
ASSIGNMENT 1 PART 2
Task 2: To make a program which reads in an integer from user, and then print all positive
even numbers up to the user’s input(Eg, if reads in 5, print 2 4), using a for loop
Task 3: To make a program which reads in an integer from user, and then print all numbers
between 1 and the read in value (inclusive) (Eg, if reads in 3, print 1 2 3), using a for loop
DO-WHILE LOOP
DO-WHILE LOOP
Use case #1:

Use case #2:


EXAMPLE
WHILE-LOOP V.S. DO-WHILE-LOOP

≥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

You might also like