Skip to main content

C# Tutorial, Decision making & loop statements

 Hello, Welcome to C# learning. My name is Abdul Yesdani. Today we are going to discuss about decision making or branching statements and looping or iteration statements in the C# language.

Decision making statements:

To make a firm decision we will use decision making statements. For this we will use specific key words such as if, if-else.

example: A buyer  wants to buy a product, but he will make a decision to buy it if  the product is available for less than some x amount.

int price = 180;

if (price < 200)
  Console.WriteLine("Buy the product");

if() statement checks Boolean value of a variable , means the value is true or false. price is assigned with 180. In the statement if (price < 200) , the boolean value is true. When the value is true the statement or block of statements which follows if will get executed.
So out put will be Buy the product.

int price = 280;
if (price < 200)
      Console.WriteLine("Buy the product");
else
      Console.WriteLine("Don't buy it , its expensive");

In the above example price has changed to 280. In the if () statement boolean value get false.When it is false the statement or block of statements which follows else get executed.
Here output will be Don't buy it , its expensive.


loops or Iterative statements:

To do a task repeatedly or more than once we will use loop / iterative statements. While, do-while, for key words are used for iteration.
Example: To print a series of numbers looping statement can be used.


int number =0;

while(number < 10)

   {

      Console.WriteLine($"The number is {number}");

      number++;

  }

The while statement check the condition and execute the block of statements until the condition get false. Here, when number reaches to 10 it will stop. While block will first check the condition and execute the following block of statements. 



int number =0;

  do

   {

      Console.WriteLine($"The number is {number}");

      number++;

  }while(number < 10);

 do- while block will first execute the  block of statements then check the condition in the while().



Another common and popular  loop is for.

  for(int number =1; number <=10; number++)

   {

     Console.WriteLine($"The number is {number}");

    }

The first part is initializer, second part checks condition and third part is incrementor.


Watch video demo here.





 नमस्कार, C # लर्निंग में आपका स्वागत है। मेरा नाम अब्दुल यसदानी है। आज हम C # भाषा में निर्णय लेने या शाखाओं में बँटने और स्टेटमेंट्स या लूपिंग या इटर्शन स्टेटमेंट के बारे में चर्चा करने जा रहे हैं।


निर्णय लेने का निर्णय:

एक दृढ़ निर्णय लेने के लिए हम निर्णय लेने वाले बयानों का उपयोग करेंगे। इसके लिए हम विशिष्ट कुंजी शब्दों का उपयोग करेंगे जैसे कि, if, if-else.


उदाहरण के लिए: एक खरीदार एक उत्पाद खरीदना चाहता है, लेकिन वह इसे खरीदने का निर्णय करेगा यदि उत्पाद कुछ एक्स राशि से कम में उपलब्ध है।

int price = 280;
if (price < 200)
      Console.WriteLine("Buy the product");
else
      Console.WriteLine("Don't buy it , its expensive");

if () स्टेटमेंट एक वेरिएबल की बूलियन वैल्यू को चेक करता है, तो इसका मतलब है कि वैल्यू सही है या गलत। price 180 के साथ दिया गया है। कथन में यदि (price <200) है, तो बूलियन मान सत्य है। जब मूल्य सही है या बयानों का खंड जो if ke baad aayega voh execute  किया जाएगा।तो output  होगा Buy the product।


int price = 280;
if (price < 200)
      Console.WriteLine("Buy the product");
else
      Console.WriteLine("Don't buy it , its expensive");


उपरोक्त उदाहरण में price बदलकर 280 हो गया है। if () स्टेटमेंट बूलियन मान गलत मिलता है। तब यह कथन या स्टेटमेंट का खंड गलत होता है जो इस प्रकार निष्पादित होता है।यहाँ आउटपुट Don't buy it, its expensive.


लूप्स या Iterative कथन:

किसी कार्य को बार-बार या एक से अधिक बार करने के लिए हम लूप / पुनरावृत्तियों का उपयोग करेंगे। while, do-while, for  कुंजी शब्दों के लिए उपयोग किया जाता है।उदाहरण: संख्याओं की एक श्रृंखला को मुद्रित करने के लिए लूपिंग स्टेटमेंट का उपयोग किया जा सकता है।

int number =0;

while(number < 10)

   {

      Console.WriteLine($"The number is {number}");

      number++;

  }

while स्टेटस की जाँच करता है और स्टेटमेंट को तब तक execute करता है जब तक कि स्थिति झूठी न हो जाए। यहां, जब संख्या 10 तक पहुंचती है तो यह बंद हो जाएगा। जबकि ब्लॉक पहले स्थिति की जांच करेगा और बयानों के निम्नलिखित ब्लॉक को execute करेगा।


int number =0;

  do

   {

      Console.WriteLine($"The number is {number}");

      number++;

  }while(number < 10);


do- while ब्लॉक पहले स्टेटमेंट के ब्लॉक को execute करेगा, फिर while () में कंडीशन को चेक करेगा।


एक और आम और लोकप्रिय लूप है for।


for(int number =1; number <=10; number++)

   {

     Console.WriteLine($"The number is {number}");

    }

 पहला भाग इनिशियलाइज़र है, दूसरा भाग चेक कंडीशन और तीसरा भाग इंक्रीमेंटर है।


Comments

Popular posts from this blog

Every day Hands on Exercises / Assignments

Day wise hands on exercises/ assignments Day 1: HTML Hands on Exercises Date:04-10-2021 1.Create a web page to display your name, designation, location. 2. Create a web page to show about yourself in 3, 4 paragraphs. 3. Create a web page to display some nature images and Describe those images. 4.create a web page about college anniversary celebrations Divide your page in 4, 5 div elements . In the 1st div, put the heading of college , and display other details about the college. In the second div put images of the college. In third div put details of celebrations in paragraphs In the fourth div display the guest list. Use style attribute for doing color, background color, font etc, To emphasise some special content use elements like , 5. Create a web app on Indian cultural festivals. Use semantic elements for lay out of the page. Define an external style sheet and link with the page. Your app should have indexpage, cultural page, about us page. You should navigate amon...

.Net Presentation

.NET is a free, cross-platform, open source developer platform for building many different types of applications. With .NET, you can use multiple languages, editors, and libraries to build for web, mobile, desktop, games, and IoT. You can write .NET apps in C#, F#, or Visual Basic. C# is a simple, modern, object-oriented, and type-safe programming language. F# is a programming language that makes it easy to write succinct, robust, and performant code. Visual Basic is an approachable language with a simple syntax for building type-safe, object-oriented apps. You can write .NET apps in C#, F#, or Visual Basic. C# is a simple, modern, object-oriented, and type-safe programming language. F# is a programming language that makes it easy to write succinct, robust, and performant code. Visual Basic is an approachable language with a simple syntax for building type-safe, object-oriented apps.

Python for Beginners Intro -3

Python for beginners introduction -3 In this presenetation you will learn about OOPs programing how to inherit a base / parent class to a derived/ child class. Exception handling concepts, file handling concepts. You will also learn about python libraries like numpy, pandas and matplotlib.