Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Computer Programming Lab 7

This document provides an overview of for loops and nested loops in C++, including their syntax and implementation. It includes example programs demonstrating the use of these loops, as well as practice exercises for finding factorials, GCD, and summing series. Additionally, it includes a rubric for evaluating performance in lab exercises.

Uploaded by

moeeniqbal175
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Computer Programming Lab 7

This document provides an overview of for loops and nested loops in C++, including their syntax and implementation. It includes example programs demonstrating the use of these loops, as well as practice exercises for finding factorials, GCD, and summing series. Additionally, it includes a rubric for evaluating performance in lab exercises.

Uploaded by

moeeniqbal175
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Computer Programming Lab 7

Repetition Statements-II
1: Objective:
• To learn about syntax and implementation of for loops

2: For Loops

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a
specific number of times. Syntax
for ( inital; condition; increment )
{ statement(s);
}

Here is the flow of control in a for loop −


The init (initialization) step is executed first, and only once. This step allows you to declare and initialize any
loop control variables. You are not required to put a statement here, as long as a semicolon appears.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop
does not execute and flow of control jumps to the next statement just after the for loop.
After the body of the for loop executes, the flow of control jumps back up to the increment statement. This
statement can be left blank, as long as a semicolon appears after the condition.
The condition is now evaluated again. If it is true, the loop executes, and the process repeats itself (body of loop,
then increment step, and then again condition). After the condition becomes false, the for loop terminates.
Example Program

#include <iostream>

using namespace std;

int main ()

for( int a = 10; a <20;

a = a + 1 )

{ cout << "value of a: "

<< a << endl;


Computer Programming Lab 7

return 0;

Output

3: Nested Loops
A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting.

Syntax
The syntax for a nested for loop statement in C++ is as follows −

for ( init; condition; increment ) {


for ( init; condition; increment ) {
statement(s);
}
statement(s); // you can put more statements.
}
The syntax for a nested while loop statement in C++ is as follows −
Computer Programming Lab 7

while(condition)
{ while(condition)
{ statement(s);
}
statement(s); // you can put more statements.
}
The syntax for a nested do...while loop statement in C++ is as follows −
do { statement(s); // you can put more
statements.
do {
statement(s); }
while( condition );

} while( condition );
Example Program

#include <iostream> using

namespace std; int main

() {

int i, j; for(i = 2; i<30; i++)

for(j = 2; j <= (i/j); j++)

if(!(i%j)) break; // if factor found, not prime

if(j > (i/j))

cout << i << " is prime\n";

return 0;

}
Output
Computer Programming Lab 7

3: Practice Exercise

1) Write a program in C++ to find the factorial of a number. Go to the editor


Sample output:
Input a number to find the factorial: 5
The factorial of the given number is: 120
2) Write a program in C++ to find the Greatest Common Divisor (GCD) of two numbers. Go to the editor
Sample Output:
Input the first number: 25
Input the second number: 15
The Greatest Common Divisor is: 5
3) Write a program that runs a loop from 1 to 10.
When there comes an odd number, the program displays
the sum of all numbers from 1 to that specific odd
number.E.g;
When there comes 1, it displays “Sum of numbers upto 1 is 1”
When there comes 3, it displays “Sum of numbers upto 3 is 4”
When there comes 5, it displays “Sum of numbers upto 5 is 9” And
so on.
Nested Loops
1) Write a program in C++ to calculate the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) +
... + (n*n). Go to the editor. Sample Output:
Input the value for nth term: 5
1*1 = 1
2*2 = 4
3*3 = 9
4*4 = 16
5*5 = 25
The sum of the above series is: 55
2) Write a program in C++ to display the pattern like right angle triangle using an asterisk. Go to the
editor
Sample Output:
Input number of rows: 5
*
**
***
****
*****
Computer Programming Lab 7

Name: Roll No:

Instructor Signature: ___________________________

Lab Rubrics

Sr. Performance Excellent Good Fair Poor (0 Marks)


# Indicator (5 Marks) (4-3 Marks) (2-1 Marks)
Skill to Quite able to Able to conduct Able to conduct and Unable to
perform test conduct and and demonstrate demonstrate the conduct and
experiment demonstrate the the problem- problem-based task demonstrate the
(PLO-05) problem-based task based task with with insufficient problem-based
1 (P-4) with complete sufficient implementation task.
implementation and implementation and inadequate
results but inadequate results
interpretation. results Interpretation.
Interpretation.
Scale

Sr. Performance Excellent Good Fair Poor (0 Marks)


# Indicator (5 Marks) (4-3 Marks) (2-1 Marks)
Data Correctly analyses Analyses and Analyses and Fails to analyze and
exploration and interprets the interprets data interprets data interpret data.
and analysis data with useful with basic level of at basic level
2
conclusions. conclusions. without useful
(PLO-04) conclusions
(C-4)
Scale

You might also like