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

C Basics Loop

Uploaded by

Akixia Sta. Cruz
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 views20 pages

C Basics Loop

Uploaded by

Akixia Sta. Cruz
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/ 20

C++

Language
Example 1:

#include <iostream>

using namespace std;

int main() {
for (int i = 1; i <= 10; i++) {
cout << i << " ";
}
cout << endl;

return 0;
}

1.Includes and namespace:


•Includes the iostream header for input/output operations.
•Uses the std namespace for convenience.

2.Main function:
•The for loop:
•for (int i = 1; i <= 10; i++):
•Initializes i to 1.
•Continues the loop as long as i is less than or equal to 10.
•Increments i by 1 after each iteration.
•cout << i << " ";: Prints the current value of i followed by a
space.
•cout << endl;: Prints a newline character to move to the next line.
Example 2:

1.Include header:
•#include <iostream>: Includes the standard input/output stream library for
input and output operations. #include <iostream>

using namespace std;


2.Namespace:
•using namespace std;: Brings the std namespace into scope to avoid
prefixing standard library elements with std::.
int main() {
3.Main function: int n;
•int main(): Defines the main function where the program execution begins.
•int n;: Declares an integer variable n to store the user-entered number.
•cout << "Enter a positive integer: ";: Prompts the user to enter a positive
cout << "Enter a positive integer: ";
integer.
•cin >> n;: Reads the user's input and stores it in the n variable. cin >> n;
•cout << "Factors of " << n << ": ";: Prints a message indicating that the
factors of n will be displayed.
•For loop:
cout << "Factors of " << n << ": ";
•for (int i = 1; i <= n; ++i): Iterates from i = 1 to i = n (inclusive).
•if (n % i == 0): Checks if i is a factor of n by using the modulo operator for (int i = 1; i <= n; ++i) {
(%). If the remainder is 0, i is a factor.
•cout << i << " ";: If i is a factor, prints it followed by a space. if (n % i == 0) {
•cout << endl;: Prints a newline character to move to the next line.
cout << i << " ";

}
4.Return statement: cout << endl;
•return 0;: Indicates successful program execution and returns 0 to the
operating system.
This program effectively demonstrates the use of a for loop to iterate through a return 0;
range of values and perform calculations or actions based on conditions. It provides
a clear and concise example of how loops can be used in C++ programming. }
C++ While Loop

 Loops can execute a block of code as long as a specified condition is


reached.
 Loops are handy because they save time, reduce errors, and they
make code more readable.
 The while loop loops through a block of code as long as a specified
condition is true.
C++ While Loop

Entry Controlled loops: In this type of loop, the test condition is tested before
entering the loop body. For Loop and While Loop is entry-controlled loops.

Exit Controlled Loops: In this type of loop the test condition is tested or
evaluated at the end of the loop body. Therefore, the loop body will execute at
least once, irrespective of whether the test condition is true or false. the do-
while loop is exit controlled loop.
DIY
DIY
C++ Do/While Loop
C++ Do/While Loop
C++ For Loop

Example explained
Statement 1 sets a variable before the loop starts (int i = 0).

Statement 2 defines the condition for the loop to run (i must be less than 5). If the
condition is true, the loop will start over again, if it is false, the loop will end.

Statement 3 increases a value (i++) each time the code block in the loop has been
executed.
Exercises:
Exercises:
Exercises:

1. Do-while Loop - do while loop prompts the user to enter a positive integer and adds it to the variable sum if it is positive. This process continues until the user enters a
negative integer.
The "inner loop" will be
Nested Loops executed one time for each
iteration of the "outer loop":
The foreach Loop
C++ Break
C++ Continue
Break and Continue in While Loop

You might also like