MidTerm Exam-C programming 1
MidTerm Exam-C programming 1
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
Flow Charts
Flow charts are visual diagrams representing the steps and decisions in a program. Here are key symbols:
Example:
Flow charts help break down complex logic visually, showing the program's flow step-by-step.
● 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
● + (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
Example:
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
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:
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 Else Statements
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.
While Loop
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
continue: Skips the current iteration and goes to the next one.
c
● 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.
int a = 5;
https://www.linkedin.com/in/omar-ahmedx/