Discuss The Following: What Is An Iterative Statement in Programming?
Discuss The Following: What Is An Iterative Statement in Programming?
VENTURA
ASSIGNMENT
BASIC C++ PROGRAMMING 01
ASSIGNMENT NO. 2
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.
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
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
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 () {
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 () {
/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
for loop
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
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