[go: up one dir, main page]

0% found this document useful (0 votes)
16 views51 pages

Topic 4

The document discusses different types of looping structures in programming: - While loops execute a block of code as long as a test expression is true, and include initialization, test expression, update expression, and body. - For loops allow specifying initialization, test condition, and update in one line to iterate a set number of times. - Switch statements allow choosing between multiple code blocks to execute based on a variable's value. - Do-while loops are similar to while loops but execute the body at least once even if test expression is false initially. Nested loops, break/continue statements, prefix/postfix increment/decrement operators are also covered. Examples are provided to illustrate the looping structures.
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)
16 views51 pages

Topic 4

The document discusses different types of looping structures in programming: - While loops execute a block of code as long as a test expression is true, and include initialization, test expression, update expression, and body. - For loops allow specifying initialization, test condition, and update in one line to iterate a set number of times. - Switch statements allow choosing between multiple code blocks to execute based on a variable's value. - Do-while loops are similar to while loops but execute the body at least once even if test expression is false initially. Nested loops, break/continue statements, prefix/postfix increment/decrement operators are also covered. Examples are provided to illustrate the looping structures.
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/ 51

ITE 12 – Fundamentals of Programming

Looping
Regien D. Bacabis-Nakazato
Overview
• Parts of Looping
• The While Statement
• The Switch Statement
• The Do-While Statement
• The For Statement
• Nested Logic and loop
Control Structures
• Sequential
• Selection
• Repetition
Parts of Looping
• Initialization
• Test Expression
• Update Expression
• The Body of the loop
Parts of Looping
• Initialization
– Involves initializing loop variables
Parts of Looping
• Test Expression
– Decides whether the loop will be executed or not. This is
commonly a conditional statement/test expression.
Parts of Looping
• Update Expression
– Updates the values of loop variables after every iteration
of the loop
Parts of Looping
• The Body of the loop
– Contains the statement to be executed in the loop
The while statement
The while statement

Initialization

Test Expression

The Body of the


loop

Update Expression
The while statement
The while statement
/* Calculation of simple interest for 3 sets of p, n and
r */

# include <stdio.h>
int main( )
{
int p, n, count ;
float r, si ;
count = 1 ;
while ( count <= 3 )
{
printf ( "\nEnter values of p, n and r " ) ;
scanf ( "%d %d %f", &p, &n, &r ) ;
si = p * n * r / 100 ;
printf ( "Simple interest = Rs. %\nf", si ) ;
count = count + 1 ;
}
return 0;
}
Important Notes
• The statements within the while loop would keep getting executed till the
condition being tested remains true. When the condition becomes false, the
control passes to the first statement that follows the body of the while loop.
• In place of the condition there can be any other valid expression. So long as
the expression evaluates to a non-zero value the statements within the loop
would get executed.
• The condition being tested may use relational or logical operators as shown in
the following examples:

• The statements within the loop may be a single line or a block of statements.
In the first case, the braces are optional. For example,
Important Notes
• Almost always, the while must test a condition that will
eventually become false, otherwise you will encounter an
infinite loop
• Loop counter can also be decremented
• Loop counter can be of any countable data type
The ++ and -- operator
The operator ++ increments the value of i by
1, every time the statement i++ gets
executed.
While
The operator -- decrements the value of I by 1,
every time the statement i– is executed

Note: There is no such thing as +++ and --- operators in C


The ++ and -- operator
Example:
The ++ and -- operator
Example:
The ++ and – operator precedence
• Using i++ is different from ++i. Same for i-- and –I
• In the prefix version (i.e., ++i), the value of i is
incremented, and the value of the expression is the
new value of i. So basically it first increments then
assigns a value to the expression.
• In the postfix version (i.e., i++), the value of i is
incremented, but the value of the expression is the
original value of i. So basically it first assigns a
value to expression and then increments the
variable.
The ++ and – operator precedence
Example:

Output:
The ++ and – operator precedence
Example:

Output:
Sample Problem

Two numbers are entered through the


keyboard. Write a program to find the
value of one number raised to the
power of another.
The Switch Statement
The control statement that allows us to
make a decision from the
number of choices
The Switch Statement
The Switch Statement
Example:
The Switch Statement
Example:
The Switch Statement
Important Notes:
• Cases can be put in any order
• Char values can be used for cases
• Can execute a common set of statements for
multiple cases
• Even if there are multiple statements to be executed
in each case, there is no need to enclose them within
a pair of braces
• Every statement in a switch must belong to some
case or the other. If a statement doesn’t belong to
any case, the compiler won’t report an error
• We can check the value of any expression in a switch
• The switch statement is very useful while writing
menu driven programs.
Sample Problem
Write a menu driven program which has
following options
1. Find the sum of 2 numbers
2. Determine if input is even or odd
3. Prints ‘I’m pretty’
The for loop
The most popular looping instruction, for it
allows us to specify three things about a loop
in a single line:
1. Setting a loop counter to an initial value.
2. Testing the loop counter to determine
whether its value has reached the number of
repetitions desired.
3. Increasing the value of loop counter each
time the body of the loop has been executed.
The for loop
1. Setting a loop counter to an initial value.
2. Testing the loop counter to determine
whether its value has reached the number of
repetitions desired.
3. Increasing the value of loop counter each
time the body of the loop has been executed.
The for loop
The for loop
The for loop
The for loop
Important Notes
• The initialization, testing and
incrementation part of a for loop can be
replaced by any valid expression
The for loop
Important Notes
• Print 1 to 100 in different ways using for
loop
The for loop
Important Notes
• Print 1 to 100 in different ways using for
loop
The for loop
Important Notes
• Print 1 to 100 in different ways using for
loop
The for loop
Important Notes
• Print 1 to 100 in different ways using for
loop
The for loop
Important Notes
• Print 1 to 100 in different ways using for
loop
The for loop
Important Notes
• Print 1 to 100 in different ways using for
loop
Nesting Loops
Nesting Loops
The break statement
• When encountered inside any loop, control
automatically passes to the first statement
after the loop
• Usually associated with an if.
The break statement
Example:
Write a program to determine whether a number is prime or
not. A prime number is said to be prime if it is divisible only by
1 or itself.
The continue statement
• When continue is encountered inside any
loop, control automatically passes to the
beginning of the loop.
• Usually associated with an if
The continue statement
Example:
Write a program that prints the following pattern using for
loop.

5 1
4 2
2 4
1 5
The do-while loop
The do-while loop
The do-while loop

VS
The do-while loop
Do while is used on situations wherein the exact number
of loops is not known beforehand.

Example:
Write a program that continually ask the user for a
number to be squared until the user stops.
The do-while loop
Example:
Write a program that continually ask the user for a number to be squared
until the user stops.
End of Lecture

You might also like