Home / Notes / Class 10 / Computer Science / Programming basics

Programming basics: Class 10 Computer Science

Written by Usman Tariq (Computer Science)

Problem solving, algorithms and flowcharts, program structure in C, data types, variables, operators, input and output, decisions with if and switch, loops, and common errors, with short programs.

Key points

A program is a set of instructions written in a programming language to solve a problem. A programmer follows these steps: understand the problem, plan the solution as an algorithm, write the code, test and debug it, and document it. An algorithm is a step by step procedure that solves a problem in a finite number of steps.

A flowchart shows an algorithm with symbols:

  • Oval: start or end.
  • Parallelogram: input or output.
  • Rectangle: process or calculation.
  • Diamond: decision with yes and no branches.
  • Arrows: show the direction of flow.

Pseudocode is an algorithm written in simple English-like statements. For example: start, read two numbers, add them, print the sum, end. It is not tied to any language.

Types of programming error:

  • Syntax error: breaking the rules of the language, such as a missing semicolon. The compiler reports it.
  • Logical error: the program runs but gives a wrong result because the logic is wrong.
  • Runtime error: the program stops while running, for example by dividing by zero.

Structure of a simple C program:

  • #include <stdio.h> adds the library for input and output.
  • int main(void) is where execution starts.
  • Statements are inside curly braces { } and each ends with a semicolon.
  • return 0; ends the program normally.
  • Comments are written between /* and */ or after // and are ignored by the compiler.

Here is a complete example: #include <stdio.h> then int main(void) { int a = 7, b = 3; printf("Sum = %d\n", a + b); return 0; } This prints Sum = 10. The code %d is a format specifier for an integer and \n moves to a new line.

Data types and variables. A variable is a named memory location that stores a value, and it must be declared with a type before use:

  • int: whole numbers, for example 25. Format %d.
  • float: decimal numbers, for example 3.14. Format %f.
  • double: decimals with greater precision. Format %lf in scanf.
  • char: a single character in single quotes, for example 'A'. Format %c.
  • Variable names use letters, digits and underscore, cannot start with a digit, cannot contain spaces, and cannot be a keyword such as int or while. C is case sensitive, so Age and age differ.

A constant is a value that does not change. It can be defined with the const keyword, for example const float PI = 3.14;. A statement such as x = 5; is an assignment: the value on the right is stored in the variable on the left.

Operators:

  • Arithmetic: +, -, *, / and % (remainder, only for integers). Integer division drops the fraction, so 17 / 5 gives 3 and 17 % 5 gives 2.
  • Relational: >, <, >=, <=, == (equal to), != (not equal to). They give true or false.
  • Logical: && (and), || (or), ! (not).
  • Increment and decrement: i++ adds 1 and i-- subtracts 1.

Input is read with scanf and output is written with printf. To read an integer: scanf("%d", &num); The & sign gives the address of the variable and must be used with scanf for numbers and characters. Without the right format specifier, the value will be read or shown wrongly.

Decision making lets a program choose between actions:

  • if (condition) { ... } runs the block only when the condition is true.
  • if ... else runs one block when true and another when false.
  • else if checks further conditions in turn.
  • switch (value) with case labels and break chooses among many fixed values.
  • Example: if (marks >= 40) printf("Pass"); else printf("Fail");

Loops repeat a block of statements:

  • for (i = 1; i <= 5; i++) { ... }: used when the number of repetitions is known. It has initialisation, condition and update.
  • while (condition) { ... }: tests the condition first, then repeats.
  • do { ... } while (condition);: runs the body at least once, then tests.
  • break leaves a loop early and continue skips to the next repetition.
  • Take care that the condition eventually becomes false, or an infinite loop results.

Study tip: Practise writing short programs by hand: sum of numbers, even or odd, largest of three numbers, table of a number, and factorial. Always include the header line, main, declarations, input prompt and return statement. Trace loops in a table of values, because dry runs are common in papers.

Definitions and flashcards

Say the meaning first, then open the card to check.

Algorithm

A finite, step by step method of solving a problem.

Flowchart

A diagram that shows an algorithm using symbols and arrows.

Variable

A named memory location that holds a value.

Syntax error

A mistake that breaks the grammar rules of the language.

Logical error

A mistake that makes the program give wrong results.

Operator

A symbol that performs an operation on values.

Loop

A structure that repeats statements.

Debugging

Finding and correcting errors in a program.

Compiler

A program that translates source code to machine code.

Short questions with model answers

These are the kind of questions asked for 2, 3 and 5 marks. Try each one before opening the answer.

Define an algorithm and state two of its qualities. (2 marks)

An algorithm is a finite sequence of clear steps used to solve a problem. It must be unambiguous, with each step clearly stated, and it must end after a finite number of steps and produce the expected output.

What is the difference between a syntax error and a logical error? (3 marks)

A syntax error breaks the rules of the language, such as a missing semicolon, and the compiler refuses to build the program. A logical error is not detected by the compiler. The program runs but gives wrong output because the instructions are wrong, for example using + instead of *.

Write a C program to read two integers and print their product. (5 marks)

#include <stdio.h> int main(void) { int a, b; scanf("%d %d", &a, &b); printf("Product = %d\n", a * b); return 0; } The program declares two integers, reads them with scanf, multiplies them and displays the result with printf.

State the use of the symbols oval, diamond and parallelogram in a flowchart. (3 marks)

The oval marks the start and end of the flowchart. The diamond shows a decision with two outcomes, yes and no. The parallelogram shows input or output.

Differentiate between while and do while loops. (3 marks)

A while loop tests its condition before running the body, so the body may never run. A do while loop runs the body first and tests the condition afterwards, so the body runs at least once.

Name the four basic data types in C with one example each. (2 marks)

int stores whole numbers such as 45. float stores decimal numbers such as 2.5. char stores a single character such as 'B'. double stores decimal numbers with more precision such as 3.141592.

Numericals with worked steps

What is the value of sum after this code? int sum = 0; for (i = 1; i <= 4; i++) sum = sum + i;
  1. i = 1: sum = 0 + 1 = 1
  2. i = 2: sum = 1 + 2 = 3
  3. i = 3: sum = 3 + 3 = 6
  4. i = 4: sum = 6 + 4 = 10
  5. i = 5 fails the condition, so the loop ends

Answer: sum = 10

Find the values of 17 / 5 and 17 % 5 where both numbers are integers.
  1. 5 goes into 17 three times: 5 × 3 = 15
  2. Integer division gives the whole part: 3
  3. Remainder = 17 - 15 = 2

Answer: 17 / 5 = 3 and 17 % 5 = 2

What is printed by: int x = 8; if (x % 2 == 0) printf("Even"); else printf("Odd");
  1. 8 % 2 = 0 because 8 = 2 × 4 with no remainder
  2. The condition 0 == 0 is true
  3. The if branch runs

Answer: Even

Common mistakes

  • Using = instead of == in a condition. The single sign assigns a value, the double sign compares.
  • Missing semicolons at the end of statements or putting a semicolon right after for or if, which changes the meaning.
  • Leaving out & in scanf for an int, which can crash the program.
  • Expecting 17 / 5 to give 3.4. Dividing two integers gives an integer.
  • Writing a loop whose condition never becomes false, creating an infinite loop.

Practise this chapter

Practice questions and timed test Printable worksheet

Previous: Spreadsheets Next: Web pages and HTML