C Programming Essentials Lec 4
C Programming Essentials Lec 4
Declaration Initialization
This step specifies the data type and name of the This step assigns an initial value to the variable.
variable. For example, `int myAge;` declares an For example, `int myAge = 25;` declares and
integer variable named `myAge`. initializes the variable `myAge` with the value 25.
Escape Sequences
Escape sequences are special character combinations used
to represent characters that are difficult or impossible to
type directly. They are often used to control formatting, add
special characters, or represent control codes.
1 \n (Newline) 2 \t (Tab)
int main() {
int x = 5, y = 10;
Relational operators compare values and return a Boolean
cout << "Is x equal to y? " << (x == y) << endl; // 0 means false
result (true or false). They are used in conditional cout << "Is x less than y? " << (x < y) << endl; // 1 means true
statements to control program flow based on comparisons. return 0;
}
Operator Description Example
== (Equal to) Checks if two values are equal. if (age == 18) ...
!= (Not equal to) Checks if two values are not equal. if (number != 0) ...
> (Greater than) Checks if the first value is greater if (score > 90) ...
than the second.
< (Less than) if (temperature < 0) ...
Checks if the first value is less than the second.
>= (Greater than or equal to) if (height >= 1.75) ...
Checks if the first value is greater
<= (Less than or equal to) if (time <= 10) ...
than or equal to the second.
int main() {
int age;
cout << "Enter your age: ";
cin >> age; // Gathering input
1 cin 2 static_cast
The `cin` object is used to read input from This operator explicitly converts a value from
the standard input stream (typically the one data type to another.
keyboard).
Library Functions
Library functions provide pre-written code to perform common tasks, such
as mathematical calculations, string manipulations, and input/output
operations. Using library functions saves time and effort by leveraging
existing code.
iostream
Provides input and output operations, such as `cin` for input and `cou
cmath
string
for Loop
1
Executes a block of code a specified number of times.
while Loop
2
Executes a block of code as long as a specific condition is true.
do-while Loop
3
Executes a block of code at least once, then
continues as long as a specific condition is true.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) { // Loop runs 5 times
cout << "Iteration " << i << endl;
}
return 0;
}
for Loop
The for loop is a versatile loop structure for iterating a set
number of times. It's used to execute a block of code
repeatedly while a counter variable changes.
1 Initialization 2 Condition
3 Increment/Decrement
Example:
#include <iostream>
using namespace std;
int main() {
int count = 1;
while (count <= 5) {
cout << "Count: " << count << endl;
count++; // Increment count
}
return 0;
}
What is a do-while Loop in C++?A do-while loop is
similar to a while loop but with a key difference: the
code inside the loop is executed at least once, even if
the condition is false from the beginning. After
executing the loop's body, the condition is checked. If
the condition is true, the loop continues to execute; if
it's false, the loop stops.
do {
// Code to be executed do {
} while (condition); cout << "Enter a positive number (greater than 0): ";
cin >> number;
} while (number <= 0); // The loop continues as long as the number is less
than or equal to 0.