9,59 €
This course takes you on a journey through the basics of C# programming and algorithmic thinking, tailored specifically for complete beginners. Starting with an understanding of how a computer works, you will explore integrated development environments, essential software packages, and basic algorithmic concepts.
As you progress, you will delve deeper into key programming constructs such as variables, constants, input and output handling, operators, and control structures. The course emphasizes practical application, guiding you through exercises involving complex mathematical expressions, string manipulations, decision structures, and loop control structures. By the time you reach the advanced sections, you will have a comprehensive understanding of data structures, subprograms, and object-oriented programming.
Designed to be engaging and informative, this course offers tips, tricks, and detailed explanations to help you master each concept. The use of Visual Studio Code is seamlessly integrated, providing you with hands-on experience in a real-world development environment. By the end of the course, you will be well-equipped to tackle more advanced programming challenges and continue your journey in the world of software development.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 1006
Veröffentlichungsjahr: 2024
For optimal reading experience, please rotate your device to landscape orientation.
C# and Algorithmic Thinking for the Complete Beginner
Learn to Think Like a Programmer
3rd Revised Edition
By
Aristides S. Bouras
C# and Algorithmic Thinking for the Complete Beginner
3rd Revised Edition
Copyright © by Aristides S. Bouras
https://www.bouraspage.com
Cover illustration: Philippos Papanikolaou
Cover design: Muhammad Arslan
The following are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries: Microsoft, Windows, IntelliSense, SQL Server, .NET Framework, Visual Studio, Visual Studio Code, VBA, Visual Basic, and Visual C#.
Oracle and Java are registered trademarks of Oracle and/or its affiliates.
Python and PyCon are trademarks or registered trademarks of the Python Software Foundation.
PHP is a copyright of the PHP Group.
Mazda and Mazda 6 are trademarks of the Mazda Motor Corporation or its affiliated companies.
Ford and Ford Focus are trademarks of the Ford Motor Company.
All crossword puzzles were created with EclipseCrossword software powered by Green Eclipse
Other names may be trademarks of their respective owners.
Rcode: 240601
All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, mechanical or electronic, including photocopying, recording, or by any information storage and retrieval system, without written permission from the author.
Warning and Disclaimer
This book is designed to provide information about learning “Algorithmic Thinking”, mainly through the use of C# programming language. Every effort has been taken to make this book compatible with the latest release of C#, and it is almost certain to be compatible with any future releases of it.
The information is provided on an “as is” basis. The authors shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of the files that may accompany it.
I was born in 1973, and from my early childhood, I discovered a passion for computer programming. At the age of 12, I got my first computer—a Commodore 64, which incorporated a ROM-based version of the BASIC programming language and 64 kilobytes of RAM. It introduced me to the world of programming!
I hold a degree in Computer Engineering from the Technological Educational Institute of Piraeus. Additionally, I earned Dipl. Eng. and Integrated Master's degrees in Electrical and Computer Engineering from the Democritus University of Thrace.
In my previous role as a software developer at a company specializing in industrial data flow and product labeling, my responsibilities included developing software applications for data terminals and PC software for data collection and storage on a Microsoft SQL Server®.
Over the years, I've designed various applications, including warehouse management systems and websites for diverse companies and organizations. Presently, I serve as a computer science teacher in a vocational high school, instructing courses on computer programming, computer networks, programming tools for the Internet/intranets, and databases.
I've authored several books, primarily focusing on algorithmic and computational thinking, utilizing languages such as Python, C#, Java, C++, PHP, and Visual Basic.
Outside of my professional pursuits, I am happily married and have the joy of raising two wonderful children.
I would like to thank, with particular gratefulness, my friend and senior editor Victoria (Vicki) Austin for her assistance in copy editing the first edition. Without her, this book might not have reached its full potential. With her patient guidance and valuable and constructive suggestions, she helped me bring this book up to a higher level!
The book you hold in your hands follows the spiral curriculum teaching approach, a method proposed in 1960 by Jerome Bruner, an American psychologist. According to this method, as a subject is being taught, basic ideas are revisited at intervals—at a more sophisticated level each time—until the reader achieves a complete understanding of the subject. First, the reader learns the basic elements without worrying about the details. Later, more details are taught and basic elements are mentioned again and again, eventually being stored in the brain's long-term memory.
According to Jerome Bruner, learning requires the student's active participation, experimentation, exploration, and discovery. This book contains many examples, most of which can be practically performed. This gives the readers the opportunity to get their hands on C# and become capable of creating their own programs.
Completely updated for the latest version of C#, this book offers a comprehensive introduction to programming, assuming no prior knowledge of the subject. It is designed for individuals, eager to learn C# from scratch, providing a strong foundation in Algorithmic Thinking—the fundamental skill every aspiring programmer must acquire. Algorithmic Thinking encompasses more than just writing code; it entails mastering the art of problem-solving through coding.
This edition retains all the popular features of its predecessor while introducing a wealth of new exercises, along with extensive revisions and updates. Furthermore, brand-new chapters offer a practical introduction to working with text files.
Structured for both classroom use and independent study, each chapter is concluded with a set of questions and exercises designed to reinforce your understanding and apply what you've learned. With approximately 250 solved and 480 unsolved exercises, 500 true/false questions, 150 multiple-choice questions, and 200 review questions and crosswords (with solutions and answers available online), this book is ideal for:
Following are some explanations on the conventions used in this book. “Conventions” refer to the standard ways in which certain parts of the text are displayed.
C# Statements
This book uses plenty of examples written in C# language. C# statements are shown in a typeface that looks like this.
Keywords, Variables, Methods, and Arguments Within the Text of a Paragraph
Words in Italics
You may notice that some of the special text is also displayed in italics. In this book, italicized words are general types that must be replaced with the specific name appropriate for your data. For example, the general form of a C# statement may be presented as
In order to complete the statement, the keywords name, type1, arg1, type2, and arg2 must be replaced with something meaningful. When you use this statement in your program, you might use it in the following form
Three dots (…): an Ellipsis
In the general form of a statement you may also notice three dots ( … ), also known as an “ellipsis”, following a list in an example. They are not part of the statement. An ellipsis indicates that you can have as many items in the list as you want. For example, the ellipsis in the general form of the statement
indicates that the list may contain more than two arguments. When you use this statement in your program, your statement might be something like this.
Square Brackets in Italics
The general form of some statements or methods (subprograms) may contain “square brackets” [ ] in italics, which indicate that the enclosed section is optional. For example, the general form of the statement
indicates that the section [, length] can be omitted.
For example, the following two statements may produce different results but they are both syntactically correct.
The Dark Header
Most of this book's examples are shown in a typeface that looks like this.
The header project_29.2-3 on top indicates the filename that you must open to test the program. All the examples that contain this header can be downloaded free of charge from my website.
Notices
Very often this book uses notices to help you better understand the meaning of a concept. Notices look like this.
This typeface designates a note.
Something Already Known or Something to Remember
Very often this book can help you recall something you have already learned (probably in a previous section or chapter). Other times, it will draw your attention to something you should memorize. Reminders look like this.
This typeface designates something to recall or something that you should memorize.
Although I have taken great care to ensure the accuracy of the content in this book, mistakes can still occur. If you come across any errors, either in the text or the code, I highly encourage you to send me a report. By doing so, you'll not only assist in saving other readers from potential confusion and frustration but also contribute to enhancing the quality of the next release. If you discover any errors, please report them by visiting one of the following addresses:
https://tinyurl.com/28nwh2nf
https://www.bouraspage.com/report-errata
.
Once I verify your reported error(s), your submission will be accepted. The errata will then be uploaded to my website and added to any existing list of corrections.
Material about this book, such as:
can be downloaded free of charge from the following addresses:
https://tinyurl.com/2wxdms79
https://www.bouraspage.com/books/cs-and-algorithmic-thinking-for-the-complete-beginner-third-edition
.
If you find this book valuable, please consider visiting the web store where you purchased it, as well as goodreads.com, to show your appreciation by writing a positive review and awarding as many stars as you think appropriate. By doing so, you will motivate me to keep writing and, of course, you'll be assisting other readers in discovering my work.
In today's society, almost every task requires the use of a computer. In schools, students use computers to search the Internet and to send emails. At work, people use them to make presentations, to analyze data, and to communicate with customers. At home, people use computers to play games, to connect to social networks and to chat with other people all over the world. Of course, don't forget smartphones such as iPhones. They are computers as well!
Computers can perform so many different tasks because of their ability to be programmed. In other words, a computer can perform any job that a program tells it to. A program is a set of statements (often called instructions or commands) that a computer follows in order to perform a specific task.
Programs are essential to a computer, because without them a computer is a dummy machine that can do nothing at all. It is the program that actually tells the computer what to do and when to do it. On the other hand, the programmer or the software developer is the person who designs, creates, and often tests computer programs.
This book introduces you to the basic concepts of computer programming using the C# language.
The term hardware refers to all devices or components that make up a computer. If you have ever opened the case of a computer or a laptop you have probably seen many of its components, such as the microprocessor (CPU), the memory, and the hard disk. A computer is not a device but a system of devices that all work together. The basic components of a typical computer system are discussed here.
This is the part of a computer that actually performs all the tasks defined in a program (basic arithmetic, logical, and input/output operations).
This is the area where the computer holds the program (while it is being executed/run) as well as the data that the program is working with. All programs and data stored in this type of memory are lost when you shut down your computer or you unplug it from the wall outlet.
ROM or Read Only Memory is a special type of memory which can only be read by the computer (but cannot be changed). All programs and data stored in this type of memory are not lost when the computer is switched off. ROM usually contains manufacturer's instructions as well as a program called the bootstrap loader whose function is to start the operation of computer system once the power is turned on.
This is usually the hard disk or the SSD (Solid State Drive), and sometimes (but more rarely) the CD/DVD drive. In contrast to main memory (RAM), this type of memory can hold data for a longer period of time, even if there is no power to the computer. However, programs stored in this memory cannot be directly executed. They must be transferred to a much faster memory; that is, the main memory.
Input devices are all those devices that collect data from the outside world and enter them into the computer for further processing. Keyboards, mice, and microphones are all input devices.
Output devices are all those devices that output data to the outside world. Monitors (screens) and printers are output devices.
Everything that a computer does is controlled by software. There are two categories of software: system software and application software.
When you turn on your computer, the main memory (RAM) is completely empty. The first thing the computer needs to do is to transfer the operating system from the hard disk to the main memory.
After the operating system is loaded to main memory, you can execute (run) any program (application software) you like. This is usually done by clicking, double clicking, or tapping the program's corresponding icon. For example, let's say you click on the icon of your favorite word processor. This action orders your computer to copy (or load) the word processing program from your hard disk to the main memory (RAM) so the CPU can execute it.
Programs are stored on secondary storage devices such as hard disks. When you install a program on your computer, the program is copied to your hard disk. Then, when you execute a program, the program is copied (loaded) from your hard disk to the main memory (RAM), and that copy of the program is executed.
The terms “run” and “execute” are synonymous and can be used interchangeably.
Computers can execute programs that are written in a strictly defined computer language. You cannot write a program using a natural language such as English or Greek, because your computer won't understand you!
But what does a computer actually understand? A computer can understand a specific low-level language called the machine language. In a machine language all statements (or commands) are made up of zeros and ones. The following is an example of a program written in a machine language, that calculates the sum of two numbers.
Shocked? Don't worry, you are not going to write programs this way. Hopefully, no one writes computer programs this way anymore. Nowadays, all programmers write their programs in a high-level language and then they use a special program to translate them into a machine language.
A high-level language is one that is not limited to a particular type of computer.
There are two types of programs that programmers use to perform translation: compilers and interpreters.
A compiler is a program that translates statements written in a high-level language into a separate machine language program. You can then execute the machine language program any time you wish. After the translation, there is no need to run the compiler again unless you make changes in the high-level language program.
An interpreter is a program that simultaneously translates and executes the statements written in a high-level language. As the interpreter reads each individual statement in the high-level language program, it translates it into a machine language code and then directly executes it. This process is repeated for every statement in the program.
The statements (often called instructions or commands) that the programmer writes in a high-level language are called source code or simply code. The programmer first types the source code into a program known as a code editor, and then uses either a compiler to translate it into a machine language program, or an interpreter to translate and execute it at the same time.
While it may seem uncommon nowadays, it's entirely possible to write programs using a simple text editor!
Choose true or false for each of the following statements.
Select the correct answer for each of the following statements.
C# (pronounced as “C sharp”) is a widely used general-purpose, high-level computer programming language that allows programmers to create desktop or mobile applications, large-scale applications, embedded systems, client-server applications, web pages, and many other types of software. C# is an extension of its predecessor, the C programming language.
C# is designed to be a platform-independent language. It is intended to let programmers “write once, run anywhere (WORA)”, meaning that code is written once but can run on any combination of hardware and operating system without being re-compiled.
Technically speaking, a script is interpreted whereas a program is compiled, but this is actually not their major difference. There is another more important difference between them!
The main purpose of a script written in a scripting language such as JavaScript, or VBA (Visual Basic for Applications) is to control another application. So you can say that, in some ways JavaScript controls the web browser, and VBA controls a Microsoft® Office application such as MS Word or MS Excel.
On the other hand, a program written in a programming language such as C#, C++, or Java (to name a few) executes independently of any other application. A program is executed as stand-alone any time the user wishes without the need of a hosting application.
Macros of Microsoft Office are scripts written in VBA. Their purpose is to automate certain functions within Microsoft Office.
A lot of people think that JavaScript is a simplified version of Java but in fact the similarity of the names is just a coincidence.
A script cannot be executed as stand-alone. It requires a hosting applicationin order to execute.
C# is what is known as a “high-level” computer language. C#'s coding style is similar to C language. It is quite easy to understand and highly efficient on multiple platforms such as Windows, Android, Linux, and Unix. C# is a flexible and powerful language, making it well-suited for developing large-scale applications, embedded systems, drivers, client-server applications, art applications, music players, or even video games.
C# is everywhere! It is on desktop computers, laptops, mobile devices, and even in data centers costing millions of dollars. With a huge community of developers worldwide, C# enables efficient development of many exciting applications and services. This huge availability of C# programmers is a major reason why organizations choose C# for new development over any other programming language. This is also a very good reason why you should actually learn C#!
Computers do not understand natural languages such as English or Greek, so you need a computer language such as C# to communicate with them. C# is a very powerful high-level computer language. The C# compiler (or, actually, a combination of two compilers) converts C# language to a language that computers can actually understand, and that is known as the “machine language”.
In the past, computer languages made use of either an interpreter or a compiler. Nowadays however, many computer languages including C# use two compilers. In C#, the first compiler translates statements into an intermediate language called Common Intermediate Language (CIL), which is a language similar to Java's bytecode. The CIL code is stored on disk in an executable file called an assembly, typically with an extension of .exe. Later, when a user wants to execute the file, .NET (pronounced as "dot net") performs a Just In Time (JIT) compilation to convert the CIL code into low-level machine language code for direct execution on the hardware.
.NET Framework is a version of .NET for building any type of app that runs on Windows.
.NET Core is a version of .NET for building websites, services, and console apps that run on Windows, Linux, or macOS
Instead of two compilers, some languages use a compiler and an interpreter. In Java, for example, the compiler translates Java statements into bytecode statements and saves them in a .class file. Later, when a user wants to execute a .class file, the Java Virtual Machine (JVM)—which is actually a combination of a compiler and an interpreter—reads the .class file and executes it, initially using interpretation. During interpretation, however, the JVM monitors which sequences of bytecode are frequently executed and translates them (compiles them) into low-level machine language code for direct execution on the hardware.
In Figure 2–1 you can see how statements written in C# are compiled into CIL code and how CIL code is then compiled and executed using the .NET.
.
Figure 2–1 Executing C# statements using the .NET
Now come some reasonable questions: Why all this trouble? Why does C# and other languages translate twice? Why are C# statements not directly translated into low-level machine language code? The answer lies in the fact that C# is designed to be a platform-independent programming language. This means that a program is written once but it can be executed on any device, regardless of its operating system or its architecture, as long as the appropriate version of .NET is installed on it. In the past, programs had to be recompiled, or even rewritten, for each computer platform. One of the biggest advantages of C# is that you only have to write and compile a program once! In Figure 2–2 you can see how statements written in C# are compiled into CIL code and how CIL code can then be executed on any platform that has the corresponding .NET installed on it.
Figure 2–2 Executing C# statements on different platforms
An Integrated Development Environment, or IDE, is a type of software that includes all the basic tools programmers need to write and test programs. An IDE typically contains a source code editor and integrates tools such as a compiler or an interpreter, along with a debugger. Visual Studio Community and Visual Studio Code are two examples of IDEs that let programmers write, execute and debug their source code.
A “debugger” is a tool that helps programmers to find and correct many of their mistakes.
Microsoft Visual Studio is an Integrated Development Environment (IDE) that provides a great set of tools for many programming languages (via extensions installed separately) and lets you easily create applications for Android, iOS, macOS, Windows, and the cloud, as well as websites, web applications, and web services.
Visual Studio is much more than a text editor. It can indent lines, match words and brackets, and highlight source code that is written incorrectly. It also provides automatic code (IntelliSense®), which means that as you type, it displays a list of possible completions. The IDE also provides hints to help you analyze your code and find any potential problems. It even suggests some simple solutions to fix those problems. You can use the Visual Studio not only to write but also to execute your programs directly from the IDE.
Visual Studio has a large community of users all around the world and this is why it comes in so many different flavors. Specifically, in Microsoft's download page you can download:
In the next chapter (Chapter 3), you will find links guiding you to instructions on how to install and configure whatever is necessary on your computer, such as Visual Studio Community or Visual Studio Code and .NET Core SDK, on either Windows or Linux. Then, in Chapter 9, you will discover guidance on using Visual Studio Community or Visual Studio Code to write, execute and debug C# programs. These instructions are available on my website. Additionally, you will find numerous tips and tricks there that will be valuable in your first steps as a budding programmer!
For the purposes of this book, you need to install an Integrated Development Environment (IDE) on your computer. Visual Studio Community and Visual Studio Code are examples of such IDEs. It's up to you to choose which one you are going to use.
Visual Studio Community is easy to install but runs only on Windows. On the other hand, Visual Studio Code runs on Windows, macOS, and Linux, but it requires a little bit more effort to install. Particularly, Visual Studio Community includes a C# compiler and debugger, while, Visual Studio Code requires installing them separately.
All the instructions you need regarding how to set up Visual Studio Community or Visual Studio Code and a C# compiler, on either Windows or Linux are maintained on my website at the following addresses. This gives me the flexibility to review them frequently and keep them up-to-date.
https://tinyurl.com/48nhbxd3
https://www.bouraspage.com/cs-setup-write-execute-debug
.
If you find any inconsistencies, please let me know, and I will update the instructions as soon as possible. To report issues, visit one of the following addresses:
https://tinyurl.com/28nwh2nf
https://www.bouraspage.com/report-errata
.
Currently, all you need is to install and configure an IDE. Instructions on my website regarding how to write, execute, and debug a C# program are unnecessary at this stage. You will require these instructions when you reach Chapter 9.
Across
1) Statements or commands.
3) Windows is such a system.
4) A computer component.