Programming and Computation I
C Programming Level One
By Omar Ahmed
Topics :
1. Flow Charts, C Programming Language Syntax, Data Types, Arithmetic Operators, Standard Inputs and Outputs
2. Format Specifiers, Operators, Control Structures - If Statements
3. Control Structures Cont’d – If Else Statements, Nested Conditional Statements, Switch Statement
4. Repetition Structures: While and Do While Statements
5. Repetition Structures: For Statements, break and continue Statements, Type Conversion and Casting
1. Flow Charts, C Programming Language Syntax, Data Types, Arithmetic Operators,
Standard Inputs and Outputs
Flow Charts
Flow charts are visual diagrams representing the steps and decisions in a program. Here are key symbols:
● Oval: Start or End of the process.
● Rectangle: Process or action (e.g., calculations).
● Parallelogram: Input or Output (e.g., taking input or displaying output).
● Diamond: Decision point (e.g., an if condition).
Example:
Flow charts help break down complex logic visually, showing the program's flow step-by-step.
C Programming Language Syntax
Basic syntax in C includes:
● Statements: Each command ends with a semicolon (;), like int x = 5;.
● Curly Braces {}: Define the beginning and end of blocks, like in functions or loops.
● Functions: The main function is int main() { ... }, where code execution starts.
Data Types
Data types define the kind of data a variable can store:
● int: For integers (e.g., int x = 5;).
● float and double: For decimals, with double offering more precision.
● char: For single characters, like char grade = 'A';.
Arithmetic Operators
Arithmetic operators perform math operations:
● + (Addition)
● - (Subtraction)
● * (Multiplication)
● / (Division)
● % (Modulo - remainder of division)
Example:
int a = 10, b = 3;
int result = a + b; // result is 13
int mod = a % b; // mod is 1
Standard Inputs and Outputs
In C, you commonly use:
● printf(): Outputs data to the screen.
● scanf(): Takes input from the user.
Example:
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
2. Format Specifiers, Operators, Control Structures - If Statements
Format Specifiers
These specify the type of data printf and scanf functions should handle:
● %d for integers
● %f for floats
● %c for characters
● %s for strings
Example:
int age = 20;
printf("Your age is %d\n", age); // Displays: Your age is 20
Operators
● Relational Operators: Compare values and return true (1) or false (0).
○ Examples: ==, !=, <, >, <=, >=
● Logical Operators: Used to combine conditions.
○ && (AND): True if both conditions are true.
○ || (OR): True if either condition is true.
○ ! (NOT): Reverses the truth value.
Example:
int x = 5;
int y = 10;
if (x < y && y > 0) {
printf("Both conditions are true.\n");
}
If Statements
if statements allow conditional execution:
int age = 18;
if (age >= 18) {
printf("You are an adult.\n");
}
3. If Else Statements, Nested Conditional Statements, Switch Statement
If Else Statements
The if...else statement provides two paths:
int age = 16;
if (age >= 18) {
printf("You are an adult.\n");
} else {
printf("You are a minor.\n");
}
Nested Conditional Statements
You can nest if statements within each other:
int score = 85;
if (score >= 60) {
if (score >= 90) {
printf("Excellent!\n");
} else {
printf("Good job!\n");
}
} else {
printf("You need to improve.\n");
}
Switch Statement
A switch statement is used when you have multiple values for a variable:
int choice = 2;
switch (choice) {
case 1:
printf("You chose option 1.\n");
break;
case 2:
printf("You chose option 2.\n");
break;
default:
printf("Invalid choice.\n");
}
● break exits the switch once a case is matched. Without it, subsequent cases would execute until a break
or the end of the switch.
4. Repetition Structures: While and Do While Statements
While Loop
Repeats code as long as a condition is true:
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
Do While Loop
Similar to while, but guarantees the code inside will run at least once:
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
5. For Statements, break and continue Statements, Type Conversion and Casting
For Loop
A for loop is typically used when the number of iterations is known:
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
This loop runs from i = 0 to i = 4.
Break and Continue Statements
break: Exits the loop immediately.
c
for (int i = 0; i < 5; i++) {
if (i == 3) break;
printf("%d ", i); // Only prints 0, 1, 2
}
continue: Skips the current iteration and goes to the next one.
c
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
printf("%d ", i); // Prints 0, 1, 3, 4
}
Type Conversion and Casting
● Type Conversion: Automatically happens in operations involving different data types. For example, if an int
and a float are used together, the int is converted to float.
Type Casting: Temporarily changes a variable's data type.
c
int a = 5;
● float b = (float)a; // Casting 'a' to float
https://www.linkedin.com/in/omar-ahmedx/