Week 4 & 5 Syntax Guide
Loop Structures
1. For Loop
Use when: You know the exact number of iterations (e.g., iterating through an array).
// Syntax
for (initialization; condition; update) {
// code
}
// Print even numbers 0-10
for (int i = 0; i <= 10; i += 2) {
printf("%d ", i);
}
// Output: 0 2 4 6 8 10
// Print even numbers 0-10
for (int i = 0; i <= 10; i += 2) {
cout << i << " ";
}
// Output: 0 2 4 6 8 10
2. While Loop
Use when: The number of iterations is unknown and depends on a condition (e.g., waiting for specific user input).
// Syntax
while (condition) {
// code
}
// Input Validation
int age;
printf("Enter age (must be positive): ");
scanf("%d", &age);
while (age <= 0) {
printf("Invalid! Try again: ");
scanf("%d", &age);
}
// Input Validation
int age;
cout << "Enter age (must be positive): ";
cin >> age;
while (age <= 0) {
cout << "Invalid! Try again: ";
cin >> age;
}
3. Do-While Loop
Use when: The code must run at least once (e.g., menus).
// Syntax
do {
// code
} while (condition);
// Simple Menus
int choice;
do {
printf("1. Play\n2. Exit\nChoice: ");
scanf("%d", &choice);
} while (choice != 1 && choice != 2);
// Simple Menus
int choice;
do {
cout << "1. Play\n2. Exit\nChoice: ";
cin >> choice;
} while (choice != 1 && choice != 2);
Arrays
Declaration & Initialization
// 1. Declare (Contents undefined)
int scores[5];
// 2. Initialize (Fixed size)
int primes[5] = {2, 3, 5, 7, 11};
// 3. Auto-size (Compiler counts elements)
int numbers[] = {1, 2, 3}; // Size is 3
// 4. Partial Init (Rest become 0)
int data[10] = {1, 2}; // [1, 2, 0, 0, ...]
Common Operations
Iterating an Array
int numbers[] = {10, 20, 30, 40, 50};
int size = 5;
for (int i = 0; i < size; i++) {
printf("Element %d: %d\n", i, numbers[i]);
}
int numbers[] = {10, 20, 30, 40, 50};
int size = 5;
for (int i = 0; i < size; i++) {
cout << "Element " << i << ": " << numbers[i] << "\n";
}
Summing Array Elements
int sum = 0;
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
Reading Different Types of Arrays
To read values into an array, you typically use a loop and scanfcin.
1. Reading Integers
int numbers[5];
printf("Enter 5 integers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &numbers[i]);
}
int numbers[5];
cout << "Enter 5 integers: ";
for (int i = 0; i < 5; i++) {
cin >> numbers[i];
}
2. Reading Floats
float grades[5];
printf("Enter 5 grades: ");
for (int i = 0; i < 5; i++) {
scanf("%f", &grades[i]);
}
float grades[5];
cout << "Enter 5 grades: ";
for (int i = 0; i < 5; i++) {
cin >> grades[i];
}
3. Reading chars
Using Loop
char values[5];
printf("Enter 5 values: ");
for (int i = 0; i < 5; i++) {
scanf(" %c", &values[i]);
}
char values[5];
cout << "Enter 5 values: ";
for (int i = 0; i < 5; i++) {
cin >> values[i];
}
Using String Format
char values[5];
printf("Enter 5 values: ");
scanf("%s", values);
char values[5];
cout << "Enter 5 values: ";
cin >> values;
Character Arrays (Strings)
Strings in CC++ are character arrays ending with \0.
Declaration
char name[] = "Alice"; // Size 6 ('A','l','i','c','e','\0')
char code[10] = "Bot"; // Safe buffer
Reading Strings
Method 1: scanf (Stops at space)cin (Stops at space)
char firstName[50];
scanf("%s", firstName);
char firstName[50];
cin >> firstName;
Method 2: Character Loop (Reads char by char)
char text[100];
int i = 0;
// Read until newline
while (i < 99) {
scanf(" %c", &text[i]);
if (text[i] == '\n')
break;
i++;
}
text[i] = '\0'; // Don't forget null terminator!
char text[100];
int i = 0;
// Read until newline
while (i < 99) {
cin >> text[i];
if (text[i] == '\n')
break;
i++;
}
text[i] = '\0'; // Don't forget null terminator!
Common Patterns
Sentinel Loop
Getting input until a specific “sentinel” value (like -1) is entered.
int n;
printf("Enter number (-1 to stop): ");
scanf("%d", &n);
while (n != -1) {
printf("You entered %d\n", n);
// Read next value
printf("Enter number: ");
scanf("%d", &n);
}
int n;
cout << "Enter number (-1 to stop): ";
cin >> n;
while (n != -1) {
cout << "You entered " << n << "\n";
// Read next value
cout << "Enter number: ";
cin >> n;
}