将while循环转换为等效的for循环
Convert while loop to an equivalent for loop
这是有while循环的代码,我希望帮助将这些代码转换为等价的for循环
#include<stdio.h>
void main()
{
int x = 1;
int y;
while(x<=10)
{
y=x*x;
cout<<x<<endl<<y;
x = x+3;
getch()
}
试试这个:
#include <stdio.h>
void main()
{
int y;
for (int x = 1; x <= 10; x += 3)
{
y = x * x;
cout << x << endl << y;
getch();
}
}
#include<stdio.h>
void main()
{
int x=1;
int y;
for(x; x<=10;x=x+3)
{
y=x*x;
cout<<x<<end1<<y;
getch()
}
像这样的东西?您可以在此处找到有关流控制(如循环等)的更多信息:http://www.cplusplus.com/doc/tutorial/control/
帮助您朝着正确的方向前进。
for (int x = 1; stop condition; x += 3)
{
doStuff();
}
此外,我不喜欢变量名x,因为它们不能解释它们的含义。
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么我的for循环不能正确获取argv
- 在基于范围的for循环中使用结构化绑定声明
- 通过for循环使用用户输入填充列表
- 使用for循环检查数组中的重复项
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 正在使用for循环创建QScatterSerie
- Python中的for循环与C++有何不同
- 在更改for循环的第三部分后,未使用for循环结果
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 如何用for循环在c++中生成单词三角形
- 如何在for循环中包含两个索引值的测试条件
- 带有多个独立参数的C++For循环
- C++ Python 循环"for i, num in enumerate(list):"版本
- C 多循环 for () 基础知识
- 没有条件值的 FOR 循环"for (int i = 1; ; i++)"无法正常工作
- 为用户提供循环for循环的选项
- 打破循环for循环