HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
Week2 ch4 part1edited 2020
1. Agenda
1- Warm up Listen to this video about motivating
programmers 5 min
2- ppt teacher demonstrate about loops 20 min
3- Video about how we can use c programming in rea
life projects , automation and solving problems 3 min
4-Students play or act a game about simulating loops
10 min
5- practical work using lap tops through code blocks i
implement a small program work in pairs 10 min
6- use netacade.com to self learn C and get certificate
from CISCO 5 min
7- Questions and answers 20 min
8-Refelection 5 min
9- Home work
2. LO CS.2.02 - Design
programs involving
decision structures
and loops.
Lesson plan
3. Warm Up
Listen to this video
motivating programmers
Open Code blocks or
www.Jdoodle.com on
line
4. Essential Question
1- why we use computers ?
2- what are the tasks , jobs
which can be computerized ?
3- what are Repetitions or loops
4- Distinguish between
while and do while and for
loop
8. 8
Repetition Statements
• Repetition statements allow us to
execute a statement or a block of
statements multiple times
• Like conditional statements, they are
controlled by boolean expressions
• C has three kinds of repetition
statements:
while
Do while
for
9. Types of Loops
• Pretest - a logical condition is checked
before each repetition to determine if
the loop should terminate
–while loop
–for loop
• Posttest - a logical condition is
checked after each repetition for
termination
–do-while loop
10. PreTest vs. PostTest Loops
Pretest Loop
Condition
Action or
Actions
true
f alse
Posttest Loop
Condition
Action or
Actions
true
f alse
11. 11
The while Statement
• A while statement has the following syntax:
• If the condition is true, the statement is
executed
• Then the condition is evaluated again, and if it is
still true, the statement is executed again
• The statement is executed repeatedly until the
condition becomes false
while ( condition )
statement;
12. Logic of a while Loop
statement
true false
condition
evaluated
5
13. 13
The while Statement
• An example of a while statement:
• If the condition of a while loop is false
initially, the statement is never executed
• Therefore, the body of a while loop will
execute zero or more times
int count = 0;
while (count < 2)
{
printf("Welcome to C programming!");
count++;
}
14. Trace while Loop
int count = 0;
while (count < 2)
{
printf("Welcome to C programming!");
count++;
}
Initialize count
animation
7
15. Trace while Loop, cont.
int count = 0;
while (count < 2)
{
printf("Welcome to C programming!");
count++;
}
(count < 2) is true
animation
8
16. Trace while Loop, cont.
int count = 0;
while (count < 2)
{
printf("Welcome to C programming!");
count++;
}
Print Welcome to Java
animation
9
17. Trace while Loop, cont.
int count = 0;
while (count < 2)
{
printf("Welcome to C programming!");
count++;
}
Increase count by 1
count is 1 now
animation
10
18. Trace while Loop, cont.
int count = 0;
while (count < 2)
{
printf("Welcome to C programming!");
count++;
}
(count < 2) is still true since count
is 1
animation
11
19. Trace while Loop, cont.
int count = 0;
while (count < 2)
{
printf("Welcome to C programming!");
count++;
}
Print Welcome to C programming
animation
12
20. Trace while Loop, cont.
int count = 0;
while (count < 2)
{
printf("Welcome to C programming!");
count++;
}
Increase count by 1
count is 2 now
animation
13
21. Trace while Loop, cont.
int count = 0;
while (count < 2)
{
printf("Welcome to C programming!");
count++;
}
(count < 2) is false since count is 2
now
animation
14
22. Trace while Loop
int count = 0;
while (count < 2)
{
System.out.println("Welcome to Java!");
count++;
}
The loop exits. Execute the next
statement after the loop.
animation
15
23. Posttest Loop: Do-While
Syntax:
do {
statement(s)
} while (condition);
Corresponds to:
statement
if (!condition) DONE
statement
if (!condition) DONE
...
condition
statement(s)
true
f alse
24. Using the Do-While
do {
printf(“Enter id# and salary: “);
scanf(“%d %f”,&id,&salary);
printf(“You entered id#%1d and salary
$%.2f, Is this correct? (Y/N) “
,id,salary);
scanf(“ %c”,&ans);
} while (!((ans == ‘Y’) || (ans == ‘y’)));
• Loop always executes at least once
25. While vs Do-While
• Differences
–where condition tested:
• while (first) - may execute 0 times
• do-while (last) - must execute at least one
time
• Similarities
–one statement executed
–initialization before loop
–update during loop
26. Pretest Loop: For
• Initialization included in loop header
• Update included in loop header
• Header also includes update
• Syntax:
for ( init ; condition ; update )
statement;
• Generally for loops expected to
execute fixed number of times
(counter-controlled)
27. For Loop
• Syntax:
for ( init ;
condition ;
update )
statement;
• Init: assignments to
counter variables
• Update: changes to
counter variables
Condition
Statement
true
f alse
Init
Update
28. For Example
• Printing vertical line
of stars:
for (counter = 0;
counter < 5;
counter++)
printf(“*n”);
counter < 5
printf ("*n");
true
f alse
counter = 0;
counter++;
29. For Example - Sum of 1 to N
printf(“Number to sum to: “);
scanf(“%d”,&N);
total = 0;
for (I = 1; I <= N; I++)
total += I;
/* total is now 1 + 2 + … + N */
30. Are we ready to code it?
http://www.stemassiut.info prepared By Mr. Osama Ghandour
30
33. For Example - Max of N Scores
printf(“Number of students: “);
scanf(“%d”,&NumStudents);
for (I = 0; I < NumStudents; I++) {
printf(“Enter student score %d: “);
scanf(“%d”,&score);
if (score > max)
max = score;
}
/* max is highest score entered */
34. Listen to this video
about programming
Arduino
Off line
On line
35. Directions of Counting
for (I = 10; I >= 1; I--)
printf(“%d…n”,I);
printf(“0 BLASTOFF!n”);
printf(“Enter start, end, inc values: “);
scanf(“%d%d%d”,&lstart,&lend,&linc);
for (I = lstart;
((linc < 0) && (I < lend)) ||
((linc > 0) && (I > lend));
I += linc)
printf(“%dn”,I);
36. practical work using lap
tops through code blocks
in implement a C small
program work in pairs 20
min
40. Reflection
• What is your goal to accomplish in
next week End Using loops in C
programming Language ?
41.
42. students create in small
groups a c program
related to their capstone
project .
Home work
+ =
C program
Arduino
Arduino hardware
controller
components of irradiation
system
43. Home Work
Discuss how to use
Loops in coding
different programs
in a video by using
office mix. 43
44. Resources
• watch a video that explain different ways
for loops and read in the following links :
https://www.youtube.com/watch?v=Rtww8
3GH0BU
44