Docsity

It includes: C basics & program structure Variables Data types Format specifiers printf(), Study notes of Computer Science

Partial preview of the text

Download It includes: C basics & program structure Variables Data types Format specifiers printf() and more Study notes Computer Science in PDF only on Docsity!

C PROGRAMMING

Beginner Notes — From Basics to break & continue

First-Year CSE • Beginner Friendly • Exam Focused

Variables • Data Types • Input/Output • Operators • Conditions • Loops • break • continue

1. Introduction to C

What is C? C is a general-purpose, procedural programming language. It is fast, efficient, and widely used to learn

programming fundamentals.

Why learn C?

  • It teaches core programming concepts clearly.
  • It builds a strong foundation for algorithms and data structures.
  • It is useful in systems and embedded programming.

Basic structure

#include

int main() { printf("Hello, World!"); return 0; }

#include provides input/output functions. main() is where program execution starts. printf() displays output.

return 0; indicates successful completion.

Remember

C is case-sensitive. Most statements end with ;. age and Age are different names.

3. Data Types

A data type tells C what kind of value a variable stores.

Data type Used for Example

int Whole numbers 25

float Decimal numbers 25.

double More precise decimals 25.

char One character 'A'

Examples

int students = 60; float percentage = 87.5; double pi = 3.14159265; char section = 'A';

A char stores one character and uses single quotes, such as 'A'.

4. Format Specifiers

Format specifiers tell printf() and scanf() what kind of data is being displayed or read.

Type Specifier Example

int %d printf("%d", age);

float %f printf("%f", marks);

double %lf scanf("%lf", x);

char %c printf("%c", grade);

scanf example

int age; scanf("%d", &age;);

For ordinary numeric and character input, & is generally used before the variable in scanf().

6. Operators

Arithmetic operators

  • addition
  • subtraction
  • multiplication / division % remainder

int a = 10, b = 3; printf("%d", a + b); // 13 printf("%d", a % b); // 1

Relational operators

==, !=, >, <, >=, <= compare values.

Logical operators

&& = AND, || = OR,! = NOT.

Assignment operators

= += -= *= /=

Example: x += 5 means x = x + 5.

7. Expressions, Increment & Precedence

Increment and decrement

x++; // increase by 1 x--; // decrease by 1

These are commonly used in loops.

Operator precedence

int result = 2 + 3 * 4; // result = 14

int result2 = (2 + 3) * 4; // result2 = 20

Use parentheses when you want to make the order clear.

Integer division

int x = 5 / 2; // x becomes 2

Both operands are integers, so the fractional part is discarded.

9. else-if and Nested if

else-if ladder

if (marks >= 90) { printf("A"); } else if (marks >= 75) { printf("B"); } else if (marks >= 60) { printf("C"); } else { printf("D"); }

Nested if

An if statement inside another if statement is called a nested if.

if (age >= 18) { if (hasID == 1) { printf("Allowed"); } }

In an else-if ladder, once a true condition is found, the remaining conditions are skipped.

10. for Loop

A loop repeats a block of code. A for loop is useful when repetition is controlled by a counter.

for (initialization; condition; update) { // statements }

Example: 1 to 5

for (int i = 1; i <= 5; i++) { printf("%d ", i); }

Output: 1 2 3 4 5

Execution order

  • Initialization happens once.
  • Condition is checked.
  • Body runs if true.
  • Update happens.
  • Condition is checked again.

12. do-while Loop

A do-while loop executes its body first and checks the condition afterward.

int i = 1; do { printf("%d ", i); i++; } while (i <= 5);

Output: 1 2 3 4 5

Main difference

while: condition → body. do-while: body → condition.

A do-while loop executes at least once, even if the condition is false initially.

13. for vs while vs do-while

Feature for while do-while

Condition Before body Before body After body

Minimum executions 0 0 1

Typical use Counting/known repetition Condition-controlled Body must run once

Same task

// for for (int i = 1; i <= 3; i++) printf("%d ", i);

// while int i = 1; while (i <= 3) { printf("%d ", i); i++; }

// do-while i = 1; do { printf("%d ", i); i++; } while (i <= 3);

15. continue Statement

The continue statement skips the remaining statements of the current iteration and moves to the next iteration.

for (int i = 1; i <= 5; i++) { if (i == 3) { continue; } printf("%d ", i); }

Output: 1 2 4 5

When i is 3, continue skips printf for that iteration. The loop then proceeds to the next iteration.

Key idea

continue = skip this iteration, but keep the loop running.

while-loop warning

With a while loop, make sure the loop-control variable is updated correctly; otherwise continue can cause an infinite

loop.

16. break vs continue

break continue

Stops the loop completely Skips only the current iteration

Execution moves outside the loop Execution moves to the next iteration

Use to terminate early Use to skip a particular case

Memory trick

break = EXIT continue = SKIP

Together

for (int i = 1; i <= 10; i++) { if (i == 3) continue; if (i == 7) break; printf("%d ", i); }

Output: 1 2 4 5 6

18. Practice Questions

Try these before checking the revision page.

1. Input two integers and print their sum.

2. Check whether a number is positive, negative, or zero.

3. Check whether marks are 40 or above.

4. Print 1 to 10 using a for loop.

5. Print even numbers from 2 to 20.

6. Print 10 to 1 using a while loop.

7. Use do-while to print 1 to 5.

8. Use break to stop when the number reaches 6.

9. Use continue to skip 5 while printing 1 to 10.

10. Explain break and continue in two sentences.

Mini challenge

Print 1 to 20 but skip multiples of 3. Then modify the program so it stops when it reaches 17.

19. Quick Revision + Answers

Core syntax

#include int main() { // code return 0; }

Remember these

int x = 10; printf("%d", x); scanf("%d", &x;);

if (condition) { } else { }

for (start; condition; update) { } while (condition) { } do { } while (condition);

break; // stop continue; // skip

Practice answers — what to use

1: two int variables + scanf + +. 2: if/else-if/else. 3: if marks >= 40.

4: for loop. 5: start at 2 and add 2. 6: while with i--.

7: do-while. 8: if (i == 6) break;. 9: if (i == 5) continue.

10: break stops the loop; continue skips only the current iteration.

One-minute exam recap

Variables store values. Data types define what kind of value. printf outputs. scanf inputs. if-else makes decisions.

Loops repeat. break exits. continue skips.

End — C Programming Beginner Notes: Basics to break & continue