20130328java基礎學習筆記-循環結構for以及for,while循環區別

1.循環結構:for講解
class ForDemo
{
?? ?public static void main(String[] args)
?? ?{
?? ??? ?/*
?? ??? ?for(初始化表達式;循環條件表達式;循環后的操作表達式)
?? ??? ?{
?? ??? ??? ?執行語句;(循環體)
?? ??? ?}
?? ??? ?*/
?? ??? ?for(int x = 1; x<3; x++)
?? ??? ?{
?? ??? ??? ?System.out.println("x="+x);
?? ??? ?}
?? ??? ?/*
?? ??? ?int x =1;
?? ??? ?for(System.out.println("a"),System.out.println("aa"); x<3; System.out.println("b"))
?? ??? ?{
?? ??? ??? ?System.out.println("c");
?? ??? ??? ?x++;
?? ??? ?} //此for循環將輸出a aa c b c b,此語句可用來測試學員對for循環的執行順序是否真的掌握透徹;
?? ??? ?*/
?? ??? ?/*注意for循環的特殊書寫格式;
?? ??? ?for(int a=0,b=0; a<3&&b>2; a++,b--)
?? ??? ?{}
?? ??? ?*/
?? ?}
}
2.循環結構for練習以及和while循環區別:
class ForTest
{
?? ?public static void main(String[] args)
?? ?{
?? ??? ?/*
?? ??? ?for和while的特點:
?? ??? ?1.for和while可以互換;
?? ??? ?2.格式上的不同,在使用上有點小區別;
?? ??? ?? 如果需要通過變量來對循環進行控制,該變量只作為循環增量存在時,區別就體現出來了;
?? ??? ?*/
?? ??? ?//打印1~10十個數字來比較for和while循環的區別
?? ??? ?int x = 1;
?? ??? ?while(x<5)
?? ??? ?{
?? ??? ??? ?System.out.println("x="+x);
?? ??? ??? ?x++;
?? ??? ?}
?? ??? ?System.out.println("x======"+x);
?? ??? ?for(int y=1; y<5; y++)
?? ??? ?{
?? ??? ??? ?System.out.println("y="+y);
?? ??? ?}
?? ??? ?//System.out.println("y====="+y);
?? ??? ?
?? ??? ?//無限循環最簡單的形式
?? ??? ?//while(true){}
?? ??? ?//for(;;){}? for循環中間表達式不寫默認為true;
?? ??? ?/*
?? ??? ?注:
?? ??? ?a.for里面的多個表達式運行的順序,初始化表達式只讀一次,判斷循環條件,為真就執行循環體,然后再執行循環后的操作表達式,接著繼續判斷循環條件,重復這個過程,直到條件不滿足為止;
?? ??? ?b.while和for可以互換,區別在于for為了循環而定義的變量在for循環結束就會在內存中釋放.而while循環使用的變量在循環結束后還可以繼續使用;
?? ??? ?c.最簡單無限循環格式:while(true){},for(;;){},無限循環存在的原因是并不知道循環多少次數,而是根據某些條件,來控制循環;
?? ??? ?*/
?? ?}
}

3.循環結構的使用場景:

什么時候使用循環結構呢?

當對某些代碼執行很多次時,使用循環結構執行;

當對一個條件進行一次判斷時,可以使用if語句;當對一個條件進行多次判斷時,可以使用while語句;

注意:在使用循環時,一定要明確哪些語句需要參與循環,哪些不需要;循環通常情況下,需要定義條件,需要控制次數;

轉載于:https://www.cnblogs.com/lisu/archive/2013/03/28/2987069.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274305.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274305.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274305.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

小程序設計避免犯什么錯_新設計師犯下的5種印刷錯誤以及如何避免

小程序設計避免犯什么錯Over the last year and a half, I’ve had the opportunity to teach the basics of typography to undergraduate graphic design students. During this time, I’ve noticed some common mistakes that my students make when first learning how to…

移動設備web文字單位_移動設備如何塑造現代Web設計

移動設備web文字單位I was working with a nonprofit earlier this month on redesigning their website and during the first meeting, I proposed a very standard idea: the home page needed to tell a story and guide the intended user through the intended process (…

hp-ux修改時區方法_UX研究人員可以倡導人類的6種方法

hp-ux修改時區方法In the UX world, we often hear terms like “user-centered,” “human-centered,” and “customer-centered.” We believe that in order to be innovative, we need to center experiences that are authentic, intuitive, and practical.在UX世界中&am…

2013年3月百度之星A題

偽隨機數生成器 題目描述 baidu熊最近在學習隨機算法&#xff0c;于是他決定自己做一個隨機數生成器。 這個隨機數生成器通過三個參數c, q, n作為種子, 然后它就可以通過以下方式生成偽隨機數序列&#xff1a; m0 c, mi1 (q2mi 1) mod 2n, for all i > 0. 因為一些奇怪的…

為什么張揚的人別人很討厭_為什么每個人總是討厭重新設計,即使他們很好

為什么張揚的人別人很討厭重點 (Top highlight)微處理 (Microprocessing) In Microprocessing, columnist Angela Lashbrook aims to improve your relationship with technology every week. Microprocessing goes deep on the little things that define your online life to…

轉載--C語言:浮點數在內存中的表示

單精度浮點數&#xff1a; 1位符號位 8位階碼位 23位尾數 雙精度浮點數&#xff1a; 1位符號位 8位階碼位 52位尾數 實數在內存中以規范化的浮點數存放&#xff0c;包括數符、階碼、尾數。數的精度取決于尾數的位數。比如32位機上float型為23位 double型為52位。…

學習ui設計_如果您想學習UI設計,該怎么辦

學習ui設計There is a question that is always asked when we want to learn something new.當我們想學習新東西時&#xff0c;總會問一個問題。 Where to start?從哪兒開始&#xff1f; This is also being my question when I want to learn UI design. In this article, …

Christmas

html5 game - Christmasloading......轉載于:https://www.cnblogs.com/yorhom/archive/2013/04/05/3001116.html

30個WordPress Retina(iPad)自適應主題

原文地址&#xff1a;http://www.goodfav.com/zh/retina-ready-wordpress-themes-3556.html WordPress Retina定制主題進行了優化&#xff0c;支持Retina屏幕上的高品質和清晰的圖像。如果你關心這個話題&#xff0c;又不知道這究竟是什么&#xff0c;那么請你繼續閱讀。 wordp…

Thinking in java第一章對象導論

這一章&#xff0c;做筆記感覺不是很好做。每個人又每個人對面向對象的理解。這里說一下書里的關鍵字&#xff0c;穿插一下自己的思想 面向對象的編程語言里面很流行的一句話&#xff0c;一切都是對象。面向對象的核心就是抽象&#xff0c;抽象的能力有大有小&#xff0c;是決定…

Android SlidingMenu插件的使用

1、在github上下載了源碼后 不知道如何使用&#xff0c;在折騰了一個晚上后終于弄好了 下載地址 https://github.com/jfeinstein10/SlidingMenu 下載完后&#xff0c;解壓&#xff0c;然后先import 其中的library &#xff0c;然后把項目名改為SlidingMenu 2、然后再到http…

css 字體字體圖標_CSS基礎知識:了解字體

css 字體字體圖標In this tutorial, we’ll be learning about working with fonts in CSS!在本教程中&#xff0c;我們將學習有關在CSS中使用字體的知識&#xff01; The font property is a shorthand property which can combine a number of sub-properties in a single d…

openstack quantum搭建過程中一些有用的鏈接

OpenvSwitch的概念和流程&#xff1a; http://blog.wachang.net/2013/03/openvswitch-fullbook-2-workflow-1/ OpenvSwitch的vlan模式&#xff1a; http://openvswitch.org/support/config-cookbooks/vlan-configuration-cookbook/ OpenvSwitch問答&#xff1a; http://openvsw…

mysql下載哪一代版本好_潮一代更好的設計

mysql下載哪一代版本好I think we can all agree that quarantined life has been strange. And while most of the day is comprised of the monotony of domestic life, I’ve been surprised at how much of my time is dominated by technology.我認為我們都可以同意隔離的…

預約清單ui設計_持續交付質量設計所需的UI清單

預約清單ui設計重點 (Top highlight)Over the past few months, my design team at StreetEasy has started experimenting in adding a “design buddy” check-in to the final stages of the design process.在過去的幾個月中&#xff0c;我在StreetEasy的設計團隊已開始嘗試…

黑書上的DP例題

pagesectionnotitlesubmit1131.5.1例題1括號序列POJ11411161.5.1例題2棋盤分割POJ11911171.5.1例題3決斗Sicily18221171.5.1例題4“舞蹈家”懷特先生ACM-ICPC Live Archive1191.5.1例題5積木游戲http://202.120.80.191/problem.php?problemid12441231.5.2例題1方塊消除http://…

靜態創意和動態創意_我在22歲時學到的關于創意指導的知識

靜態創意和動態創意During my last semester at college, I took a course titled “Collaborative Workshop”. The entire course focused on how to best collaborate within a team setting. We were placed into groups of 4 or 5. These were our “creative director” …

vim7.1在windows下的編碼設置[轉]

在gvm配置文件中&#xff1a; &#xff08;gvim安裝目錄下的_vimrc文件中&#xff09; """""""""""""""""""""""""""""&…

絕對編碼和增量編碼_用戶體驗設計師應該學習編碼嗎? 絕對

絕對編碼和增量編碼Even though I was trained as a graphic designer, I’ve never limited myself to that field exclusively. My particular interest in how things work didn’t allow me to stand still and as a young kid, I was already pulling apart all my toys t…

兩個ID

在itpub上注冊了ID googlgoracle &#xff0c;發過不少的求助帖子。 http://www.itpub.net/home.php?modspace&dothread&viewme 在CSDN 上ID:googlg,注冊時間挺早的2008年&#xff0c;一直也沒有弄過。 http://write.blog.csdn.net/postlist http://blog.csdn.net/goo…