0% found this document useful (0 votes)
34 views6 pages

Discuss The Following: What Is An Iterative Statement in Programming?

The document discusses iterative statements in C++ programming such as while, do-while, and for loops. It provides examples of each loop type and explains their components and usage. The document also asks the student to create C++ programs using a while loop that displays even numbers from 30-70, calculates the factorial of a given input, and determines if a number is prime or composite.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

Discuss The Following: What Is An Iterative Statement in Programming?

The document discusses iterative statements in C++ programming such as while, do-while, and for loops. It provides examples of each loop type and explains their components and usage. The document also asks the student to create C++ programs using a while loop that displays even numbers from 30-70, calculates the factorial of a given input, and determines if a number is prime or composite.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

NATIONAL UNIVERSITY MANILA COMPUTER FUNDAMENTALS WITH PROGRAMMING ENGR. RAFAEL A.

VENTURA

NAME: RAMIREZ, DANILO, JR. R. SECTION: ESE196 GRADE :


DATE: JANUARY 8, 2020

ASSIGNMENT
BASIC C++ PROGRAMMING 01
ASSIGNMENT NO. 2

Q1: DISCUSS THE FOLLOWING:

 What is an iterative statement in programming?

Iteration is the process where a set of instructions or statements is executed


repeatedly for a specified number of time or until a condition is met. These statements
also alter the control flow of the program and thus can also be classified as control
statements in C Programming Language.

 Parts of an Iterative Statement

 Initialization
In computer programming, initialization (or initialisation) is the assignment of an initial
value for a data object or variable. The manner in which initialization is performed
depends on programming language, as well as type, storage class, etc., of an object
to be initialized. Programming constructs which perform initialization are typically
called initializers and initializer lists. Initialization is distinct from (and preceded by)
declaration, although the two can sometimes be conflated in practice. The
complement of initialization is finalization, which is primarily used for objects, but not
variables.

 Condition
In computer science, conditional statements, conditional expressions and conditional
constructs are features of a programming language, which perform different
computations or actions depending on whether a programmer-specified boolean
condition evaluates to true or false. Apart from the case of branch predication, this is
always achieved by selectively altering the control flow based on some condition.

 Update
An update is new, improved, or fixed software, which replaces older versions of the
same software. For example, updating your operating system brings it up-to-date with
the latest drivers, system utilities, and security software. Updates are often provided
by the software publisher free of additional charge.

 Iterative Statement used in C++

 while
The while loop evaluates the test expression inside the parenthesis ().
If the test expression is true, statements inside the body of while loop are executed.
Then, the test expression is evaluated again.
The process goes on until the test expression is evaluated to false.
If the test expression is false, the loop terminates (ends).

 do-while
The body of do...while loop is executed once. Only then, the test expression is
evaluated.

Page 1 of 6
NATIONAL UNIVERSITY MANILA COMPUTER FUNDAMENTALS WITH PROGRAMMING ENGR. RAFAEL A. VENTURA

If the test expression is true, the body of the loop is executed again and the test
expression is evaluated.
This process goes on until the test expression becomes false.
If the test expression is false, the loop ends.

 for

The initialization statement is executed only once.


Then, the test expression is evaluated. If the test expression is evaluated to false,
the for loop is terminated.
However, if the test expression is evaluated to true, statements inside the body
of for loop are executed, and the update expression is updated.
Again, the test expression is evaluated.

Q2: Give at least three program examples for each of the iterative statements (while, do-
while, and for). Briefly discuss the purpose of each program example.

 while loop

The while loop evaluates the test expression inside the parenthesis ().


If the test expression is true, statements inside the body of while loop are
executed. Then, the test expression is evaluated again.
The process goes on until the test expression is evaluated to false.
If the test expression is false, the loop terminates (ends).

Example 1:
1. // Print numbers from 1 to 5
2.
3. #include <stdio.h>
4. int main()
5. {
6. int i = 1;
7.
8. while (i <= 5)
9. {
10. printf("%d\n", i);
11. ++i;
12. }
13.
14. return 0;
15. }

Example 2:
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
while(num<=10) //while loop with condition
{
printf("%d\n",num);
num++; //incrementing operation

Page 2 of 6
NATIONAL UNIVERSITY MANILA COMPUTER FUNDAMENTALS WITH PROGRAMMING ENGR. RAFAEL A. VENTURA

}
return 0;
}

Example 3:
#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

/* while loop execution */


while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
}

return 0;
}

 do-while loop

The body of do-while loop is executed once. Only then, the test expression is
evaluated.
If the test expression is true, the body of the loop is executed again, and the test
expression is evaluated.
This process goes on until the test expression becomes false.
If the test expression is false, the loop ends.

Example 1:
1. // Program to add numbers until the user enters zero
2.
3. #include <stdio.h>
4. int main()
5. {
6. double number, sum = 0;
7.
8. // the body of the loop is executed at least once
9. do
10. {
11. printf("Enter a number: ");
12. scanf("%lf", &number);
13. sum += number;
14. }
15. while(number != 0.0);
16.
17. printf("Sum = %.2lf",sum);
18.
19. return 0;
20. }

Example 2:
#include <stdio.h>
int main()

Page 3 of 6
NATIONAL UNIVERSITY MANILA COMPUTER FUNDAMENTALS WITH PROGRAMMING ENGR. RAFAEL A. VENTURA

{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}while (j<=3);
return 0;
}

Example 3:
#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );

return 0;
}

 for loop

The initialization statement is executed only once.


Then, the test expression is evaluated. If the test expression is evaluated to false,
the for loop is terminated.
However, if the test expression is evaluated to true, statements inside the body
of for loop are executed, and the update expression is updated.
Again, the test expression is evaluated.

Example 1:
1. for (initializationStatement; testExpression; updateStatement)
2. {
3. // statements inside the body of loop
4. }

Example 2:
#include <stdio.h>
int main()
{
int i;
for (i=1; i<=3; i++)
{
printf("%d\n", i);
}
return 0;
}
Example 3:
#include <stdio.h>
int main()
{

Page 4 of 6
NATIONAL UNIVERSITY MANILA COMPUTER FUNDAMENTALS WITH PROGRAMMING ENGR. RAFAEL A. VENTURA

for (int i=0; i<2; i++)


{
for (int j=0; j<4; j++)
{
printf("%d, %d\n",i ,j);
}
}
return 0;
}

Q3: Create a C++ program using only “while loop” for the for the following problems:

A.) Create a program that will display all even numbers from 30-70.

B.) Create a program that will display the factorial value of a given integral input.

Page 5 of 6
NATIONAL UNIVERSITY MANILA COMPUTER FUNDAMENTALS WITH PROGRAMMING ENGR. RAFAEL A. VENTURA

C.) Create a program that will determine if an integral input is a prime or composite
number.

Page 6 of 6

You might also like