0% found this document useful (0 votes)
12 views

03 - Loop

Uploaded by

dhibhan30
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)
12 views

03 - Loop

Uploaded by

dhibhan30
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/ 22

Introduction to C Programming

CT018-3-1-ICP

Loop
Subject/Module Duration

W1 W2 W3 W4 W5 W6 W7 W8 W9 W10 W11 W12 W13 W14 R E

Weekly Exercises Group Final


(15%) Assignment Exam
(35%) (50%)

CT077-3-2-DSTR Overview of C++ (Part 1) 2


What we have learnt so far . .

HEADER FUNCTIONS ETC


#include <stdio.h> main() Compiler
printf() Va r i a b l e s
scanf() M a t h s / L o g i c a l O p r.
if()
conditional
if()..else
statement
if()..elseif..else

CT077-3-2-DSTR Overview of C++ (Part 1) 3


What we have learnt so far . .

HEADER FUNCTIONS ETC


#include <stdio.h> main() Compiler
printf() Va r i a b l e s
scanf() M a t h s / L o g i c a l O p r.
if()
conditional
if()..else
statement
if()..elseif..else
for()
loop while()
do..while()

CT077-3-2-DSTR Overview of C++ (Part 1) 4


Loop

A loop executes many times


until the condition becomes false

starting point
Every loop has a ending point
a counter (asc / desc)

CT077-3-2-DSTR Overview of C++ (Part 1) 5


Loops

1200m

301

CT077-3-2-DSTR Overview of C++ (Part 1) 6


Loop

1. f o r l o o p ( start, end point)

2. w h i l e l o o p ( start, end point)

3. d o . . w h i l e l o o p ( start, end point)

CT077-3-2-DSTR Overview of C++ (Part 1) 7


Scenario 1 -
Loops
Create a program that enable users to run for seven (7) laps and end the
program after seven (7) laps

What loop to use?

For loop. Starting point – 1, Ending point - 7

CT077-3-2-DSTR Overview of C++ (Part 1) 8


Scenario 2 -
Loops
Create a program that enable users to keep running around the laps. End the
program once its start to rain.

What loop to use?

While loop. Starting point – 1, Ending point - undefined

CT077-3-2-DSTR Overview of C++ (Part 1) 9


Scenario 3 -
Create a program that enable users toLoops
keep running around the laps. End the
program once its start to rain. However, users need to complete at least one lap.

What loop to use?

Do..while loop. Starting point – 1, Ending point - undefined

CT077-3-2-DSTR Overview of C++ (Part 1) 10


1. F o r l o o p 2. While loop 3. Do..While

CT077-3-2-DSTR Overview of C++ (Part 1) 11


Loop

1. f o r l o o p

2. w h i l e l o o p

3. d o . . w h i l e l o o p

CT077-3-2-DSTR Overview of C++ (Part 1) 12


1. F o r l o o p

CT077-3-2-DSTR Overview of C++ (Part 1) 13


1. F o r l o o p

END
STARTIN
POINT
G
POINT

ASCENDING
DESCENDING

CT077-3-2-DSTR Overview of C++ (Part 1) 14


i++
equivalent to
i=i+1

CT077-3-2-DSTR Overview of C++ (Part 1) 15


Loop

1. f o r l o o p

2. w h i l e l o o p

3. d o . . w h i l e l o o p

CT077-3-2-DSTR Overview of C++ (Part 1) 16


2. while loop

CT077-3-2-DSTR Overview of C++ (Part 1) 17


Loop

1. f o r l o o p

2. w h i l e l o o p

3. d o . . w h i l e l o o p

CT077-3-2-DSTR Overview of C++ (Part 1) 18


3. do…while loop

CT077-3-2-DSTR Overview of C++ (Part 1) 19


QUESTION 1: QUESTION 2: QUESTION 3: QUESTION 4:
Create a program that Create a program that Create a program that Create a program that
will get user key in their will get user key in their will keep getting user will get user key in their
age and display their age and display their key in their age and age and display their age
age. age FIVE (5) times. display their age until once and then keep
user key in 0 to stop asking until user key in
the program. 0 to stop the program.

(without loop) (for loop) (while loop) (do-while loop)

CT077-3-2-DSTR Overview of C++ (Part 1) 20


CT077-3-2-DSTR Overview of C++ (Part 1) 21
Introduction to C Programming
CT018-3-1-ICP

Loop

You might also like