C#: Control Statement - Loops

Author: Ronald S. Holland
at
Total Application Works




To contact us



HOME Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript

© 2002 - 2013 All Rights Reserved Total Application Works


    Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
    <html>
    <head>
    <title>C#: Table of Contents </title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/CS/CS_Cont_State_Loops.html">
    C# Loops </a>
    </table>
    </body>
    </html>


Table of Contents

  1. Introduction to C#
  2. Control Statement: Selection Statement in C#
  3. Control Statements: Loops Statement
  4. Methods
  5. Namespaces
  6. Introduction to Classes:
  7. Inheritance:<
  8. Polymorphism
  9. Properties
  10. Indexers
  11. Structs
  12. Interfaces
  13. Delegates:
  14. Exception Handling:
  15. Attributes
  16. Enums
  17. Encapsulation
  18. Parameter Passing in C#
  19. Method Overloading
  20. Database Interaction Using C#
  21. Operator Overloading in C# -1
  22. Operator Overloading in C# -2
  23. Operator Overloading in C# -2
  24. Sockets
  25. DNS [Domain Name System]
  26. Working with Files
  27. Generating Help File in C#
  28. Code Access Security
  29. Multi-Threading
  30. Globalization and Localization -1
  31. Working with Registry in C#
  32. Globalization and Localization -2
  33. Windows Service
  34. Web Service
  35. Consuming Web Services
  36. Creating Proxy Object of Web Service
  37. Creating an XML document
  38. Reading XML document in C#
  39. Using XMLWriter class to Write XML document in C#
  40. Assembly Information : Getting Permission set of the assembly
  41. Creating your own Permission Set
  42. Using C# Socket

Introduction

This tutorial assumes that you have some prior programming experience. It is not intended to be a comprehensive discussion on c# or programming. As we ease into c#, we will briefly discuss the if control structure. c# as a programming language is widespread and can be found on nearly all computers. In the next section, we will briefly describe control structures.

Control Structures

Bohm and Jacopini demonstrated, in their Structured program theorem, that all programs could be written using just three control structures.

  • sequence structure
      Consider the following example:

      
      int a = 8 , b ;
      b = (5 + 2) * a  ;
      cout << b << endl ;
      
      Figure 1: Squence of statements structure
      If a code snippet consists of the above three statements, then the statements will execute in the order/sequence that they are listed. This is also known as an ordered execution of statements.
  • selection structure
      In a selection statement, one of a number of statements is executed depending on the state of the program. Consider the following example:

      
      int a, b = 0;
      
      cout << "Enter a number less than 10 and greater than 0." << endl ;
      cin >> a ;
      
      if( a > 5 )
         b = (5 + 2) * a  ;
      
      cout << "The value of b is " <<  b << endl ;
      
      Figure 2: selection structure
  • repetition structure
      A repetition structure is used when a set of instructions need to be executed more than once.

      
      int b = 0;
      
      for( int ii = 0 ; ii < 10 ; ii++ ) {
         b = (5 + 2) * ii  ;
         cout << "The value of b is " <<  b << endl ;
      }
      
      
      Figure 3: repetition structure
Control Structures or constructs are described further in the next section.

Control Structures or constructs

What are Control Structures or constructs? The Control Structures constructs used in programming are:
  1. if - single selection structure
      This structure is used to determine whether a condition is true or false. If the condition evaluates to true, then some action is taken.
      
      if( condition is true ) {
         statement_1
         statement_2
         statement_3
         ...
         statement_n
      }
      
      Figure 4: if - single selection structure


  2. if/else - double selection structure
      This structure is used to determine whether a condition is true or false. If the condition evaluates to true, then some action is taken; otherwise, a different action is taken.
      
      if( condition is true ) {
         statement_1
         statement_2
         statement_3
         ...
         statement_n
      }
      else {
         statements
      }
      
      Figure 5: if/else - double selection structure


  3. switch - multiple selection structure
      This structure allows selection among multiple sections of code, depending on the value of an integral expression. It operates much like a nested if construct.
      
      switch( expression ) {
         case 1;
            do something ;
         case 2;
            do something ;
         case 3;
            do something ;
         ...
      
         default;
            do something ;
      }
      
      Figure 6: switch - multiple selection structure


  4. while
      In this construct, the condition is a boolean statement that is checked after the final } braces of the while statement executes. If the condition is true then the while statement executes again. If the condition is false, the statements inside the while construct do not execute again, due to the code branching out of the construct.
      
      while loop control
      word                start bracket
       |                       |
       v                       v 
      
      while( condition  is true )                         {
         statement_1
         statement_2
         statement_3
         ...
         statement_n
      }
      
      Figure 7: while loop structure


  5. do/while
      The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. This construct guarantees that the body of the loop always executes at least one time. The format of the do-while loop is shown below.
      do {
         statement_1
         statement_2
         statement_3
         ...
         statement_n
      }
      while( condition is true )
      
      Figure 8: do-while loop structure


  6. for
      Executes the statements and loop-expression repeatedly until test becomes false. After the first pass, the loop counter is updated and then the test-condition is checked to determine whether it is true or false.
      
      for( initialization ; test ; update ) {
         statement_1
         statement_2
         statement_3
         ...
         statement_n
      }
      
      Figure 9: for loop structure


For the remainder of this tutorial, we will describe the while loop contruct.

The loop/iteration construct

An example of a The loop/iteration construct program will be depicted in the following table.



while loop code/examples
1- while Loop - raise a variable "x" to a power of "y"
2- while loop - obtains the class grades and then computes the class average
3- while Loop - obtains a number from the user and create a custom countdown counter
4- while Loop - adjust the car speed to the set cruise speed
5- while Loop - get integers, sum them and the calculate the average
6- while Loop - get ten numbers, determine the two largest and the smallest numbers.
7- while Loop - multiplies the number of the loop iteration by 5, 10, 100, and 1000.
8- while Loop - obtains numbers from the user and determines whether that number is positive or negative.
9- while Loop - obtains binary number from the user and then converts it to an integer.
10- while Loop this program obtains two random * numbers, a and y and then
  1. Multiplies the two numbers, x * y
  2. Adds the two numbers together, x + y
  3. Subtracts the two numbers, x - y
11- while Loop this program computes a
  1. Geometric Expression, using a while construct
  2. Geometric series, using a for construct
  3. Last value of the loop iterator, using a switch construct
12- while Loop creates a ten-by-ten multiplication table.

Summary

The if statement can be written in multiple ways to implement different branches of logic. The switch statement allows a choice among a set of bool, enum, integral, or string types. You use break, continue, goto, return, or throw statements to leave a case statement. Be sure to avoid the goto statement in your code unless you have an extremely good reason for using it.

In addition to branching based on a condition, it is useful to be able to execute a block of statements multiple times. A goto statement is not proper or adequate for such logic. Therefore, I invite you to return for Lesson 4: Control Statements - Loops. This will be a continuation of the same topic.






HOME Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript


To contact us

© 2002 - 2013 All Rights Reserved Total Application Works