<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>for相关文章列表 | 皇家元林</title>
	<atom:link href="https://hjyl.org/tags/for/feed/" rel="self" type="application/rss+xml" />
	<link>https://hjyl.org</link>
	<description>刘元林的个人博客</description>
	<lastBuildDate>Wed, 09 Nov 2011 15:24:59 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://img.hjyl.org/uploads/2019/10/cropped-about-me-32x32.png</url>
	<title>for相关文章列表 | 皇家元林</title>
	<link>https://hjyl.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>循环语句for和while</title>
		<link>https://hjyl.org/for-while-java/</link>
					<comments>https://hjyl.org/for-while-java/#comments</comments>
		
		<dc:creator><![CDATA[皇家元林]]></dc:creator>
		<pubDate>Wed, 09 Nov 2011 15:24:59 +0000</pubDate>
				<category><![CDATA[元林手札]]></category>
		<category><![CDATA[do while]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[while]]></category>
		<category><![CDATA[循环语句]]></category>
		<guid isPermaLink="false">http://hjyl.org/?p=2037</guid>

					<description><![CDATA[循环语句for和while介绍和实例]]></description>
										<content:encoded><![CDATA[<p>首先介绍下语法：</p>
<pre lang="java" line="1">
//while 语句语法：（while 先判断条件，只有满足条件才执行语句；）
while(条件表达式)
{
	执行语句;
}

//do while 语句语法：（do while 特点是无论条件是否满足，循环体至少被执行一次。）
do
{
	执行语句;
}
while(条件表达式);

//for 语句语法：
for(初始化变量; 条件表达式 ; 循环后操作表达式)
{
	执行语句;
}
</pre>
<p>这三个循环语句都差不多，while 与 do while的区别如上括弧里所述；while与for的区别在于，for语句中，用于控制循环的增量定义在for内，变量只在for语句内有效，for语句执行完毕，则变量在内存中释放，而while的变量会一直在内存里。for与while可以互换，当如果定义增量在循环语句内，则用for。</p>
<p>简单实例：</p>
<pre lang="java" line="1">
class WhileFor
{
	public static void main(String[] args)
	{
		int x=0;
		while(x<5)
		{
			System.out.println("陈冠希-Blow Jobs"); //这句while 含义就是 定义x=0，当x<5的时候，打印“显示陈冠希n次！”这时候只能ctrl+C停止；
		}


		int x=0;
		do
		{
			System.out.println("陈冠希-Blow Jobs"); //跟while一样的效果
		}
		while(x<5);

		int x=0;
		do
		{
			System.out.println("陈冠希-Blow Jobs 显示"+x+++"次"); //这样就只显示5次
		}
		while(x<5);

		for(int x=0; x<5; x++)
		{
			System.out.println("陈冠希-Blow Jobs"); //显示“陈冠希-Blow Jobs” 5次
		}
	}

</pre>
<p>这些简单，看例子就知道怎么用了！<br />
知识点：无限循环最简单的表现形式：</p>
<pre lang="java" line="1">
for(;;){}
while(true){}
</pre>
<p>接着看点复杂点的，嵌套语句：</p>
<pre lang="java" line="1">
/*
实例：1~100中7的倍数的个数，并打印显示；

思路：
	1、先对1~100进行循环（遍历）的形式表示出来；
	2、在遍历中定义条件，只对7的倍数进行操作；
	3、因为7的倍数不确定，只要符合条件，定义一个变量记录这个变化的次数；

步骤：
	1、定义循环语句，选择for语句；
	2、在循环中定义判断，使用if语句，条件：7的倍数：x%7==0;
	3、定义一个变量，随7的倍数出现而递增；

*/

class WhileIf
{
	public static void main(String[] args)
	{
		int count=0;
		for(int x=1; x<=100; x++)
		{
			if(x%7==0)
				//System.out.println("x="+x); //这段代码是打印所有7的倍数；
				count++;
		}
		System.out.println("7的倍数有"+count+++"个"); //计数7的倍数有多少个；
	}
}
</pre>
<p>比嵌套语句还要复杂点的，就是循环中还要循环：<br />
经典实例——99乘法表：</p>
<pre lang="java" line="1">
/*
九九乘法表
1*1=1
1*2=2 2*2=4
1*3=3 3*2=6 3*3=9
.......
*/

class ForFor
{
	public static void main(String[] args)
	{
		for(int x=1; x<=9; x++)
		{
			for(int y=1; y<=x; y++)
			{
				System.out.print(y+"*"+x+"="+y*x+"t"); //注意这里没有换行；
			}
			System.out.println(); //换行；
		}
	}
}
</pre>
<p>好了，第五节课结束！NND，每次看教程看的我只想睡觉。。。只有做笔记的时候还算清醒点，不说了，困觉去。。。</p>
<div id="content-copyright"><span style="font-weight:bold;text-shadow:0 1px 0 #ddd;font-size: 13px;">版权声明: </span><span style="font-size: 13px;">本文采用 <a href="https://hjyl.org/go/aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzMuMC8=" rel="nofollow" target="_blank">BY-NC-SA</a> 协议进行授权，如无注明均为原创，转载请注明转自 <a href="https://hjyl.org">皇家元林</a><br>本文链接: <a rel="bookmark" title="循环语句for和while" href="https://hjyl.org/for-while-java/">循环语句for和while</a></span></div>]]></content:encoded>
					
					<wfw:commentRss>https://hjyl.org/for-while-java/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
