for-loop

For Loop Exit Condition (size_t vs. int) [duplicate]

ε祈祈猫儿з 提交于 2021-01-28 00:41:43
问题 This question already has answers here : What's the best way to do a reverse 'for' loop with an unsigned index? (20 answers) Closed 5 years ago . When I put the following in my program: for (size_t i = VectorOfStructs.size()-1; i > 0; i--) It works correctly but does "i" will never equal 0. So, I cannot access the first element (VectorOfStructs[0]). If I change it to: for (size_t i = VectorOfStructs.size()-1; i > -1; i--) The program doesn't even enter the for loop! But, if I change it to the

How do I print out a list of common factors for two numbers?

心不动则不痛 提交于 2021-01-27 21:14:05
问题 I need to print out a list of common factors for two numbers def print_nums(x, y): for i in range(1, x + 1): if x % i == 0: print(i) for t in range(1, y + 1): if y % t == 0: print(t) number = int(input("Enter a number: ")) number2 = int(input("Enter a second number: ")) print("Common factors are: ".format(number, number2)) print_nums(number, number2) It prints out both lists but not the common factors of each 回答1: def print_nums(x, y): zet = [] for i in range(1, x + 1): if x % i == 0: #print

How to display ggplotly plots with dynamically created tabs and for-loops?

可紊 提交于 2021-01-27 20:03:27
问题 I have R markdown document and I want to dynamically create tabs with ggplotly graphics inside them --- output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ```{r} library(ggplot2) library(plotly) ``` ```{r} fig=ggplot(cars)+geom_point(aes(speed, dist)) ``` # level 1 ## level 2{.tabset .tabset-pills} ```{r echo=FALSE, results='asis'} for (h in 1:3){ cat("###", h,'{-}', '\n\n') ggplotly(fig) cat( '\n\n') } ``` I understand that it is different from

SQL Server : Iterate over all tables contained in a database

限于喜欢 提交于 2021-01-27 07:52:09
问题 I know using cursors is a bad idea, but I just do not know how to solve this. I would like to iterate over my table list ( select [table_name] from information_schema.tables ) and issue a specific select statement on each of them. Here is my attempt: DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR SELECT [TABLE_NAME] FROM INFORMATION_SCHEMA.TABLES WHERE [TABLE_NAME] like 'TS%' DECLARE @tableName char(7) OPEN c FETCH NEXT FROM c INTO @tableName WHILE (@@FETCH_STATUS = 0) BEGIN -- which distinct

Iterating over Arrays in Javascript

亡梦爱人 提交于 2021-01-27 06:53:35
问题 I am a JavaScript newbie. I'm trying to practice some sample JavaScript problems. I'm a little stuck when it comes to this question about iterating over arrays. Can anyone point me in the right direction? I am trying to take the values in oldArray , add 5 to each of them, and store in newArray . var oldArray = [12, 45, 6, 23, 19, 20, 20, 15, 30, 42]; var newArray = []; function plusFive(oldArray[i]) { for (var i = 0; i < oldArray.length; i++) { newArray.push(oldArray[i]) + 5) }; } } 回答1: Bug

Java Enhanced For Loop

蓝咒 提交于 2021-01-27 06:48:08
问题 How would I write the following for loop using an enhanced for loop> int [] info = {1,2,3,4,5,6,7,8,9,10}; int i; for (i = 0; i < info.length; i++) { if ((i+1) % 10 == 0) System.out.println(info[i]); else System.out.println(info[i] + ", "); } I am trying the following, but i guess im doing it incorreclty for(int i: info){ body here/// 回答1: Your syntax is correct. The difference is only that you're assigning the actual int value to i instead of the loop index. Thus, if you replace (i+1) % 10

Java Enhanced For Loop

主宰稳场 提交于 2021-01-27 06:45:42
问题 How would I write the following for loop using an enhanced for loop> int [] info = {1,2,3,4,5,6,7,8,9,10}; int i; for (i = 0; i < info.length; i++) { if ((i+1) % 10 == 0) System.out.println(info[i]); else System.out.println(info[i] + ", "); } I am trying the following, but i guess im doing it incorreclty for(int i: info){ body here/// 回答1: Your syntax is correct. The difference is only that you're assigning the actual int value to i instead of the loop index. Thus, if you replace (i+1) % 10

Java - Turning the for-loop counter back based on a conditional

冷暖自知 提交于 2021-01-21 05:15:42
问题 The following is part of the code for my college assignment. else if (!codeList.contains(userCode)) { i--; // i is the counter for the for-loop } else if (userQuantity[i]==0) { i--; } The first part makes sure that if the user enters the wrong code, the counter i does not increment 1, or rather, it subtracts 1 from the recently incremented counter. This part works fine. The second part however is what I seem to be having problems with. userQuantity[] is an int array and it has to be an array.

Java - Turning the for-loop counter back based on a conditional

本秂侑毒 提交于 2021-01-21 05:14:30
问题 The following is part of the code for my college assignment. else if (!codeList.contains(userCode)) { i--; // i is the counter for the for-loop } else if (userQuantity[i]==0) { i--; } The first part makes sure that if the user enters the wrong code, the counter i does not increment 1, or rather, it subtracts 1 from the recently incremented counter. This part works fine. The second part however is what I seem to be having problems with. userQuantity[] is an int array and it has to be an array.

In JavaScript what does for(;;){…} do?

偶尔善良 提交于 2021-01-20 12:20:58
问题 I've seen this in some JavaScript code but I don't get what it does. for(;;){ //other code } I'm used to the for(i=0;i<someLength;i++){...} format but I'm stuck to figure out how or what the "(;;)" syntax is for? 回答1: In JavaScript, for (;;) { ... } just creates an infinite endless loop, which is almost exactly the same as: while (true) { // ... } or do { // ... } while (true); 回答2: for (;;) is the exact same syntax. You're allowed to leave out parts that go between the semicolons, even all