Decision Making in C++
Decision Making in C++
• Loops in C++
There may be a situation, when you need to execute a block of code several number of times.
In general, statements are executed sequentially: The first statement in a function is executed
first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and
following is the general from of a loop statement in most of the programming languages –
C++ programming language provides the following type of loops to handle looping
requirements.
1 while loop
2 for loop
Execute a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
3 do...while loop
Like a ‘while’ statement, except that it tests the condition at the end of the loop
body.
4 nested loops
You can use one or more loop inside any another ‘while’, ‘for’ or ‘do..while’ loop.
While Loop in C++
In C++, a while loop is a control structure used to execute a block of code repeatedly as long
as a given condition evaluates to true. The condition is checked before every iteration,
making it a pre-test loop.
Flow Diagram
cout << "Your age is: " << age << endl;
return 0;
}
Explanation:
• Iteration 1:
o The user is prompted for an age.
o If the age entered is not between 1 and 120, the loop runs again.
o The condition age < 1 || age > 120 checks if the input is invalid.
• Iteration 2:
o After the invalid input in the first iteration, the loop prompts the user again.
o This process continues until a valid age is entered.
• Exit:
o When a valid age is entered (e.g., 25), the condition becomes false.
o The loop exits, and the program prints the user's age.
cout << "Your age is: " << age << endl;
• Explanation:
o The loop body executes at least once before checking the condition.
o Even if the condition is initially false (e.g., entering a value of -5), the prompt
still appears once before validating.
o The loop guarantees that the user is prompted at least once.
Key Differences:
• while Loop:
o Condition first: The loop may not execute at all if the condition is false
initially.
o Best for: Cases where the loop might not need to run at all.
o Examples: Reading from a file where no data might be available.
• do-while Loop:
o At least one iteration: The loop body executes at least once.
o Best for: Situations where the loop must run at least once (e.g., user prompts,
menu-driven programs).
o Examples: Input validation, repeatedly prompting the user for a valid option.
3. Error Handling:
o Control Flow: The do-while loop is useful for error handling in iterative
processes. It ensures that the loop runs once regardless of the condition, which
can be beneficial for scenarios where input is expected at least once.
o Avoids Infinite Loop: By ensuring the loop executes at least once, the do-
while loop mitigates risks of an infinite loop that might occur in a simple
while loop due to an incorrect condition.
4. Flexibility:
o Dynamic Conditions: The condition is checked after the loop body, which
allows the body to execute first regardless of the condition. This can be useful
in applications that require user interaction or input validation.
• Example:
for (int i = 0; i < 10; i++) {
cout << i << endl;
}
o Initialization: int i = 0;
o Condition: i < 10; — The loop runs until i is less than 10.
o Update: i++ — i is incremented after each iteration.
o Explanation:
▪ The loop starts with i = 0, runs as long as i < 10, and increments i
by 1 after each iteration, printing values from 0 to 9.
Flow Diagram
Example
#include <iostream>
using namespace std;
int main () {
// for loop execution
for( int a = 10; a < 20; a = a + 1 ) {
cout << "value of a: " << a << endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
Example
1. Write a program that calculates the sum of all integers from 1 up to a given integer n
#include <iostream>
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
int sum = 0;
cout << "Sum of numbers from 1 to " << n << " is: " << sum << endl;
return 0;
}
2. How would you use a for loop to calculate the factorial of a given integer n?
#include <iostream>
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factorial of " << n << " is: " << factorial << endl;
return 0;
}
3. Printing Multiplication Table
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
return 0;
}
4. Write a program that finds and prints all prime numbers up to a given integer n
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Prime numbers up to " << n << " are: ";
if (isPrime) {
cout << num << " ";
}
}
return 0;
}
5. Write a program that generates the first n numbers in the Fibonacci sequence.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
cin >> n;
int a = 0, b = 1;
return 0;
}