C#语言学习--基础部分(五) --复合运算符,循环语句

1.复合运算符的使用:*=,/=,%=,+=,-=

2.while语句的编写

   a.WPF Demo:

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;

namespace WhileStatement
{
    /// <summary>
    
/// MainWindow.xaml 的交互逻辑
    
/// </summary>
    public partial class MainWindow : Window
    {
        private OpenFileDialog openFileDialog = null;
        public MainWindow()
        {
            InitializeComponent();
            openFileDialog = new OpenFileDialog();
            openFileDialog.FileOk += openFileDialogFileOk;
        }

        private void openFile_Click(object sender, RoutedEventArgs e)
        {
            openFileDialog.ShowDialog();
        }
        private void openFileDialogFileOk(object sender,System.ComponentModel.CancelEventArgs e)
        {
            string fullPathName = openFileDialog.FileName;
            FileInfo src = new FileInfo(fullPathName);
            fileName.Text = src.Name;
            source.Text = "";
            TextReader reader = src.OpenText();
            string line = reader.ReadLine();
            while(line!=null)
            {
                source.Text+=line+'\n';  
                line=reader.ReadLine();
            }
            reader.Close();
        }
    }
}

 

3.continue,break语句的使用

a. Console Demo:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WhileDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            while (true)
            {
                i++;
                Console.WriteLine("i={0}",i);
                if (i < 10)
                    continue;
                else
                 break;
                
            }
           
        }
    }
}

 

4.for语句的编写

5.do...while语句的编写

 a.Console Demo

    

           1.
            int i = 0;
            for(;i<10;i++)
                Console.WriteLine("i={0}",i);
           2.
            for(int i=0;i<10;i++)
                Console.WriteLine("i={0}",i);
           3
            int i=0;
            do {
                Console.WriteLine("i={0}",i);
                i++;
            } while (i < 10);
           

 b.WPF Demo

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DoWhileStatement
{
    /// <summary>
    
/// MainWindow.xaml 的交互逻辑
    
/// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void showSteps_Click(object sender, RoutedEventArgs e)
        {
            steps.Text = "";
            int a = int.Parse(number.Text);
            string current="";
            do
            {
                current = Convert.ToString(a % 8)+current;
                steps.Text+=current+"\n";
                a=a/8;
            }while(a!=0);
        }
    }
}

 

posted on 2012-08-13 22:09  松波  阅读(263)  评论(1编辑  收藏  举报

导航