只想得到这个结果。
阵列A 1,2,3,4,5,6,7,8,9,10 阵列B 6,7,8,9,10,1,2,3,4,5
for(k=1; k=10; k++)
{
if(k<10)
{
a[k]=k;
j=k+5;
b[j] = a[k];
}
else
{
a[k] = k;
j = k-5;
b[j] = a[k];
}
printf("%d %d \n",j,k);
}
但不能出于 - 保持循环在k = 10。
答案 0 :(得分:4)
for循环中的第二部分是在每个循环之后计算的条件。在您的情况下,条件为R version 3.5.0 (2018-04-23) -- "Joy in Playing"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[R.app GUI 1.70 (7521) x86_64-apple-darwin15.6.0]
> a <- available.packages()
--- Please select a CRAN mirror for use in this session ---
Warning: failed to download mirrors file (cannot open URL 'https://cran.r-project.org/CRAN_mirrors.csv'); using local file '/Library/Frameworks/R.framework/Resources/doc/CRAN_mirrors.csv'
Warning message:
In download.file(url, destfile = f, quiet = TRUE) :
URL 'https://cran.r-project.org/CRAN_mirrors.csv': status was 'Couldn't connect to server'
> install.packages('ggplot2', repos='http://cran.us.r-project.org')
> options(repos = c(CRAN = "http://cran.rstudio.com"))
> install.packages('ggplot2')
Warning: unable to access index for repository http://cran.rstudio.com/src/contrib:
cannot open URL 'http://cran.rstudio.com/src/contrib/PACKAGES'
Warning: unable to access index for repository http://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5:
cannot open URL 'http://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/PACKAGES'
> options(repos = c(CRAN = "http://cloud.r-project.org/"))
> install.packages('ggplot2')
Warning: unable to access index for repository http://cloud.r-project.org/src/contrib:
cannot open URL 'http://cloud.r-project.org/src/contrib/PACKAGES'
Warning: unable to access index for repository http://cloud.r-project.org/bin/macosx/el-capitan/contrib/3.5:
cannot open URL 'http://cloud.r-project.org/bin/macosx/el-capitan/contrib/3.5/PACKAGES'
>
,其返回值为10,当类型转换为bool时,总是返回k=10
。
true
转换为
for (init; condition; iter-expression) {
code
}
正确的代码是:
{
init
while ( condition ) {
code
iter_expression ;
}
}