The document discusses input and output statements in C++. It explains that the iostream library includes cout and cin for standard output and input. cout uses the insertion operator << to output data to the screen, while cin uses the extraction operator >> to input data from the keyboard. The document provides examples of using cout and cin to output text, numbers, and calculate values from user input.
Iterative control structures, looping, types of loops, loop workingNeeru Mittal
Introduction to looping, for loop. while loop, do loop jump statements, entry controlled vs exit controlled loop, algorithm and flowchart of loops, factorial of a number
Iterative control structures, looping, types of loops, loop workingNeeru Mittal
Introduction to looping, for loop. while loop, do loop jump statements, entry controlled vs exit controlled loop, algorithm and flowchart of loops, factorial of a number
Complete Information till 2D arrays. In this slides you can also find information about loops and control decision....
Best slides for beginners who wants to learn about C programming language..
This is continuation of the slide Advanced C part 1. In part 1 you learnt about fundamentals of C - How to build an algorithm, operators. In this module - Advanced C part 2 you will be learning about functions, pointers and standard Input Output functions. This slide will help you to move a further ahead in Advanced C and gain deeper knowledge on it.
Complete Information till 2D arrays. In this slides you can also find information about loops and control decision....
Best slides for beginners who wants to learn about C programming language..
This is continuation of the slide Advanced C part 1. In part 1 you learnt about fundamentals of C - How to build an algorithm, operators. In this module - Advanced C part 2 you will be learning about functions, pointers and standard Input Output functions. This slide will help you to move a further ahead in Advanced C and gain deeper knowledge on it.
The symbols which are used to perform logical and mathematical operations in a C program are called C operators. These C operators join individual constants and variables to form expressions. Operators, functions, constants and variables are combined together to form expressions. Consider the expression A + B * 5.
Operators and expressions in c languagetanmaymodi4
what is operator in c language
uses of operator in c language
syatax of operator in c language
program of operator in c language
what is expressions in c language
use of expressions in c language
syantax of expressions in c language
18 css101j pps unit 2
Relational and logical Operators - Condition Operators, Operator Precedence - Expressions with pre / post increment operator - Expression with conditional and assignment operators - If statement in expression - L value and R value in expression -
Control Statements – if and else - else if and nested if, switch case - Iterations, Conditional and Unconditional branching
For loop - while loop - do while, goto, break, continue
Array Basic and Types - Array Initialization and Declaration - Initialization: one Dimensional Array - Accessing, Indexing one Dimensional Array Operations - One Dimensional Array operations - Array Programs – 1D
learn how to program with python
from scratch to be an expert
use the link in the first slide to get the full course
here is the link if you want : https://oke.io/JdxdUl
the link of the documentation : https://oke.io/J6DeY3
link to download python : https://oke.io/BENgRLR2
2. Learning outcome
By the end of the course, students should be able to:
Define the following operators:
a. Arithmetic operator
b. Assignment operator
c. Increment and decrement operator
d. Relational operator
e. Logical operator
Explain operators’ precedence
Explain type conversion
Write expression using operator
Use expression in program
3. Operators
Operator Action
- subtraction (also unary minus)
+ addition
* multiplication
/ division
% modulus division
-- decrement
++ increment
&& AND
|| OR
! NOT
5. Arithmetic Operators
Perform four fundamental operations which are addition,
subtraction, multiplication and division.
Operator Description Example Result
* Multiplication 2*8 16
/ Division 8/2 4
+ Addition 2+8 10
- Subtraction 8-2 6
() Parentheses. For grouping (8+2) 10
% Modulus. Divides two number and 4%3 1
returns just the remainder portion
6. Arithmetic Operators
arithmetic expressions are evaluated with some rule called
operator precedence.
multiplications (*) and divisions (/) are evaluated first
additions (+) and subtractions (-) are performed last.
7. Activity 1
Write the resultant value of the following expressions.
Expression Result
14-4
14+4
14* 4
14/4
14%4
8. ACTIVITY 2
Calculate the total marks for the given five subjects.
Declare appropriate variables, assign values as given
below:
English = 85
Maths = 100
History = 75
Geography = 70
Art = 85
calculate the total marks
calculate the average
9. #include<iostream>
using namespace std;
int main()
{
int english = 85, maths = 100, history = 75, geography = 70, art = 85;
int sum=0;
sum=english+maths+history+geography+art;
cout<<"Sum is: "<<sum<<endl;;
return 0;
}
10. #include<iostream>
using namespace std;
int main()
{
int english = 85, maths = 100, history = 75, geography = 70, art = 85;
int sum=0, average=0;
sum=english+maths+history+geography+art;
average=sum/5;
cout<<"Sum is: "<<sum<<endl;
cout<<"Average is:"<<average<<endl;
return 0;
}
11. Assignment Operator
variables are given a value through the use of assignment
operator (=)
general form:
variable = expression;
eg:
int x, y;
x = 2;
y = 5*x; // y = 10
x = x + 4; // x = 6
y = y/2; // y = 5
12. Assignment Operator
The value of an assignment can be used in another
assignment.
This is a chained assignment.
Eg:
m = (n = 66) + 9; // n = 66 and m = 75
x = y = 22; // x = 22 and y = 22
13. Compound Assignment Operators
(+=), (-=), (*=), (/=), (%=)
allow us to use assignment and an arithmetic operator
together.
general form is:
variable operator= expression;
14. Compound Assignment Operators
Operator Description Example Equivalent to
+= add and assign x += 3 x=x+3
-= subtract and assign x -= 5 x=x-5
*= multiply and assign x *= 4 x=x*4
/= divide and assign x /= 2 x=x/2
%= find reminder and assign x %= 9 x=x%9
15. In Class Exercise
Given the following declaration and initial assignment:
int i = 4, j = 21, k;
float x = 94.55, y;
char a = ‘z’, b = ‘t’, c =‘ ‘;
Determine the value for the following assignments:
k=i*j
y = x + i
y = k = j
a = b =c
i += i
16. Increment & Decrement Operator
increment operator (++) and decrement operator (--)
increase or reduce by one the value stored in a variable.
the following are equivalent in functionality.
x++;
x += 1;
x = x + 1;
characteristic of this operator:
can be used both as a prefix and as a postfix.
eg: a++ or ++a
the results is different
17. Prefix
When a prefix expression (++x or --x) is used as part
of an expression, the value returned is the value
calculated after the prefix operator is applied
int x = 0;
int y = 0;
y = ++x;
// result: y=1, x=1 x is incremented by 1 and the result is
assigned to y
18. Postfix
When a postfix expression (x++ or x--) is used as part
of an expression, the value returned is the value
calculated before the postfix operator is applied
int x = 0;
int y = 0;
y = x++;
// result: y=0, x=1 original value of x is stored, x is
incremented, original value of x is assigned to y
19. Increment & Decrement Operator
eg:
a = 5; // a = 5
b = a++; // b = a = 5, then a = 5+1 = 6
c = ++a; // a = 6+1 = 7, then c = a = 7
20. Relational Operator
Control statements use relation operators in order to compare two objects,
which results in a value that is either true (1) or false (0).
There are six relational operators as follows:
Operator Description Example Value
< less than x<y 6<9 1
<= less than or equal to x <= y 12 <= 13 1
> greater than x>y 7 > 10 0
>= greater than or equal to x >= y 9 >= 5 1
== equal to x == y 7 ==5 0
!= not equal to x != y 6 != 5 1
21. Logical Operator
Operator Description Example
logical AND, conjunction.
&& Both sides must be true for the result to x > 2 && y == 3
be true
logical OR, disjunction.
|| The result is true if either side or both x > 2 || x <= 9
sides are true.
! logical NOT, negation !(x>0)
23. Logical Operator
using these operators, we can form a Boolean Expression that
can be either true or false.
Boolean expressions evaluate to integer values.
The value 0 means 'false' and all non-zero value means 'true'.
There are also two literal constants used with bool
variables: true and false.
24. In Class Exercise
What is the difference between x = 3 and x == 3?
Given a = 2, b = 5, c = 7 determine the value for the
following expressions either TRUE or FALSE: :
1. a < 2 && b > 5
2. c >= a
3. a < b || b > c
4. c != 10
5. a == 25
25. Operator Precedence
Determines the order in which mathematical computations
and operations are performed in an expression.
27. Operator Precedence
Given a=b = 5, c = 2; solve the following expression
a+b*2/c–5
=5+5*2/2–5
= 5 + 10 / 2 – 5
=5+5–5
= 10 – 5
=5
28. In Class Exercise
Given x = 10, y = 7, z = 2; solve the following expression
x + 2y – z
x / z – x * x + y
x * y % z
5 ( x + y + z ) – x / z
++x*y – y*z
29. Type conversion/Type Casting
Is the conversion of a value of one data type into another
data type.
Typecasting consist of two types:
Implicit conversion
Explicit conversion
30. Implicit conversion
A value of data type with lower range is assigned to a variable
of data type with higher range
Lower range to higher range
31. Example
#include<iostream>
using namespace std;
OUTPUT
void main() Result: 4.2555
{
double x; // Size is 8 bytes
int y=2; //Size is 4 bytes
float z=2.2555f;
x=y+z; //Implicit conversion
cout<<"Result: "<<x;
}
32. Explicit conversion
A value of data type with higher range is assigned to a
variable of data type with lower range
Higher range to lower range
Explicit conversion leads to data loss.
33. Example
#include<iostream>
using namespace std;
OUTPUT
void main() The total value is:300
{
int total_value;
double book1 = 300.46;
total_value=(int)book1; // Explicit conversion
cout<<"The total value is:"<<total_value;
}
34. Exercise
Write a program to declare a variable d of type double.
Assign the value 102.5789 to d. Declare a float variable fl.
Assign the value of d to fl by performing an explicit
conversion and print the value on the screen.
35. Answer
#include<iostream>
using namespace std;
void main() OUTPUT
{ Value of the float variable fl = 102.579
double d=102.5789;
float fl;
fl=(float)d; // Explicit conversion
cout<<"Value of the float variable fl = "<<fl;
}
36. Summary
In this presentation, you learnt the following:
Operator is a symbol that instructs compiler to perform an
operation or action.
Typecasting is the conversion of data type of the value of a
variable into another.
There are two types of casting: implicit conversion and
explicit conversion.
37. FP 201 – Programming
Fundamentals with C++
Input Output Statements
38. Learning outcome
By the end of the course, students should be able to:
Identify the syntax use for input and output
Write program that use the input and output statements
39. Input/Output Statements
The standard C++ library includes the header file
iostream, where the standard input and output stream
objects are declared.
cout to output data to the screen and
cin to input data from the keyboard.
40. Standard Output
standard output:
need to use cout, followed by the insertion operator (<<)
eg:
cout << "Hello World!"; // Outputs "Hello World!"
on the screen
cout << 1299; // Outputs the number 1299 on the
screen
cout << x; // Outputs the content of x on the
screen
insertion operator (<<) may be used more than once in a
single statement:
cout << "Hi, " << "this is " << "a simple C++ statement";
41. Standard Output
Note that, cout does not add a line break.
For example:
cout << "Computer Programming ";
cout << "is important for engineers.";
will be output as:
Computer Programming is important for
engineers.
42. Standard Output
A line break can be added by inserting a 'n' character or a
using a endl manipulator.
cout << "Computer Programming n";
cout << "is important for engineers.";
cout << "Computer Programming " << endl;
cout << "is important for engineers.";
These two produce the following (same) output: Computer
Programming
is important for engineers.
43. Standard Input
Handling the standard input is done by applying the
overloaded operator of extraction (>>) on the cin stream
Value can be input from the keyboard and assigned to a
variable x as follows:
cin >> x;
and two variables can assigned as any of the follows:
cin >> x;
cin >> y;
cin >> x >> y;
44. Code Example
// Calculates the sum of two integers
#include <iostream>
using namespace std;
int main()
{
int a, b, total;
cout << "Enter two integers to be added: ";
cin >> a >> b;
total = a + b;
cout << "The sum is " << total << endl;
return 0;
}
45. Code Example
// Calculates the area of a triangle
// given an input of the triangle base and height.
#include <iostream>
int main()
{
float base, height, area;
std::cout << "Enter the base and height of the
triangle: ";
std::cin >> base >> height;
area = 0.5*base*height;
std::cout << "The area of the triangle is "
<< area << std::endl;
}
46. Q&A
Develop a program that can calculate the total and average of
four numbers that is input by user.
47. #include <iostream>
using namespace std;
int main()
{
int a, b, c, d, total, average;
cout << "Enter four integers to be added: ";
cin >> a >> b>>c>>d;
total = a + b + c + d;
average = total/4;
cout << "The sum is " << total << endl;
cout<< "The average is "<< average << endl;
return 0;
}
48. Summary
In this presentation, you learnt the following:
cout to output data to the screen
cin to input data from the keyboard.
cout use insertion operator (<<)
cin use overloaded operator of extraction (>>)
Editor's Notes
k=84 y=98.55 y=21 a= i=8
a < 2 && b > 5 : FALSE c >= a : TRUE a < b || b > c : FALSE c != 10: TRUE a == 25: FALSE
x + 2y – z=22 x / z – x * x + y=-88 x * y % z=0 5 ( x + y + z ) – x / z:90 ++x*y – y*z=63