You are viewing our Forum Archives. To view or take place in current topics click here.
C++ Noob loops help
Posted:

C++ Noob loops helpPosted:

Whoever
  • Resident Elite
Status: Offline
Joined: Aug 05, 201410Year Member
Posts: 260
Reputation Power: 13
Status: Offline
Joined: Aug 05, 201410Year Member
Posts: 260
Reputation Power: 13
So I'm new to c++ coding and have to do a module on it for uni

I've got a worksheet about for and while loops
I need to create a loop that outputs this:
1
12
123
1234
12345
123456

It has to be inside a loop I cannot just used std::cout
If anyone has any ideas or ways to help that would be great
#2. Posted:
-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
You can use any type of loop for this. You simply need to exit the loop once your variable matches 6.


For Loop

for (int i = 1; i <= 6; i++) {
  // output i
}



Do Loop

int i = 1;
do {
  // output i
  // increase i
} while (i <= 6);



While Loop

int i = 1;
while (i <= 6) {
  // output i
  // increase i
}
#3. Posted:
Whoever
  • Resident Elite
Status: Offline
Joined: Aug 05, 201410Year Member
Posts: 260
Reputation Power: 13
Status: Offline
Joined: Aug 05, 201410Year Member
Posts: 260
Reputation Power: 13
Thankyou so much
I've not really done much C++ and I have a whole sheet on loops to do by tomorrow,
there is 6 exercises and I've done 4
Also If I wanted It to print something like
THIS:
1*****
12****
123***
1234**
12345*
123456

Could you tell me how I would go about that?
#4. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
If you take 6 minus whatever the current value is and the do a for loop for that many times.

Example based on the do loop.


Do Loop

int i = 1;
do {
  // output i
  for (var j = 6 - i; j > 0; j--) {
    // output *
  }
  // increase i
} while (i <= 6);


Here the for loop for j will start at 5 and each iteration of the do loop will result in j getting smaller.
This means that for the first loop: i = 1, j = 5. 2nd loop: i = 2, j = 4. etc.
#5. Posted:
Sunder
  • Summer 2019
Status: Offline
Joined: Nov 05, 201113Year Member
Posts: 2,315
Reputation Power: 1306
Status: Offline
Joined: Nov 05, 201113Year Member
Posts: 2,315
Reputation Power: 1306
Is this for the console or a GUI?
#6. Posted:
Whoever
  • Resident Elite
Status: Offline
Joined: Aug 05, 201410Year Member
Posts: 260
Reputation Power: 13
Status: Offline
Joined: Aug 05, 201410Year Member
Posts: 260
Reputation Power: 13
Thanks for the help again I'm really quite lost here
Also I'm not sure what its for, we've been given a sheet to practice Imbricated loops and I have very little knowledge in the area, so I'm just trying to get the specified output in Visual Studios
#7. Posted:
Visxal
  • TTG Addict
Status: Offline
Joined: Nov 07, 20159Year Member
Posts: 2,261
Reputation Power: 332
Status: Offline
Joined: Nov 07, 20159Year Member
Posts: 2,261
Reputation Power: 332
It's probably for console, i had to do this last year.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.