Introduction To Computer Science For Beginners
Introduction To Computer Science For Beginners
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 1
01
COURCE: SC50 Video lecture by DAVID J. MALAN
https://www.youtube.com/watch?v=8mAITcNt710
GUIDEBOOK OUTLINE
Week 0 Scratch
SL NO. SUBJECTS TIME STAMP PAGE NO.
4 Formula 07
5 ASCII [00:17:12] 08
6 ASCII TABLE 1 08
7 ASCII TABLE 2 09
8 UNICODE [00:21:38] 13
9 RGB [00:26:10] 13
10 MIDI [00:31:24] 14
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 2
01
programming language
13 Functions [00:58:24] 17
14 Variables 18
15 Boolean expressions 18
16 Conditionals 19
Week 1 C [1:47:45]
SL NO. SUBJECTS TIME STAMP PAGE NO.
18 Correctness [1:50:00] 20
19 Design 21
20 Style 22
21 Conclusion 23
24 "Hello, World!" 26
Integrated Development
25 [1:53:38] 27
Environment
30 Function [02:08:17] 31
31 Argument 32
32 String [02:09:26] 33
33 Side Effects 34
36 \n Means [02:16:43] 38
38 <sc50.h> Means 40
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 3
01
39 %s Means [02:24:00] 42
41 Syntax highlights 44
47 Operators [02:45:50] 55
50 Comment [02:55:20] 59
51 Constant [03:09:37] 60
60 Truncation [04:02:41] 75
62 Integer overflow 77
Week 0 Scratch
Unary notation [00:07:10]
Unary notation is a way of representing numbers using only a single digit or symbol.
In unary notation, the number of symbols used corresponds to the value of the
number being represented. For example, the number 4 can be represented in unary
notation as "||||".
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 4
01
Unary notation is not commonly used, as it is not very efficient. It can be difficult to
represent larger numbers in unary notation, as the number of symbols required
increases rapidly. However, it can be useful in certain contexts, such as when
working with very large numbers or in certain types of computer algorithms.
To count in binary, the computer starts with a value of 0 and increments the value by
1 each time. When it reaches the maximum value that can be represented with a
given number of bits (for example, 111 in three-bit binary), the next increment causes
the value to roll over to the minimum value (in this case, 000) and an additional
"carry" bit is added to the left to represent the extra value. This is similar to how
carrying works in the decimal system.
For example, here's how the computer would count from 0 to 10 in binary:
Decimal Binary
0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 5
01
💠 NOTE
These digits are called "bits," and they can be combined to represent
different values or instructions.
1. Start from the rightmost digit and work your way left.
2. For each 1 that you encounter, add the value of the place to the running total.
So, for the binary number 1110, you would do the following:
1. Start with the rightmost digit, which is 0. Its place value is 1, so you don't add
anything to the running total.
2. The next digit is 1. Its place value is 2, so you add 2 to the running total (which is
currently 0).
3. The next digit is 1. Its place value is 4, so you add 4 to the running total (which is
currently 2).
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 6
01
4. The final digit is 1. Its place value is 8, so you add 8 to the running total (which is
currently 6).
After adding up all the place values for the 1s in the binary number, you get a total of
14. Therefore, the binary number 1110 represents the decimal value of 14.
Formula
To convert a binary number to its decimal (base 10) equivalent, you can use the
following formula:
Where:
bn is the nth digit of the binary number, starting from the rightmost digit and
working left
For example, to convert the binary number 1110 to decimal, you would use the
following formula:
decimal = (1 x 0) + (2 x 0) + (4 x 1) + (8 x 1)
=0+0+4+8
= 14
EXAMPLE
In the binary numbering system, the symbol for the number 11 is 1011.
To understand why this is the case, you need to know how the place values of a
binary number work. Each place value in a binary number is double the value of the
place to its right. For example, the "ones" place has a value of 1, the "twos" place
has a value of 2, the "fours" place has a value of 4, and so on.
In the case of the number 11, you would need to use four digits to represent it in
binary, since the largest place value is 8 (which is less than 11). The binary
representation of 11 would be "1011," which stands for:
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 7
01
4. 1 in the "ones" place (1)
Adding up all these values gives you a total of 11, which is why the binary number
"1011" represents the decimal value of 11.
ASCII [00:17:12]
ASCII (pronounced "ask-ee") stands for American Standard Code for Information
Interchange. It is a standardized code used to represent characters (letters,
numbers, symbols, etc.) in computers and other devices.
Each character in the ASCII table is represented by a unique number between 0 and
127. For example, the letter "A" is represented by the number 65, the letter "B" is
represented by the number 66, and so on. ASCII codes are usually represented
using the binary numbering system since computers understand and process data in
the form of binary digits (bits).
ASCII is used to represent characters in many different contexts, including text files,
email messages, and web pages. It is a widely used and widely supported standard,
which makes it easy to exchange text and other data between different devices and
systems.
ASCII TABLE 1
Dec Bin Char
8 00011000 BS (backspace)
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 8
01
13 00011101 CR (carriage return)
ASCII TABLE 2
33 0021 00100001 !
35 0023 00100011 #
36 0024 00100100 $
37 0025 00100101 %
40 0028 00101000 (
41 0029 00101001 )
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 9
01
42 002A 00101010 *
43 002B 00101011 +
44 002C 00101100 ,
45 002D 00101101 -
46 002E 00101110 .
47 002F 00101111 /
48 0030 00110000 0
49 0031 00110001 1
50 0032 00110010 2
51 0033 00110011 3
52 0034 00110100 4
53 0035 00110101 5
54 0036 00110110 6
55 0037 00110111 7
56 0038 00111000 8
57 0039 00111001 9
58 003A 00111010 :
59 003B 00111011 ;
61 003D 00111101 =
63 003F 00111111 ?
64 0040 01000000 @
65 0041 01000001 A
66 0042 01000010 B
67 0043 01000011 C
68 0044 01000100 D
69 0045 01000101 E
70 0046 01000110 F
71 0047 01000111 G
72 0048 01001000 H
73 0049 01001001 I
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 10
01
74 004A 01001010 J
75 004B 01001011 K
76 004C 01001100 L
77 004D 01001101 M
78 004E 01001110 N
79 004F 01001111 O
80 0050 01010000 P
81 0051 01010001 Q
82 0052 01010010 R
83 0053 01010011 S
84 0054 01010100 T
85 0055 01010101 U
86 0056 01010110 V
87 0057 01010111 W
88 0058 01011000 X
89 0059 01011001 Y
90 005A 01011010 Z
91 005B 01011011 [
92 005C 01011100 \
93 005D 01011101 ]
94 005E 01011110 ^
95 005F 01011111 _
96 0060 01100000 `
97 0061 01100001 a
98 0062 01100010 b
99 0063 01100011 c
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 11
01
106 006A 01101010 j
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 12
01
💠 NOTE
For example, the letter "A" is represented by the number 65, and the
letter "B" is represented by the number 66
UNICODE [00:21:38]
Unicode is a computing industry standard for the consistent encoding,
representation, and handling of text expressed in most of the world's writing
systems. Unicode provides a unique number for every character, no matter what
platform, device, application, or language you are using.
Unicode includes ASCII and many other characters from a variety of scripts and
symbols. Unicode defines more than 128,000 different characters, including the
ASCII characters, which are the first 128 characters of the Unicode character set.
RGB [00:26:10]
RGB stands for red, green, and blue. It is a color model used in digital imaging and
computer graphics to represent colors using a combination of red, green, and blue
light. In the RGB color model, each color is represented by a value between 0 and
255, which corresponds to the intensity of that color. For example, (255, 0, 0)
represents pure red, (0, 255, 0) represents pure green, and (0, 0, 255) represents
pure blue. By combining different levels of red, green, and blue, it is possible to
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 13
01
create a wide range of colors.
The RGB color model is widely used because it is supported by most computer
displays and many other devices, such as printers and digital cameras. It is also
often used in web design and other types of digital media.
MIDI [31:24]
MIDI (Musical Instrument Digital Interface) is a technical standard that describes a
protocol, a digital interface, and a connector for communicating musical information.
MIDI allows computers, synthesizers, and other electronic musical instruments to
communicate with each other and to store, edit, and playback music.
MIDI messages are sent via a MIDI cable, which is a standard 5-pin DIN cable. MIDI
messages are made up of 8-bit words that are organized into two 4-bit nibbles. The
first nibble is the status nibble, which specifies the type of message being sent, and
the second nibble is the data nibble, which contains the actual data being
transmitted.
Note On/Off: these messages specify which note is being played and whether it
is being turned on or off
Program Change: these messages specify which instrument patch (i.e., sound)
is being used
Pitch Bend: these messages specify how the pitch of a note is being bent
MIDI has been widely used in the music industry since the 1980s, and it is still in
widespread use today.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 14
01
for helping to clarify and organize the logic of a program before actually writing it in a
programming language.
Here is an example of pseudocode that describes the steps of a program that
calculates the average of a list of numbers:
DEFINE sum as 0
DEFINE count as 0
This pseudocode describes a program that goes through a list of numbers, adds
them up, and then divides the sum by the number of elements in the list to find the
average. It then prints the average.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 15
01
💠 NOTE
RGB (red, green, blue) is a color model used in digital imaging and
graphics to represent colors using a combination of red, green, and
blue light
MIDI messages are sent via a standard 5-pin DIN cable and consist of
8-bit words organized into two 4-bit nibbles
MIDI has been widely used in the music industry since the 1980s and
is still in use today
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 16
01
Example of pseudocode: a program that calculates the average of a
list of numbers by adding up the numbers, dividing the sum by the
number of elements in the list, and printing the result.
Functions
In Scratch, a function is a block of code that performs a specific task and can be
reused in different parts of a program. Functions are a key concept in programming,
as they allow you to break down a large program into smaller, more manageable
pieces.
In Scratch, you can define your own functions using the "define" block, which is
found in the "More Blocks" category. To create a function, you first give it a name and
then add the blocks of code that you want the function to execute when it is called.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 17
01
To use this function in your program, you can simply drag and drop the "myFunction"
block wherever you want the code to be executed. For example, you might use the
function in a loop to make the character say "Hello, world!" multiple times.
Functions are a powerful tool in Scratch and can help you write more organized,
efficient code. They are especially useful when you have a block of code that you
want to use in multiple places in your program, as they allow you to reuse that code
without having to copy and paste it.
Variables
In the Scratch programming language, a variable is a piece of information that can
change, like a number or a string of text. You can use variables to store data, make
your programs more flexible and easier to read, and perform mathematical
operations.
In Scratch, you can create variables by clicking on the "Data" category in the blocks
palette, and then clicking on the "Make a Variable" button. You can also create
variables by right-clicking on the blocks palette and selecting "New Variable".
Once you have created a variable, you can use it in your program by dragging the
variable block from the blocks palette and placing it in your script. You can use the
"set" block to change the value of a variable, and the "change" block to modify the
value of a variable by a certain amount.
Controlling the behavior of your program, like changing the speed of a sprite
based on user input
Boolean expressions
In the Scratch programming language, a boolean expression is a statement that
evaluates to either "true" or "false". Boolean expressions are often used to test
conditions and control the flow of a program.
In Scratch, you can use boolean expressions by using the "true" and "false" blocks
from the "Looks" category in the blocks palette. You can also create boolean
expressions using comparison operators, such as "=", ">", "<", "≥", and "≤".
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 18
01
"Sprite1 is touching Sprite2" - This expression will be "true" if Sprite1 is touching
Sprite2, and "false" if Sprite1 is not touching Sprite2.
"Mouse down?" - This expression will be "true" if the mouse button is being
pressed, and "false" if the mouse button is not being pressed.
"Variable1 < Variable2" - This expression will be "true" if the value of Variable1 is
less than the value of Variable2, and "false" if the value of Variable1 is greater
than or equal to the value of Variable2.
You can use boolean expressions in conjunction with control blocks, such as "if" and
"if-else", to execute different code paths based on the evaluation of the boolean
expression.
Conditionals
In the Scratch programming language, a conditional is a type of control structure that
allows you to execute different code paths based on the evaluation of a boolean
expression.
In Scratch, you can use the "if" block to execute a code path if a boolean expression
is "true", and the "if-else" block to execute one code path if a boolean expression is
"true" and another code path if the boolean expression is "false".
if <mouse down?>
move (10) steps
else
say [Hello!] for (2) seconds
In this example, the sprite will move 10 steps if the mouse button is being pressed
and say "Hello!" for 2 seconds if the mouse button is not being pressed.
You can use conditionals to test for a wide variety of conditions in Scratch, such as
the value of a variable, the position of a sprite, or the presence of a particular sprite
on the stage.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 19
01
"forever" loops, which repeat a block of code indefinitely
"while" loops, which repeat a block of code until a certain condition is met
You can use loops to repeat actions, such as moving a sprite or playing a sound, or
to perform a series of actions in a certain order.
In Scratch, you can use the "forever" block to create a forever loop, the "repeat"
block to create a repeat loop, and the "while" block to create a while loop.
Here's an example of a forever loop in Scratch:
forever
move (10) steps
turn right (15) degrees
In this example, the sprite will move 10 steps and turn right 15 degrees indefinitely.
As for events, Scratch includes a number of "event blocks" that allow you to trigger
code execution based on certain events, such as the start of the program, the press
of a key, or the receipt of a message.
For example, you can use the "when flag clicked" block to execute a code path when
the green flag is clicked, or the "when [space] key pressed" block to execute a code
path when the space key is pressed.
Week 1 C [1:47:45]
Correctness [1:50:00]
In programming, correctness refers to the quality of a program or algorithm in
producing the correct results when it is executed. A program is considered valid if it
has the expected output for a given set of inputs and follows the rules and
conventions of the programming language it is written in.
Testing: This involves running the program with a variety of inputs and verifying
that the output is correct.
Debugging: This involves finding and fixing errors (also known as bugs) in the
program.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 20
01
Code review: This involves having other programmers review and critique the
code to ensure that it is correct and follows good programming practices.
Documentation: This involves writing clear and accurate documentation for the
program, including explanations of the algorithms and data structures used, to
help ensure that the program is understood and can be maintained over time.
💠 NOTE
Design
In programming, design refers to the process of planning and organizing the
structure and features of a program or system. Design is an important part of the
software development process and involves identifying the requirements of the
program, determining the best solution to meet those requirements, and creating a
plan to implement that solution.
There are several different approaches to software design, including:
Structured design: This approach involves breaking the program down into
smaller, independent parts that can be easily understood and modified.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 21
01
the program; the view, which represents the user interface; and the controller,
which mediates between the model and the view.
💠 NOTE
Style
In programming, style refers to the conventions and guidelines used for writing and
formatting code. Style includes things like the use of indentation, naming
conventions, commenting, and the overall layout of the code.
There are several reasons why following a consistent style is important in
programming:
Clarity: A consistent style makes the code easier to read and understand, which
is important for maintaining and modifying the code over time.
Collaboration: When multiple people are working on the same codebase, having
a consistent style helps to reduce conflicts and makes it easier for everyone to
work together.
Quality: Adhering to a consistent style helps to ensure that the code is of high
quality and follows good programming practices.
There are several widely used style guides for different programming languages,
such as the Google Python Style Guide and the Airbnb JavaScript Style Guide. It is
a good idea to follow a style guide when writing code to ensure that it is consistent
and easy to read.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 22
01
💠 NOTE
Conclusion
Correctness, design, and style are all related in that they all contribute to the overall
quality of a program.
Correctness is essential because it ensures that the program will produce the
desired results. If a program is not correct, it will not function as intended and may
produce incorrect or unexpected results.
Design is important because it determines the structure and organization of the
program, which in turn affects its correctness, maintainability, and scalability. A well-
designed program is easier to understand, modify, and debug, which helps to ensure
its correctness.
Style is important because it affects the readability and understandability of the code,
which is important for maintaining and modifying the program over time. A consistent
style makes the code easier to read and understand, which in turn helps to ensure
its correctness.
In short, good design and style contribute to the overall quality and correctness of a
program by making it easier to understand and maintain.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 23
01
Debugging: VSCode has a built-in debugging tool that allows you to set
breakpoints, step through your code, and inspect variables. This can help you
find and fix errors in your code more easily.
Integrated Terminal: You can run C language code by using the terminal in
VSCode. It allows you to run commands and tasks directly inside the editor.
To start using VSCode for C development, you'll need to install the C/C++ extension.
Once you've done that, you can start creating new C projects, editing C source code,
and building and running your programs directly from within VSCode. It's a good idea
to also install a linter to help you with potential errors before you even run your code,
like c/c++ extension, cpplint, cppcheck.
💠 NOTE
You can create, edit, build and run C programs directly from within
VSCode.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 24
01
Syntax highlighting is a feature found in many code editors that is used to visually
distinguish different parts of the code based on their syntax. This makes it easier for
developers to read and understand the code by providing a visual cue for different
elements of the language, such as keywords, comments, variables, and functions.
In a code editor, different parts of the code are typically assigned different colors or
font styles to indicate their syntax. For example, keywords may be colored blue,
comments may be colored green, and strings may be colored red. This makes it
easy to quickly identify different parts of the code and understand its meaning.
Most code editors nowadays have built-in syntax highlighting for various languages,
including C. But if you are using a code editor that doesn't have built-in syntax
highlighting for C, you can often install a plugin or extension to add syntax
highlighting for C.
Syntax highlighting can also be customized, many editors have settings where you
can adjust the color scheme, or create your own color scheme that makes it easy for
you to read and understand the code.
In addition to syntax highlighting, many code editors also have other features to help
you write and read code more easily, such as code folding, code snippets, code
completion, and go-to-definition.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 25
01
💠 NOTE
Different parts of the code are assigned different colors or font styles
to indicate their syntax.
This feature makes it easy to quickly identify different parts of the code
and understand its meaning.
Other features like code folding, code snippets, code completion, and
go-to-definition can also be found in many code editors to make the
coding experience easier.
"Hello, World!"
"Hello, World!" program written in C:
#include <stdio.h>int
int main(void)
{
printf("Hello, World!\n");
}
To compile and run this program, you would first save it to a file with a .c extension
(for example, hello.c ), and then use a C compiler to compile it. For example, on a
Unix-like system, you could use the following command to compile the program:
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 26
01
This would create an executable file called hello , which you could then run by
typing ./hello at the command prompt. When you run the program, it should print
"Hello, World!" to the console.
Code completion: Many IDEs provide code completion features, which suggest
code snippets or complete statements as you type, to help you write code more
quickly and accurately.
Debugging: IDEs often include debugging tools that allow you to step through
your code, set breakpoints, and inspect variables to help you find and fix errors.
Project management: IDEs often provide features for managing and organizing
your code, such as the ability to create projects, manage files, and navigate
through your code more easily.
Integration: IDEs often provide integration with other tools, such as version
control systems, build systems, and testing frameworks, to help streamline the
development process.
💠 NOTE
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 27
01
Command Line Interface (CLI) [2:00:20] [02:38:09]
A Command Line Interface (CLI) is a text-based interface for interacting with a
computer or operating system. It allows you to enter commands using a keyboard,
and the computer or operating system responds by performing the requested
actions.
CLIs are typically used in a terminal or command prompt window, and they are often
preferred by experienced users and developers because they offer more control and
flexibility than graphical user interfaces (GUIs).
To use a CLI, you enter commands by typing them into the terminal window and
pressing the Enter key. The command is then executed, and the output is displayed
in the terminal window.
Some examples of common CLI commands include:
CLIs are often used to perform advanced tasks and automate processes, and they
are commonly used in conjunction with scripts and programming languages.
💠 NOTE
CLI offers more control and flexibility than graphical user interfaces
(GUIs) and is preferred by experienced users and developers.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 28
01
“./ “command [02:01:27]
In a C program ./ is used to specify the current directory when running an
executable file. For example, if you have compiled a C program and created an
executable file called myprogram , you can run the program by typing ./myprogram at
the command prompt.
The ./ notation is used to indicate that the file is located in the current directory. If
you omit the ./ , the operating system may not be able to find the file, especially if it
is not in one of the directories listed in the PATH environment variable.
The first command compiles the C program and creates an executable file called
myprogram . The second command runs the myprogram executable.
Windows: These are rectangular areas that can be opened, closed, minimized,
and resized on the screen. Each window typically contains a specific application
or document.
Icons: These are small graphics that represent applications, files, or actions.
Users can typically click on an icon to open an application or perform a task.
Menus: These are lists of options that can be accessed by clicking on a button or
menu bar.
Pointers: These are graphical objects that appear on the screen and allow users
to interact with the GUI by pointing and clicking on objects.
Some popular operating systems with GUI interfaces include Windows, macOS, and
Linux with a desktop environment such as GNOME or KDE.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 29
01
💠 NOTE
Machine code, also known as machine language or object code, is the low-level
code that is directly executed by a computer's central processing unit (CPU). It
consists of a series of binary digits (ones and zeros) that represent the instructions
that the CPU is to execute. Machine code is not easy for humans to read or
understand, as it consists of a series of numbers and symbols that have no meaning
to us.
To create a program, a programmer writes the source code in a text editor and then
uses a compiler to translate the source code into machine code. The machine code
is then saved as an executable file, which can be run on a computer to perform the
tasks specified in the source code.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 30
01
💠 NOTE
Function [02:08:17]
In C, a function is a block of code that performs a specific task and optionally returns
a value. Functions are a useful way to organize and reuse code, as they allow you to
define a set of instructions once and then call them multiple times from different parts
of your program.
This function, called area , takes two arguments: width and height , which represent
the width and height of the rectangle. The function calculates the area of the
rectangle by multiplying the width and height and then returns the result using the
return statement.
To call a function in C, you use the function's name followed by a set of parentheses
containing the arguments. For example, to call the area function with arguments 3
and 4 , you would write:
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 31
01
int result = area(3, 4);
This would call the area function with the arguments 3 and 4 , and the result (12)
would be stored in the variable result .
💠 NOTE
Functions are a useful way to organize and reuse code, allowing you
to define a set of instructions once and then call them multiple times
from different parts of your program.
The function's syntax includes the return type, name, and parameters.
Argument
In programming, an argument is a value or expression that is passed to a function or
method when it is called. Arguments are used to provide additional information or
context to the function or method, and they are typically used to customize or modify
the behavior of the function or method.
For example, consider the following C function that takes two arguments:
In this example, x and y are the arguments of the add function. When the add
function is called, it expects the caller to pass two integer values, which are then
stored in the variables x and y . The function then adds these values together and
returns the result.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 32
01
Here is an example of calling the add function with arguments:
In this example, the add function is called with arguments 3 and 4 . The function
adds these values together and returns the result, which is then stored in the
variable result .
💠 NOTE
String [02:09:26]
In C programming, a string is a sequence of characters that is stored in an array and
is used to represent and manipulate text. Strings are an important data type in C,
and they are used in a wide variety of applications.
char str[10];
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 33
01
char str[10] = "Hello";
This assigns the string "Hello" to the str array. It is important to note that the size of
the array must be sufficient to hold the string and the null terminator character
( '\0' ), which indicates the end of the string.
To access the characters in a string, you can use array notation, like this:
char c = str[0];
This retrieves the first character of the str array (the letter 'H') and assigns it to the
variable c .
Strings are an important aspect of C programming, and there are numerous built-in
functions available for manipulating and formatting them. Examples of string
functions include strlen , which returns the length of a string, and strcat , which
concatenates (joins) two strings.
Side Effects
In programming, a side effect is an effect of a function or operation that is not the
primary purpose of the function or operation. Side effects can occur when a function
or operation modifies something outside of its local scope, such as a global variable
or an external resource.
Side effects can be intentional or unintentional, and they can have a variety of
consequences. For example, a function might have the primary purpose of
performing a calculation, but it might also have the side effect of updating a global
variable or writing to a file.
In general, it is generally considered good programming practice to minimize side
effects, as they can make code more difficult to understand and debug. This is
because side effects can have unintended consequences, and they can make it
harder to predict the behavior of a program.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 34
01
This function has the primary purpose of adding two numbers and returning the
result. However, it also has the side effect of updating the global variable global_sum
with the sum of the two numbers. This side effect might be intentional, but it can
make the function more difficult to understand and predict, as the value of global_sum
will be affected every time the function is called.
💠 NOTE
To return a value from a function or method, you can use the return statement. For
example, consider the following C function:
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 35
01
return x + y;
}
This function takes two arguments, x and y , and returns their sum. The return
statement indicates that the function should return the value of x + y to the caller.
To use the return value of a function or method, you can assign it to a variable or use
it in an expression. For example:
This calls the add function with the arguments 3 and 4 , and assigns the return
value (7) to the variable result .
💠 NOTE:
Functions and methods are usually defined with a return type, which
specifies the data type of the return value.
To return a value from a function or method, you can use the return
statement.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 36
01
variable = value;
Here, variable is the name of the variable to which the value should be assigned,
and value is the value to be assigned. The value on the right side of the = sign is
assigned to the variable on the left side.
int x = 5;
This code uses the assignment operator to assign the value 5 to the variable x .
After this assignment, the value of x will be 5 .
It is important to note that the = sign should not be confused with the == sign, which
is the equality operator. The equality operator is used to test if two values are equal,
and it has the following syntax:
if (x == y)
{
// do something
}
Here, x and y are compared to see if they are equal. If they are equal, the code
inside the if statement will be executed. If they are not equal, the code will be
skipped.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 37
01
💠 NOTE:
value to be assigned.
The value on the right side of the = sign is assigned to the variable on
the left side.
The = sign should not be confused with the == sign, which is the
equality operator.
The equality operator is used to test if two values are equal, and its
syntax is: if (x == y) { ... } , where x and y are the values to be
compared. If x and y are equal, the code inside the if statement is
executed; otherwise, the code is skipped.
\n Means [02:16:43]
In C, \n is a special escape sequence that represents a newline character. When a
string containing \n is printed, the output is displayed on a new line.
For example, consider the following C code:
This code prints the string "Hello\nWorld" to the console. Because the string contains
the \n escape sequence, the output is displayed on two separate lines:
Hello
World
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 38
01
The \n escape sequence can be used anywhere within a string to force a line
break. For example:
Line 1
Line 2
Line 3
Escape sequences are used to represent special characters that cannot be typed
directly into a string, such as a newline ( \n ), tab ( \t ), and backslash ( \\ ). There
are many other escape sequences in C, and you can find a complete list in the
documentation for the printf function.
💠 NOTE:
There are many other escape sequences in C, and a complete list can
be found in the documentation for the printf function.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 39
01
The stdio.h header file is a standard part of most C compilers and is included in
many C programs using the #include preprocessor directive. For example:
In this example, the stdio.h header file is included at the top of the program, and the
printf function is used to print the string "Hello, World!" to the console. The printf
function is defined in the stdio.h header file, so it must be included in order to use it.
The stdio.h header file contains a wide range of functions for input and output,
including functions for reading and writing to the console, files, and other streams.
Some examples of functions defined in stdio.h include printf , scanf , fprintf ,
fscanf , and puts .
💠 NOTE:
The stdio.h header file contains a wide range of functions for input
and output, including functions for reading and writing to the console,
files, and other streams.
<sc50.h> Means
is a header file used in the CS50 library, which is a set of tools and libraries
<cs50.h>
for computer science education. The CS50 library is used in the Introduction to
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 40
01
Computer Science course (CS50) at Harvard University, and it is designed to make it
easier to teach and learn computer science concepts.
The <cs50.h> header file contains definitions and prototypes for a variety of functions
and data types that are used in the CS50 course. These functions and data types
are designed to provide a simple and consistent interface for performing common
tasks in C programming.
For example, the <cs50.h> header file defines the string data type, which is a
convenient way to represent and manipulate strings in C. It also defines functions
such as GetString , which reads a string from the user, and GetInt , which reads an
integer from the user.
To use the <cs50.h> header file in a C program, you can include it at the top of your
program using the #include preprocessor directive. For example:
This code includes the <cs50.h> header file and uses the GetString function to read
a string from the user. The string is then printed to the console using the printf
function.
💠 NOTE:
These functions and data types are designed to provide a simple and
consistent interface for performing common tasks in C programming.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 41
01
%s Means [02:24:00]
In C programming, %s is a placeholder in a format string that represents a string. It is
used in conjunction with the printf function to print a string to the console.
The printf function is a standard C function that is used to print output to the
console. It takes a format string as its first argument, followed by a list of arguments
corresponding to the format string's placeholders.
The %s placeholder is used to indicate that a string argument should be inserted into
the output at that point. For example, consider the following code:
This code defines a string variable called name and assigns it the value "John". It
then uses the printf function to print the string "Hello, John!" to the console. The %s
placeholder in the format string is replaced with the value of the name variable.
The printf function can take many different placeholders, including %s for strings,
%d for integers, %f for floating-point numbers, and %c for characters. You can find a
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 42
01
💠 NOTE:
for characters.
The above example would print the string "The value of x is:" followed by the value of
the integer variable x .
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 43
01
💠 NOTE
printf takes a format string as its first argument, which specifies how
the subsequent arguments should be formatted and displayed.
Example: printf("The value of x is: %d", x); This will print "The value of
x is:" followed by the value of the integer variable x.
Syntax highlights
Syntax highlighting is a feature of some text editors and integrated development
environments (IDEs) that makes it easier to read and write code by applying different
colors and font styles to different parts of the code, such as keywords, operators,
comments, and strings.
Different programming languages have their own syntax, and syntax highlighting
needs to be adjusted accordingly. It's supported by many source code editors and
IDEs like VSCode, Sublime, Atom, and many more.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 44
01
💠 NOTE
It can also make the code more readable and easier to understand.
A source file that wants to use the declarations in a header file has to include it using
preprocessor directive #include . The preprocessor looks for the header file in
predefined system paths and includes paths of the compiler.
Example:
#include <stdio.h>
int main()
{
printf("Hello, World!");
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 45
01
return 0;
}
In the above example, #include <stdio.h> directive tells the preprocessor to include
the contents of the "stdio.h" header file in this source file before the compiler
processes it.
💠 NOTE
Header files usually have a file name with the ".h" extension but it is
not necessary.
A source file that wants to use the declarations in a header file has to
include it using preprocessor directive #include.
The preprocessor looks for the header file in predefined system paths
and includes paths of the compiler.
pwd : This command displays the current working directory, which is the directory
that the command-line prompt is currently set to.
cd : This command changes the current working directory. You can specify a
directory path as an argument to move to a specific directory. For example, cd
Documents will change the working directory to a directory called "Documents"
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 46
01
ls : This command lists the contents of the current working directory. You can
also specify a directory path as an argument to list the contents of a specific
directory.
.. : This token represents the parent directory of the current working directory.
It can be used with cd command to move to the parent directory. For example,
cd .. will move to the parent directory of the current working directory.
/ : This token represents the root directory of the file system. You can use it as
an argument for cd command to move to the root directory of the file system.
For example, cd / will move to the root directory.
pushd and popd : These commands that are used to navigate between
directories quickly. pushd allows you to switch to a new directory while also
keeping track of the previous directory and popd allows you to switch back to the
previous directory.
These are some of the most common commands and techniques for navigating in
the CLI, and it can be combined and used in conjunction with other commands,
options, and redirection and pipelining to perform more advanced tasks.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 47
01
💠 NOTE
Integer types:
char : It's a signed or unsigned integer type that typically has a size of 8
bits, but it can also be 16 or 32 bits in some systems. It's used to store
characters, ASCII values, and small integers.
short : It's a signed or unsigned integer type that typically has a size of 16
bits, it's used to store integers that are larger than char and smaller than
int .
int : It's a signed or unsigned integer type that typically has a size of 32
bits, it's the most common integer type used in C programs.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 48
01
long: It's a signed or unsigned integer type that typically has a size of 32 or
64 bits, it's used to store integers that are larger than int .
long long : It's a signed or unsigned integer type that typically has a size of
64 bits, it's used to store integers that are larger than long .
Floating-point types:
float : It's a 32-bit floating-point type, used to store decimal numbers with
single precision.
double: It's a 64-bit floating-point type, used to store decimal numbers with
double precision.
long double : it's an 80-bit or more precision floating point number, used to
store decimal numbers with more precision than double .
Pointer types:
type* : A pointer to an object of type type , where type can be any valid C
data type.
Array types:
Structure types:
Union types:
different data types, but only one member can be active at a time.
Enumerated types:
constants.
Boolean types:
or bool : A type that can hold only two values, true and false , it's
_Bool
not a built-in data type in C but can be implemented using _Bool or bool
keyword, stdbool.h header defines it.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 49
01
Additionally, C also has some special keywords and qualifiers such as const ,
volatile , signed , unsigned , restrict that can be used to modify the behavior or
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 50
01
💠 NOTE
Integer types:
short: 16-bit integer, used to store integers larger than char and
smaller than int .
long long : 64-bit integer, used to store integers larger than long .
Floating-point types:
Pointer types:
Array types:
Enumeration types:
Structure types:
Union types:
union : Similar to struct, but all members share the same memory
location.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 51
01
Boolean type:
It's important to note that in C the size and range of data types are
machine dependent, and the sign of integer variable can be specified
using signed and unsigned keywords.
get_int() : reads an integer from the user and returns it. It also checks for errors
such as non-numeric input, and prompts the user to re-enter the input until a
valid integer is received.
get_float() : reads a float value from the user and returns it. It also checks for
errors such as non-numeric input and prompts the user to re-enter the input until
a valid float value is received.
get_long_long() : reads a long long value from the user and returns it. It also
checks for errors such as non-numeric input and prompts the user to re-enter the
input until a valid long long value is received.
get_string() : reads a string of characters from the user and returns it. It also
checks for errors such as an empty string or input that exceeds a specified
maximum length and prompts the user to re-enter the input until a valid string is
received.
get_char() : reads a single character from the user and returns it. It also checks
for errors such as input that exceeds a specified maximum length and prompts
the user to re-enter the input until a valid character is received.
These functions are very convenient and easy to use, they prompt the user for input
and then validate that input before returning it to the program. They can save a lot of
time and simplify the process of reading input from the user, especially when it
comes to input validation.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 52
01
It's worth noting that for all these functions to work correctly, cs50.h must be included
in your code.
💠 NOTE
These functions are very convenient and easy to use, they prompt the
user for input and then validate that input before returning it to the
program.
They can save a lot of time and simplify the process of reading input
from the user, especially when it comes to input validation.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 53
01
%p : This format specifier is used to print or read a pointer.
%i : This format specifier is used to print or read signed integers. It works the
same as %d .
%li : This format specifier is used to print or read signed long integers. It's
useful for larger integers.
%lu : This format specifier is used to print or read unsigned long integers.
%lld : This format specifier is used to print or read signed long long integers.
%llu : This format specifier is used to print or read unsigned long long integers.
The format specifiers should be used in combination with the printf() and scanf()
functions to print or read the data respectively. Also, it is important to match the type
of the variable with the format specifier when using these functions, otherwise, it may
lead to unexpected behavior or undefined behavior.
💠 NOTE
In C, format specifiers are used with the printf() and scanf() functions
to control how data is formatted and read.
It's important to match the type of the variable with the format specifier
when using printf() or scanf() functions otherwise it may lead to
unexpected behavior or undefined behavior.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 54
01
Operators [02:45:50]
The following is a list of operators that are commonly used in C:
Arithmetic operators: +, -, *, /, %
Conditional operator: ? :
It's also important to use parentheses to group expressions and clarify the order of
operations.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 55
01
💠 NOTE
Arithmetic operators: +, -, *, /, %
Conditional operator: ? :
The name of a variable is called an identifier and it must follow the rules for naming
variables in the specific programming language. In C, variable names can only
contain letters, digits, and the underscore character, and they cannot begin with a
digit.
Syntactic sugar is a term used to describe language features that make the code
more readable or convenient for the programmer but do not add any new
functionality. These features are not strictly necessary for the functioning of the
program, but they make it easier for the programmer to write and understand the
code.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 56
01
For example, C99 introduced a feature called "variable-length arrays" which allows
you to declare arrays whose size is determined at runtime. This is a new feature in C
that was not present in earlier versions of the language.
Another example is the for loop. In C, we can accomplish the same functionality of
a for loop with a while loop, but the for loop is more readable and thus more
convenient to use.
In short, Syntactic sugar is a way to make the code more readable and convenient
for the programmer, but it does not add any new functionality to the programming
language.
💠 NOTE
Variables are typically used to store data that can change during the
execution of a program, and their name is called an identifier.
2. Add the following line at the top of the file to include the CS50 library and the
standard input/output library:
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 57
01
3. Define the main function and include the void argument:
int main(void)
{
4. Use the get_int function from the CS50 library to prompt the user to enter an
integer value for x . Store the value in a variable named x :
5. Use the get_int function again to prompt the user to enter an integer value for
y . Store the value in a variable named y :
6. Add the values of x and y and store the result in a variable named sum :
int sum = x + y;
7. Use the printf function to print the result using the %i format specifier:
./add
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 58
01
12. The program will prompt the user to enter the x and y values and will print the
sum of x and y values.
Make sure that the CS50 library is installed on your system and that the
headers(cs50.h) are properly included in the program before compiling.
Comment [02:55:20]
In a C program, a comment is a line or block of text that is ignored by the compiler
and is not executed as part of the program. Comments are used to add notes,
explanations, and documentation to a program to make it easier for others to
understand and maintain the code.
Single-line comments begin with the // characters and continue to the end of
the line. Anything on the right of // is considered as a comment
int main()
{
// This is a single-line comment
int x = 5; // This is also a single-line comment
//...
}
Multi-line comments begin with the /* characters and end with the /
int main()
{
/*
This is a multi-line comment
that can span
multiple lines
*/
int x = 5;
//...
}
It is a good practice to use comments to document your code, to explain what the
code is doing, and to make the code more readable. It will also help you and any
other developer who needs to read and maintain the code in the future.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 59
01
Constant [03:09:37]
A constant in C is a value that cannot be modified during the execution of a program.
Constants can be defined in several ways in C:
#define PI 3.14159
int main() {
double radius = 5.0;
double area = PI * radius * radius;
//...
}
int main() {
const double PI = 3.14159;
double radius = 5.0;
double area = PI * radius * radius;
//...
}
When using constant, you should use UPPERCASE characters and separate words
with an underscore for better readability.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 60
01
💠 NOTE
A good practice is to only change the constant in one place and it will
be reflected throughout the program.
Here is a simple example that demonstrates how the "||" operator can be used in a C
program:
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 61
01
if (age >= 21 || money >= 1000) {
printf("You are allowed to enter the club.\n");
} else {
printf("You are not allowed to enter the club.\n");
}
return 0;
}
In this example, the, if statement tests whether the value of the variable "age" is
greater than or equal to 21 OR the value of the variable "money", is greater than or
equal to 1000. If either of these conditions is true, the code inside the if statement is
executed and the message "You are allowed to enter the club." is printed to the
screen. If both conditions are false, the code inside the else statement is executed
and the message "You are not allowed to enter the club." is printed to the screen.
2. Next, the, if statement is used to test whether the value of the "age" variable is
greater than or equal to 21 OR the value of the "money" variable, is greater than
or equal to 1000.
3. If either of these conditions is true, the code inside the if statement is executed,
which prints the message "You are allowed to enter the club." to the screen.
4. If both conditions are false, the code inside the else statement is executed and
the message "You are not allowed to enter the club." is printed to the screen.
5. The program then terminates and returns control to the operating system.
The "||" operator is often used in conditional statements such as "if" or "while" to test
multiple conditions at the same time. It is useful when you want to allow multiple
conditions to be true before executing a certain piece of code.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 62
01
💠 NOTE
The operator is represented by two vertical bars (||) and is often used
in conditional statements such as "if" or "while".
Here is a simple example that demonstrates how the "&&" operator can be used in a
C program:
return 0;
}
In this example, the, if statement tests whether the value of the variable "age" is
greater than or equal to 21 AND the value of the variable "money", is greater than or
equal to 1000. If both of these conditions are true, the code inside the if statement is
executed and the message "You are allowed to enter the club." is printed on the
screen. If either of these conditions is false, the code inside the else statement is
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 63
01
executed and the message "You are not allowed to enter the club." is printed to the
screen.
2. Next, the, if statement is used to test whether the value of the "age" variable is
greater than or equal to 21 AND the value of the "money" variable, is greater
than or equal to 1000.
3. If both conditions are true, the code inside the if statement is executed, which
prints the message "You are allowed to enter the club." to the screen.
4. If either of the conditions is false, the code inside the else statement is executed
and the message "You are not allowed to enter the club." is printed on the
screen.
5. The program then terminates and returns control to the operating system.
The "&&" operator is often used in conditional statements such as "if" or "while" to
test multiple conditions at the same time. It is useful when you want to ensure that
multiple conditions are true before executing a certain piece of code.
It is important to note that the "&&" operator has higher precedence than "||"
operator. This means when both operators appear in the same statement, the "&&"
operator will be evaluated first.
💠 NOTE
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 64
01
The while loop in C allows you to repeatedly execute a block of code as long as a
certain condition is true. The keyword "true" is a constant value that is always
considered to be true in a boolean context. So, when the condition "true" is used as
the loop's controlling condition, the loop will run indefinitely, until it is explicitly
terminated.
return 0;
}
In this example, the while loop will run indefinitely, because the condition "1" or "true"
is always true. The code inside the loop increments the value of the variable "i" by 1
and prints its value to the screen on each iteration.
1. The program starts by declaring an integer variable "i" and initializing it with the
value 0.
2. The while loop is used with the condition "1" or "true" as the controlling condition.
3. The code inside the loop increments the value of the variable "i" by 1 and prints
its value to the screen on each iteration.
It is important to note that an infinite loop like this will run forever if there is no
termination condition specified inside the loop. Therefore, it's crucial to have a
termination condition in the loop to avoid an infinite loop, as it will cause the program
to hang or crash. For example, you can use a counter variable, like i , to keep track
of how many times the loop has run, and use an if statement inside the loop to check
whether the counter has reached a certain value, and if so, use the break statement
to exit the loop.
while (1) {
printf("i = %d\n", i);
i++;
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 65
01
if(i == 10)
break;
}
In the above example, the loop will run for 10 iterations and then break out of the
loop and the program continues further.
💠 NOTE
Example:
int i = 0;
while (1) { // or while(true)
printf("i = %d\n", i);
i++;
}
The above example, the while loop will run indefinitely, because the
condition "1" or "true" is always true.
It is important to note that an infinite loop like this will run forever if
there is no termination condition specified inside the loop.
e.g. you can use a counter variable, like i , to keep track of how many
times the loop has run, and use an if statement inside the loop to
check whether the counter has reached a certain value, and if so, use
the break statement to exit the loop.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 66
01
In C, the "<=" operator is a comparison operator that is used to determine whether
one value is less than or equal to another. The operator is represented by the less-
than symbol (<) and the equal-to symbol (=) together ( <= ).
The "<=" operator is used to compare two values and returns a Boolean value of true
if the value on the left side of the operator is less than or equal to the value on the
right side of the operator, and false otherwise.
if (x <= y) {
printf("x is less than or equal to y.\n");
} else {
printf("x is greater than y.\n");
}
return 0;
}
In this example, the if statement tests whether the value of x is less than or equal to
the value of y. Since the value of x is 5 and the value of y is 7, x is less than y, and
the code inside the if statement is executed, which prints the message "x is less than
or equal to y." to the screen.
The "<=" operator can also be used in while loops as a controlling condition. For
example, you can use a while loop that runs while a variable is less than or equal to
a certain value.
The above example, the while loop will run until the value of the variable i is greater
than 10
The "<=" operator is one of the basic comparison operators in C, and it is frequently
used in many programming constructs such as if-else statements, while loops, and
for loops to control the flow of the program based on the comparison results.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 67
01
for loop [03:28:58]
The for loop in C is a control flow statement that allows you to execute a block of
code multiple times. It is often used when you want to iterate over a sequence of
values, such as an array or a list.
1. The initialization: This is executed only once before the loop starts. It is used to
initialize the loop variable, often called the counter.
Here is an example of a for loop that iterates 10 times and prints the value of the
loop variable on each iteration:
return 0;
}
In this example, the for loop initializes the loop variable i to 0, the condition is i<10,
and the increment is i++. The code inside the loop prints the value of i on each
iteration.
The for loop is a versatile control flow statement in C that can be used to perform a
wide variety of tasks, from simple counting to more complex operations like
traversing and manipulating data structures. It's a useful tool that allows you to
repeat a block of code a specific number of times, which makes it very powerful and
efficient.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 68
01
💠 NOTE
The for loop in C is a control flow statement that allows you to execute
a block of code multiple times.
Syntax:
1. Declare the function: At the top of your C program, or in a header file, you need
to declare the function with its name, return type, and the types and names of its
parameters. The general syntax for declaring a function is as follows:
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 69
01
1. Define the function: After declaring the function, you need to define its body. The
definition of the function starts with the name of the function, followed by the
parameter list in parentheses, and then a block of code enclosed in curly braces.
The general syntax for defining a function is as follows:
1. Call the function: To use a function in your C program, you need to call it by its
name, followed by the values of its parameters in parentheses. The general
syntax for calling a function is as follows:
Here is an example of a custom function in C that takes two integers as input and
returns their sum:
int main() {
int x = 3;
int y = 4;
int result = add(x, y);
printf("The sum of %d and %d is %d\n", x, y, result);
return 0;
}
In this example, the function "add" is declared with a return type of int, and two
parameters of type int: "a" and "b". The function body simply returns the sum of "a"
and "b". The function is called in the main function, passing the values of x and y as
arguments. The output will be "The sum of 3 and 4 is 7".
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 70
01
💠 NOTE
Functions can take one or more inputs (parameters) and can return
one or more outputs (return values).
It's good practice to put the function declaration in a header file and
the definition in a separate source file. This allows you to reuse the
functions in multiple source files and separate the interface from the
implementation.
do {
// code to be executed
} while (condition);
1. The code block: This is the code that will be executed repeatedly while the
condition is true.
2. The condition: This is a Boolean expression that is evaluated after each iteration
of the loop. If the condition is true, the code inside the loop is executed again,
and if the condition is false, the loop is terminated.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 71
01
The difference between the do-while loop and the while loop is that the do-while loop
guarantees that the code block will be executed at least once because the condition
is evaluated after the code block has been executed.
Here is an example of a do-while loop that prompts the user to enter a number
greater than zero and keeps prompting until the user enters a valid number:
In this example, the code block inside the do-while loop prompts the user to enter a
number and the condition (number <= 0) is evaluated after the input is read. If the
user enters a number less than or equal to zero, the code block is executed again
and the user is prompted to enter a valid number. This continues until the user
enters a number greater than zero, at which point the condition is false and the loop
is terminated. The final printf statement is then executed, displaying a message
thanking the user for entering a valid number.
The do-while loop is particularly useful when you need to execute a block of code at
least once before checking the condition. It's also useful when you want to keep
prompting the user for input until a valid input is received.
In summary, the do-while loop is a control flow statement in C that allows you to
execute a block of code repeatedly while a certain condition is true. The loop's
controlling condition is evaluated after each iteration, and the code block is
guaranteed to be executed at least once.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 72
01
💠 NOTE
Syntax:
do {
// code to be executed
} while (condition);
The difference between the do-while loop and the while loop is that
the do-while loop guarantees that the code block will be executed at
least once because the condition is evaluated after the code block has
been executed.
2. you want to keep prompting the user for input until a valid input is
received.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 73
01
Floating-point imprecision refers to the limitations of precision when working with
floating-point numbers in computer systems. Since computers use a finite number of
bits to represent floating-point numbers, they can only approximate the infinite
precision of real numbers. This can lead to errors when performing calculations with
floating-point numbers, particularly when dealing with very large or very small
numbers.
In addition, the order of operations can also affect the precision of the result. For
example, when performing multiplications and divisions before additions and
subtractions, precision errors can accumulate quickly.
It is important to be aware of the limitations of precision when working with floating-
point numbers and to take appropriate measures to minimize imprecision errors in
your calculations. This can include using the appropriate data types for your
calculations, being mindful of the order of operations, and using libraries such as the
GNU Multiple Precision Arithmetic Library (GMP) for very precise calculations.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 74
01
💠 NOTE
The order of operations can also affect the precision of the result.
Truncation [04:02:41]
Truncation in computer science refers to the process of discarding the fractional part
of a number and keeping only the integer part. Truncation is often used in computer
arithmetic to round a number down to the nearest whole number. This is also known
as "rounding towards zero."
In C, the truncation of a float to an int can be performed using the (int) typecast
operator. For example:
Copy code
float x = 3.14;
int y = (int) x;
In this example, the value of x is 3.14, but the value of y will be 3, discarding the
fractional part.
Truncation can also be performed using the floor() function from the math.h library.
For example:
Copy code
#include <math.h>float x = 3.14;
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 75
01
int y = floor(x);
In this case, the floor() function returns the largest integer that is less than or equal
to the input, in this case 3.
It's important to note that truncation can lead to a loss of precision in the final result,
particularly when working with large numbers or performing multiple arithmetic
operations. In some cases, it may be preferable to use rounding instead of
truncation.
💠 NOTE
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 76
01
Explicit type conversion, also known as a casting, is performed by the programmer
using a typecast operator. For example, the following code performs an explicit type
conversion from a float to an int :
float x = 3.14;
int y = (int) x;
Here, the float value of x is explicitly converted to an int by the (int) typecast
operator.
It's important to note that explicit type conversion can lead to a loss of precision or
range, especially when converting from a larger data type to a smaller one.
Therefore it's important to use explicit conversion judiciously.
In summary, type conversion is the process of converting a variable from one data
type to another. C provides two types of type conversion: implicit and explicit. Implicit
conversion happens automatically, while explicit conversion is performed by the
programmer using a typecast operator. It's important to use explicit conversion
judiciously, as it can lead to a loss of precision or range.
💠 NOTE
Integer overflow
Integer overflow refers to a condition that occurs when a computation produces a
result that is too large to be represented within the range of values that can be
stored by the integer data type. This can happen when a computation exceeds the
maximum value that can be stored in an integer variable, resulting in a wrap-around
to a smaller value.
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 77
01
For example, in C, an int data type can hold a maximum value of 2147483647. If
an operation is performed that causes the value to exceed this maximum, it will
wrap-around to the minimum value of -2147483648.
Copy code
int x = 2147483647;
x = x + 1;
printf("%d", x);
In this example, the value of x is 2147483647 before the operation, but after the
operation x = x+1 the value of x will be -2147483648.
Integer overflow can lead to unexpected results and can be a source of bugs in a
program. For example, if a program is designed to check for a maximum value and
take some action, an overflow can cause the check to fail and result in unintended
behavior.
To avoid integer overflow, it's important to use the appropriate data types for your
calculations, and to check for overflow before performing operations that can cause
it. For example, you can use the long long data type in C which can hold values up
to 9223372036854775807 and check it before performing any operation that can
cause overflow.
💠 NOTE
Introduction to Computer Science for Beginners: A Comprehensive Guide to Computer Science Fundamentals Part 78
01