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

Module 3.1 - Introduction to Loops

Uploaded by

dorotheagrey25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Module 3.1 - Introduction to Loops

Uploaded by

dorotheagrey25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Cebu Institute of Technology – University

Computer Engineering Department


Module 3.1 – Introduction to Loops

Loops
In your previous course and module, you learned how to use the input and output functions as well as
the conditional flow structure specifically the if-else statements.

So, if you encounter a certain problem asking you to print a phrase 10 times, it is obvious that your first
solution is to repeatedly use and type the printf function in order to reach the expected result.

But what if you will be asked to do the same task, but instead of 10, print it a 1000 times. Is it still
possible to use the same approach? The answer would be yes, it would still be effective. Yet the real
question is, will it be efficient? That would totally be a no. Remember that each line of your code takes up
a certain portion in your memory space and adds unto your program's compiling time. You will not only
have a code that consumes almost a 1000 blocks of memory space but one that takes longer to compile
as well. Yes, this approach can reach the same intended output but there exists a method way better than
this.

That is where the use of loops/iteration comes in. Iteration or loops in programming is simply a
repetition of certain statement/s bound by a specific condition. It is a sequence of instruction/s that is/are
continually repeated until a certain condition is reached. So instead of repeatedly typing printf 1000 times,
we can just use a single one (block of code/statement) and put it in a loop set to repeat 1000 times. In this
way, you will be able to reduce your 1000-lined code into a line count of less than 10.

In a loop structure, there is what we call a "loop statement" which contains the block of codes to be
repeated, inside it, there exists a "counter variable". A counter variable's task is to ensure that a certain
loop is only repeated one at a time. This variable is created to count the number of repetitions that were
already done. It is usually incremented by 1 after each loop and continues until a condition is met. The
counter variable is essential to make sure that your loop wouldn’t be a never-ending or infinite loop.

There are several types of loops, and you will also learn on what type to use best on certain situations.
Take note that every loops can be transformed into one another, meaning all types can be used to solve
the problem, but on what to use best depends on your own decision based on the loops' characteristics,
advantages and disadvantages.

A loop has the following components:

1. Initialization of variables – setting an initial value of zero before the loop statement is reached.
2. Condition/testing – (that would evaluate to either true or false) the condition to check is usually
made on the current value of the variable initialized in (1) for it controls the loop repetition.
3. Body – statements that are repeated in the loop.
4. Updating – a statement inside the body of the loop that updates the value of the variable that is
initialized.
2 Types of Loop Structures:
• do-while statement
• while statement
• for statement
Components of a Loop:
• Initialization
• Condition
• Body
• Update

Engr. Jundith D. Alterado


[email protected]
Cebu Institute of Technology – University
Computer Engineering Department
Module 3.1 – Introduction to Loops
Increment and Decrement Operators

Increment (++) Decrement (--)

- adds 1 to the variable - subtracts 1 to the variable

- equivalent to the expression: - equivalent to the expression:

given variable x = 0; given variable x = 0;

x = x + 1; //x is now equal to 1 x = x - 1; // x is now equal to -1

- examples: - examples:
Two ways to use increment or decrement:

1. Prefix increment/decrement
- Place the operator before the variable
- the value of the expression is the value of the variable after incrementing
-expression: ++x or --x
2. Postfix increment/decrement (most commonly used)
- Place operator after the variable
- the value of the expression is the value of the variable before it is incremented.
- the variable would only be updated on the next line of code.
- expression: x++ or x--

Prefix VS Postfix

Prefix Postfix
Example #1: (update a variable) Example #1: (update a variable)
Conditional Expressions – basis/condition which determines your program’s output. Faulty expressions
can lead to logical errors. We can write conditional expressions
#include<stdio.h> #include<stdio.h>using conditional and logical operators.
#include<math.h>
Conditional Operators – evaluates either true or false #include<math.h>
and is used between two
main() main()
{ variables/constants/arithmetic expressions. {
int x = 5;Note: op1 and op2 can be variables/constants/arithmetic int x expressions.
= 5;
printf("%d", ++x); //at this line, x is already updated to 6. printf("%d",x++); //at this line, x is still 5.
Operator
printf("\n%d", x); //now it is updated to 6
Use Result
printf("\n%d", x); //now it is updated to 6
} > op1 > op2 } true if op1 is greater than op2
Sample Output: >=6 op1 >= op2 Sample Output:
true if op1 is greater 5 or equal to op2
6 6
< op1 < op2 true if op1 is less than op2
Example #2: (use the<= expression in anotherop1arithmetic
<= op2 expression) true if op1
Example is less
#2: (use or equalintoanother
the expression thanarithmetic
op2 expression)
== op1 == op2 true if op1 is equal to op2
#include<stdio.h>
#include<math.h> != op1 != op2 true if op1 is not equal to op2
#include<stdio.h>
#include<math.h>
main()
{
Examples: main()
//initialize variables with no definite value yet to 0. {
int a = 3, x = 5, y = 0, z = 0; //initialize variables with no definite value yet to 0.
y = a * ++x; //at this line x is immediately updated to 6 --> y = 3 * 6 int a = 3, x = 5, y = 0, z = 0;
z = a * x; //at this line x already updated to 6 y = a * x++; //at this line x is still equal to 5 → y = 3 * 5
printf("%d %d", y, z); //18 18 z = a * x; //at this line x already updated to 6
} printf("%d %d", y, z); //15 18
}

Engr. Jundith D. Alterado


[email protected]

You might also like