0% found this document useful (0 votes)
13 views

C Programming Essentials Lec 4

Uploaded by

kayanizain523
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

C Programming Essentials Lec 4

Uploaded by

kayanizain523
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

C++ Programming Essen

This presentation will guide you through the fundamentals of


C++ programming, covering essential concepts like variable
declaration, operators, input gathering, and looping
structures.
Variable Definition and Declaration
Variables are containers that store data. To use a variable, you must first declare it with a specific data type, like
int for integers or double for decimal numbers.

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)

Moves the cursor to Inserts a horizontal


the next line. tab character.

3 \' (Single quote) 4 \" (Double quote)

Represents a single Represents a double


quote character. quote character.
#include <iostream>
using namespace std;
Arithmetic Operators int main() {
int a = 10, b = 3;
Arithmetic operators perform mathematical operations on numerical cout << "Addition: " << a + b << endl;
values. They are essential for calculations and manipulating data. cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl;
Operator Description Example cout << "Modulus: " << a % b << endl;
return 0;
+ Addition int sum = 10 + 5; // }
sum = 15

- Subtraction int difference = 10 -


5; // difference = 5

* Multiplication int product = 10 *


5; // product = 50

/ Division double quotient =


10 / 5; // quotient =
2
Modulo (remainder) int remainder = 10
% 3; // remainder =
1
#include <iostream>
Relational Operators using namespace std;

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.

Checks if the first value is less than


or equal to the second.
#include <iostream>
using namespace std;

int main() {
int age;
cout << "Enter your age: ";
cin >> age; // Gathering input

float height = (float)age; // Type casting int to


float
cout << "Your age as a float: " << height << endl;
return 0;

Input Gathering and Type Casting


Input gathering allows users to interact with your program by providing data. Type casting converts data
from one type to another. These techniques are crucial for creating dynamic and user-friendly programs.

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

Offers mathematical functions, such as `sqrt` for square root


and `pow` for exponentiation.

string

Provides string manipulation functions, such as `length` for


string length and `find` for substring search.
Loops
Loops allow you to repeat a block of code multiple times, making
your programs more efficient and concise. They are essential for
performing tasks that require repetition or iteration.

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

The counter variable is The loop continues as


initialized with a long as the condition
starting value. is true.

3 Increment/Decrement

The counter variable is incremented or decremented after eac


Nested Loops
Nested loops involve one loop inside another. This allows you
to perform repetitive tasks on multiple levels, for example,
iterating through rows and columns of a matrix.

for Loop while Loop

A for loop can be nested A while loop can also be


#include <iostream> inside another for loop to nested inside another while
using namespace std;
create multiple iterations. loop, allowing for complex
int main() {
for (int i = 1; i <= 3; i++) { // Outer loop For example, you can use iterative operations. For
for (int j = 1; j <= 2; j++) { // Inner loop
cout << "i = " << i << ", j = " << j << endl; nested for loops to print a example, you can use
} multiplication table or nested while loops to
}
return 0; process data in a two- simulate game logic or
}
dimensional array. process data with specific
While Loop

The while loop repeats a block of code as long as a condition is true.

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.

cout << "You entered: " << number << endl;


return 0;

You might also like