Fruitful Loops: A Tasty Journey into Programming Iterations
Before we begin, riddle me this!!!
Why did the programmer go broke?
Because he used up all his loops!!!
Loops are indeed a very powerful tool. There are two types of loops namely entry controlled and exit controlled loops
Entry controlled loops consists of for and while loops and the exit controlled loops consists of do while loop.
Let's go over the syntax of each loop before I share one of my coding excerpts.
1) For loop
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
2) While loop
initialization expression;
while (test_expression)
{
// statements
update_expression;
}
3) Do while loop
initialization expression;
do
{
// statements
update_expression;
} while (test_expression);
Now here's my excerpt
As you can see now that I had familiarized myself with the nuances of the loops. I decided to tinker with them for a bit.
1} The code below is beginner friendly and its just used to figure out whether the value of n that is entered by the user is a prime number or not . The concept of flags makes the code below function more efficiently.
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "enter n: ";
cin >> n;
bool iota = true;
if (n <= 1)
{
cout << "the number is neither prime nor composite";
}
else
{
for (int i = 2; i < n; i++)
{
if (n % i == 0)
{
cout << "not a prime number";
iota = false;
break;
}
}
if (iota )
{
cout << "the number is prime";
if (n == 2)
{
cout << ", and it's an even prime number";
}break;
}
}
return 0;
}
2) It's a piece of cake to print numbers in a serial order using for loop.
As I was randomly scrolling through youtube I came across a vid from BroCode channel regarding loops in python where he basically made a count-down timer using for loop.
The idea of creating a timer using for loop was enticing to me. I thought let's do something similar in C++. So here's what my brain cooked up- let's make a mini-game type of thing using for loop where the user would enter a particular value [ in this case it's range ] and depending upon whether it's even or odd, even or odd numbers would be displayed on the console but here's the catch make the whole thing backwards similar to a timer.
I feel that newbies [ ahemm i'm still a part of that group ] get overwhelmed by looking at a problem which is more than of four lines. To combat this issue I started approaching the problem with the simplest of the simplest steps.
In this case I figured out how to print the numbers in reverse order then followed it up with the basic logic of even and odd numbers.
Approach 1)
#include <iostream>
using namespace std;
int main()
{
int n; //range
cout<<"enter the value of n: ";
cin>>n;
cout<<"here comes the fun!!!\n";
//print even and odd nos. in reverse order using a loop
if(n % 2 == 0){
for(int i = n; i>0; i-- ){
if ( i % 2== 0){
cout<<i<<endl;
}
}
}
else{
for(int i = n; i>0; i-- ){
if ( i % 2!= 0){
cout<<i<<endl;
}
}
}
}
Approach 2) I just replaced the if - statement in the else block with a while loop
#include <iostream>
using namespace std;
int main()
{
int n; //range
cout<<"enter the value of n: ";
cin>>n;
cout<<"here comes the fun!!!\n";
//print even and odd nos. in reverse order using a loop
if(n % 2 == 0){
for(int i = n; i>0; i-- ){
if ( i % 2== 0){
cout<<i<<endl;
}
}
}
else{
for(int i = n; i>0; i-- ){
while ( i % 2!= 0){
cout<<i<<endl;
break;
}
}
}
}
And there you have it—the world of loops unlocked! From for
to while
and do-while
, you've gained the tools to make your code dynamic and efficient.
Remember, programming is not just about solving problems; it's also about having fun with code! We've ventured into creating a countdown-style display of even and odd numbers, providing a glimpse of the creative possibilities that loops offer.
But this is just the beginning. Loops are your creative playground. In future explorations, we'll dive into the art of printing patterns with for loops—opening doors to endless possibilities.
Subscribe to my newsletter
Read articles from Vaishnavi Pawar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vaishnavi Pawar
Vaishnavi Pawar
Hello fellow tech enthusiasts! It's me, and I find myself immersed in the fascinating world of programming languages, eagerly exploring the wonders they offer. As someone on this journey of discovery, I am constantly captivated by the endless possibilities that programming languages bring. From the fundamental concepts to the intricate syntax, each programming language seems to unfold its own set of wonders. Currently in the process of figuring out where my interests lie, I've dipped my toes into python, C++ and then lastly tinkered a bit with APIs.