3 Topic 3 Basic Elements Part 3
3 Topic 3 Basic Elements Part 3
% Modulus
• For example, the expression (Remainder)
• 10 % 3 results in 1, -- Decrement by 1
• 12 % 7 results in 5, and
• 20 % 5 results in 0 ++ Increment by 1
Relational Operator
• To compare the values of Operator Operation
== Equal to
!= Not equal to
Relational Operator
(Cont…)
• Examples
• if (thisNum < minimumSoFar)
minimumSoFar = thisNum
• if (job == Teacher) salary = minimumWage
• if (numberOfLegs != 8) thisBug = insect
• if (degreeOfPolynomial < 2) polynomial = linear
• if (surf == up) myLocation = oughtaHere
Relational Operator
(Cont…)
The Truth Value of Expressions
bodyTem
bodyTemp < 100.0 bodyTemp == 100.0 bodyTemp>=100.0
p
Logical Operators
Operator Meaning Example of Use Truth Value
&& AND (exp 1) && (exp 2) True if exp 1 and exp 2 are BOTH true.
True if EITHER (or BOTH) exp 1 or exp
|| OR (exp 1) || (exp 2)
2 are true.
Returns the opposite truth value of exp 1;
! NOT ! (exp 1) if exp 1 is true, ! (exp 1) is false; if exp 1
is false, ! (exp 1) is true.
Logical Operator
(Cont…)
• Examples of expressions which contain relational and
logical operators
• pi * radius * radius
• 0.5 * acceleration * time * time + initialVelocity * time + initialPosition
• 4.0 / 3.0 * pi * radius * radius * radius
• twentyFourHourTime % 12
• 1./2.* base * height
Assignment Statement
• It is used to assign a value to a variable
eg : num1 = 2 + num2;
area = length * width;
• Syntax :-
identifier = expression;
must be a variable can be constant, variable or any complex expression
int main ()
{
char name[20];
cout << "What's your name?";
cin.getline (name, 4);
cout << "Hello " << name << "\n";
return 0;
}
Output
If u enter Ahmad
Output will be:
Hello Ahm
String Library Function
• Use string predefined functions (cstring) and must be
included in the preprocessor directive command.
• eg : #include <cstring>
Output
str1: Sample string
str2: Sample string
str3: copy successful
Examples of String Library Function
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[20];
char str2[20];
strcpy (str1,"To be ");
strcpy (str2,"or not to be");
strcat (str1, str2);
cout << str1;
return 0;
}
Output
To be or not to be
Examples of String Library
Function
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char szKey[] = "apple";
char szInput[80];
if (strcmp (szKey,szInput) == 0)
cout<< "Same fruit!\n";
else
cout << "Different fruit";
return 0;
}
Examples of String Library
Function
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char name[40];
Output
Enter a string : I love banana
It’s length is: 13 characters
Functions (continued)
• Functions
• Called modules
• sqrt(2.25) is 1.5
• Type double
• floor(48.79) is 48.0
• Type double
#include <iostream.h>
#include <iomanip.h>
int main () {
cout << "*" << "Hi there!" << "*" << endl;
cout << "*" << setw(20) << "Hi there!" << "*" << endl;
cout << "*" << setw(3) << "Hi there!" << "*" << endl;
return 0;
}
Output
*Hi there!*
* Hi there!*
*Hi there!*
Examples of Formatting
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float real = 12.3443;
cout << setprecision(2)<< real << "\n";
cout << setprecision(4)<< real;
return 0;
}
Output
12
12.34
Examples of Formatting
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float real = 12.344;
cout << setiosflags(ios::fixed)<< real << "\n";
cout << setiosflags(ios::showpoint)<< real ;
return 0;
}
Output
12.344000
12.344000