Introduction to programming concepts
Computers belonging to different domains are used to perform a variety of tasks. Do you know how a computer is able to perform different tasks?
To carry out the work, the computer input, process and adhere to the basic principle OUYPUT. Computer information in a set of instructions called a program, process, and production activities is needed.
In this chapter input, process, output requirements and the introduction of a computer. The instructions in a computer program and died PROGRMMING explains the role of languages In addition, it describes various tools to solve the problem solving.
Input process and output
You might have noticed computer being used at airline reservation counters, shops, restaurants, and various other locations. At each of these places, a user enters some values in the computer and the computer generates an output, which is, either displayed on the computer screen or printed on a paper.
Let us consider the example of an airline reservation counter. When you want to resrve a seat on particular flight, you provide information about your requirements, such as the destination, the data and time of your departure, and the class in which a booking is required. The executive at the reservation counter enters this information in a computer. The details regarding the availability of tickets are then displayed on the screen.
The preceding automated process is not carried out as one activity. It is broken into a set of activities that are carried out in separate phases. Let us see the phases involved and the process followed for each of the tasks.
To undersand what happens when you enter some values in a computer, let us break the entire set of activities into separate phases.
Consider the example of the airline reservation counter. In the first phase, flight requirements are keyed into the computer. This phase is called the input phase. The flight requirement information is then processed to determine whether seats are availble on the particular flight is known as the processphase. Once the processing is complete, the result is displayed on the computer screen, indicating the status of seat availability. This phase is called the output phase.
The cycle of activities performed by a computer is also known as the input - process- output (I-P-O) cycle.
A computer consists of several componets, such as a keyboard, mouse, mouse, monitor, printer, and central processing unit (CPU). Each computer participates in any one of the phases: process, or output. For example, the keyboard and mouse are used for input. The CPU and memory inside the CPU are used for processing. The monitor and printer are used for output. Following figure depicts the (I-P-O) cycle.
The (I-P-O) cycle
Programs and programming languages
Have you ever wondered how a computer:
Is a computer an all-knowing machine? Do you have to buy a different computer for each of the preceding activities? The answer to the last two questions is ‘No’.
It is not an all-knowing machine, and you do not require a separate computer for each of these activities.
Then, how does a computer function? A computer is an electronic device. It needs a set of instructions called a program to perform the I-P-O cycle. A program needs to be written in a specific language, called a programming language.
Let us discuss programs and programming languages in detail.
All of us perform a number of tasks in our day-to-day lives. Some of the tasks that we perform are:
To perform these tasks, you follow a step-by-step procedure. For example, while driving a can, you comply with the following sequence of steps to cross a traffic signal:
<!--[if !supportLists]-->1. <!--[endif]-->Approach the light.
<!--[if !supportLists]-->2. <!--[endif]-->If the light is red, you will stop.
<!--[if !supportLists]-->3. <!--[endif]-->If the light is yellow, you will slow down and then, stop.
<!--[if !supportLists]-->4. <!--[endif]-->Otherwise, the light will be green and continue driving.
In the same way, a computer uses a step-by-step procedure to perform any task. A computer is designed to accept an input, process it, and generate an output. However, you need to provide a set of instructions that specify:
Therefore, for each job that you want the computer to perform, you require a separate program. Depending upon the requirements, instructions in a program can be:
Decision making: instructions that evaluate an expression (relation or condition) first and then, depending upon whether the value of the expression is true or false, the control is transferred to a particular statement.
Iterative: instructions that are executed repeatedly, depending upon the value of an expression (relation or condition).
Any real-life problem that you solve with the help of a computer will consist of a number of small jobs. Therefore, to solve such a problem, you have a number of programs that together form an application. For example, a gaming application might have the following two programs:
A computer is made up of a number of electronic components. An electronic component can be either in an ON state or in an OFF state. The ON state is represented by 1, and the OFF state is represented by 0. This implies that a computer can understand only 0s and instructions. This collection is called machine language code or simply machine code. For example, the following table depicts machine code that can be used to represent the commands, ADD and SUBTRACT.
The machine code of the ADD and SUBTRACT commands
However, it is difficult for anybody to remember instructions in the form of machine language code. Therefore, we use high-level programming languages to write programs. A high-level programming language consists of a set of instructions, which are represented using simple English words. Therefore, when you want the computer to display the output on the screen, you type an instruction like ‘WRITE NO SCREEN’ and not ‘00100000’. There are many high-level programming languages available, such as C, C++, and java.
All language have a vocabulary, which is a list of words that have specific meanings in that language. Languages also have their own grammar rules, which state the rules for combining words to form sentences. The vocabulary of programming languages is known as the set of keywords of that language, and the grammar is known as the syntax. For example, in a programming language, you can the following statement to display the message, “MY first program”:
Print (“MY first program”);
In the preceding statement, print can be referred to as a keyword used in that programming language to display a text on the screen. Further, as per the syntax of programming language, the text that needs to be displayed is semicolon.
Can computers directly interpret the instructions written in any programming language? NO, they cannot. Then, how do you ensure a computer interprets the instructions given in a program? For this purpose, a special program known as a compiler is used. Consider a situation. You are in a foreign country and trying to ask directions form a person who does not know your language. How would you understand the directions?
You would need a person who can translate the direction information into English or any other language that you can undersand. Similarly, you need a translator to convert the instructions written in a programming language into the machine language.
A computer is a special program that processes the statements written in a particular programming language and converts them into the machine language, at one go. Like everything else in the computer, the compiler also follows the I-P-O cycle. It takes the programming language instructions as an input. It then, processes these instructions to convert them into the machine language. These instructions can then be executed by the computer. This process of conversion is called compilation.
A ADD b
Programming language machine language
For each programming language, there is a different compiler available. For example, to compile a program written in the C language, you require a C compiler; and for a java program, you require a java compiler.
(Input-process output cycle)
(Central processing unit) this is a answer= compiler?
Tools used in problem solving
As seen, a program is a set of instructions to solve a particular problem. In the process of writing a program, you need to derive a technique to solve the problem. After deriving the technique, you need to represent the technique in a way that is helpful in writing the program easily. For this, you can various tools. The tools that you can use to represent the technique to solve a problem are:
An algorithm is a sequence of steps required to solve a problem. It is a list of well-defined instructions to solve a problem. According to D.E. Knuth, a pioneer in the field of computer science, an algorithm has the following characteristics:
For example, when you approach a traffic signal while driving a vehicle, the algorithm to take adecision will be:
Step 1: start the algorithm.
Step 2: approach traffic light.
Step 3: If the light is red, stop.
Step 4: if the light is yellow, slow down and then, stop
Step 5: otherwise, the light is green, continue driving.
Step 6: end the algorithm.
An algorithm follows the I-P-O cycle to solve a problem. Here, the input is the color of the traffic light that is passed to your drain, after seeing the traffic light. Information about the color is processed in the brain. If the color is red, you need to stop. If the color is yellow, you need to slow down and then, stop. Otherwise, you need to continue driving. Here, your final action, after seeing the color, is the output.
Consider another example where you want to withdraw a required amount from an ATM.
In this case, the algorithm will be.
Step 1: start the algorithm.
Step 2: insert your ATM card.
Step 3: the ATM machine checks your card details.
Step 4: enter the pin number.
Step 5: the ATM machine validates the pin number.
Step 6: press the cash with receipt button.
Step 7: select the amount you want to withdraw.
Step 8: collect your ATM card.
Step 9: the ATM machine counts the cash.
Step 10: collect the amount from the ATM machine.
Step 11: collect the statement from the ATM machine.
Step 12: end the algorithm.
Here, the input to withdraw a required amount from an ATM would be the ATM card, pin code number, and amount to withdraw. These inputs are that accepted, and the process of withdrawing the amount place. At the and of the process, the amount, which is the output, is withdrawn.
Consider another example where a television company may provide the following algorithm in the television manual to troubleshoot the ‘NO sound’ problem:
Step 1: start the algorithm.
Step 2: ensure that the mute button is not pressed.
Step 3: ensure that the desired volume is set.
Step 4: if the sound is still inaudible, call the television engineer.
Step 5: end the algorithm.
A flowchart is a graphical representation of an algorithm. It consists of a sat of symbols. Each symbol represents a specific kind of activity depicted in the algorithm. A typical flowchart would involve accepting instructions and data as inputs, processing these instructions and data and displaying the result as an output. The processing would involve the computations and the decision that are being taken.
For example, the following flowchart depicts how to take a decision when you approach a traffic signal while driving a vehicle.
Light is green, continue driving
Is traffic light yellow?
Is traffic light red?
The flowchart to take a decision on a traffic signal
Advantages of flowchart
Flowchart offers many advantages. Some of these advantages are:
Disadvantages of flowchart
Although a flowchart provides several advantages, it has a few disadvantages as well. Some of these disadvantages are:
A Pseudocode is a detailed yet readable description of what an algorithm must do. It is expressed in a formally styed natural language rather than in a programming language. A Pseudocode is used as an initial step in the process of developing. It provides the programming a detailed template for writing instructions in a specific programming language.
For example, the following Pseudocode shows the process of deciding the course of action when you approach a signal while driving a vehicle.
Pseudocode for deciding the course of action on a traffic signal
Character light, action
If (light == ‘red’)
Action – ‘stop’
Else if (light == ‘yellow’)
Action = ‘slow down and then stop’
Action = ‘continue driving’
Introduction to C#
A computer needs a set of instructions called a program to perform any operation. A program needs to be written in a specific language called programming language, so that the computer can understand the instructions. C# is one such programming language.
This chapter introduces you to C#. it explains how to define classes and declare variables in C#. In addition, it discusses how to create the object of a class. Further, it discusses how to write execute C# programs.
Computer languages have come a long way since the 1940s.during that period, scientists punched instructions into large, room-sized computer systems. These instructions were given in machine language, which consisted of a long series of zeroes and ones. These machine instructions were executed directly by the CPU. The machine language is called the first generation of computer language, the assembly language. Assembly language is easier to write than machine could still understand only machine language. Therefore, the assembler software was developed to translate the code written in assembly language into machine language.
In 1967, martin Richard developed a language called BPCL for writing operating systems. An operating system is a set of program that manages the resources of a computer and its interactions with users. The era of the third generation of computer language had arrived. In 1970, ken Thompson teamed up with Dennis Ritchie and wrote an initial version of the Unix operating system for a DEC PDP-7 computer.
Dennis Richie was working on a project to further develop the UNIX operating system. He wanted a low level language, like the assembly language, that could control hardware efficiently. At the same time, he wanted the language to provide the features of a high-level language, that is, it should be able to run on different types of hardware.
B had performance drawbacks, so in 1972 he rewrote B and called it C. Therefore, C is categorized as both a second and a third generation language. Thompson and Ritchie rewrote the UNIX operating system in C. in the years that followed; C was widely accepted and used over different hardware platforms. This led to many variations of C. in 1989, the American national standards institute (ANSI), along with the international standards organization (ISO), approved a machine-independent and standard version of C.
In the early 1980s, Bjarne Stroustrup up of bell labs developed the C++ language. In his own words,” C++ was designed primarily so that my friends and I would not have to program in assembly, C, or various modern high-level languages. Its main purpose was to make writing good programs easier and more pleasant for the individual programmer.”
C++ was originally known as ‘C with classes’ because two languages contributed to its design: C, which provided low-level features, and simula67, which provided the class concept. C++ is an object-oriented language. Other object-oriented language are java, Smalltalk, and C# (pronounced as C sharp).
All language Have a vocabulary, which is a list of words that have a specific meaning in that language. Language Also have grammar rules, which state the rules for combining words to form sentences. This ensures that whatever is spoken in a particular language is understood in the same way by all people who know the language. Similarly, programming language also have a vocabulary, which is referred to as of keywords of that language, and a grammar, which is referred to as the syntax.
Consider a scenario, where one person speaks in English with the other person. However, the person who is listening understands Chinese and not English. Therefore, he calls a translator who can translate the words of English to Chinese. Similarly, computers cannot directly understand the instructions written in a programming language. Therefore, you need a translator to convert the instructions written in a programming language to machine language.
A computer is a special that program that processes the statements written in a particular programming language and converts them into machine language. Lake everything else in the computer, the compiler also follows the input-process-output (I-O-P) cycle. It takes the programming language instructions to convert them to machine language. These instructions can then be executed by the computer. This process of conversion is called compilation.
The following figure shows the working of a compiler.
program written in language 2
machine language program 2
Working of a compiler
For each programming language, there is a different compiler available. For example, to compile a program written in the C language, you require a C compiler. For a java program, you require a Java Compiler. For C# program, you will use the CSC compiler.
Programming language into machine language.
Using C# for writing programs
Similar to the various programming language, C# also has some predefined keywords that can be used for writing programs. For example, class is a keyword in C# that is used to define classes. Keywords are reserved words that have a special meaning. Further, the syntax for C# defines rules for grammatical arrangement of these keywords. For example, the syntax of declaring a class in C# is:
In the preceding syntax, the braces, known as delimiters, are used to indicate the start and end of a class body.
Classes in C#
C# classes are the primary building blocks of the language. C# also provides certain predefined set of classes and methods. These classes and methods provide various basic functionalities that you may way to implement in your application.
For example, if you want to perform some input/output operations in your application, you can use predefined classes available in the system. Io namespace
In addition to using the predefined classes, consider the following code,
Which defines a classes named Hello:
Public class hello
Public static void Main(string  args)
System. Console. Write Line (“Hello, World!\n”);
The preceding class declaration includes the method, main() that will display the message. “Hello, World!” on your screen. The preceding code includes the following components:
The class keyword
The class keyword is used to declare a class. In the preceding code, the class keyword declares the class, Hello.
The class name
The class keyword is following by the name of the class. In the preceding code, Hello is the name of the class defined by using the class keyword.
Class naming conventions in C#
Class name should follow certain naming conventions or guidelines. A class name:
Should be meaningful (strongly recommended).
Should ideally be a noun.
Can use either the Pascal case or Camel case. In Pascal case, the first letter is capitalized and the rest of the letters are in lower case, such as My class. In camel case, the first letter is in lower case and the first letter of each subsequent word is capitalized, such as my class or int Employee Details.
Rules for naming classes in C#
In addition to the conventions, there are certain rules that must be following while naming classes in C#. The name of classes:
‘_’. The first character in a class name cannot be a digit.
The main () method
<!--[if !supportLists]-->1. <!--[endif]-->The first line of code that a C# compiler looks for in the source file is the main () method. This method is the entry point for an application. This means that the execution of code starts from the main () method.
<!--[if !supportLists]-->2. <!--[endif]-->The main () method is ideally used to create objects and invoke the methods of various classes that constitute the program.
The System. Console. WriteLine ()
Console is a class that belongs to the system namespace. The console class includes a predefined Write Line () method. This method display the enclosed text on the user’s screen. The console class has various other method that are used for various input/output operations.
The dot character (.) is used to access the Write Line () method, which is present in the console class of the System namespace.
The System. Write Line () statement can also be written as Console. Write Line () if the statement, using System is included as the first line of the code.
The following code snippet is an example of the console. Write Line () method:
Console. Write Line(“Hello, world! \n”);
The preceding code snippet will display the following message on the screen:
To display special characters, such as the new line character or the backspace character, you need to include appropriate escape sequences in your code. An escape sequence is a combination of characters consisting of a backslash (\) followed by a letter or a combination of digits.
The following table lists some of the escape sequences provided in C#.
Single quotation mark
Double quotation mark
The common escape sequences in C#
The following code snippet depicts the usage of the new line escape sequence:
Console. Write Line (“Hello \n World”);
The preceding code snippet will display the following message on the screen:
Identify the output of the following code snippet:
Console. Write Line (“Dr\b\b tom Wilkins”);
Dr\b\b tom Wilkins
Dr tom Wilkins
Dr\b tom Wilkins
This ans =(Tom Wilkins)
Consider a situation where you have to create a program that accepts two numbers from a urea and display the sum of the numbers on the screen. New, while reading the numbers provided by the user, you need to store these numbers somewhere in the memory so that you can perform the add operation on the numbers. You can store the numbers in the memory by storing user input in the memory by using variables.
Using variables to store user input
In the preceding figure, a user provides 5 and 3 as input. The digit 3 is stored in a variable, num2. A variable is a location the value. The value could be an integer, such as ‘L’. A variable is associated with a data type that defines the type of data that can be stored in the variable.
For example, a variable called tennis player name will ideally store Characters, whereas a variable called High score will store numbers. A program refers to a variable by its name.
For example, a variable called Tennis Player Name will ideally store characters, whereas a variable called High_ Score will store number. A program refers to a variable by name.