Lecture 03
Lecture 03
Example:
#include<iostream>
using namespace std;
int main(){
//bool isCPPHard = true;
//bool CPPEnjoyable = false;
//cout << isCPPHard;
//cout << CPPEnjoyable;
int x = 10;
int y = 9;
//cout << (x > y);
//cout << (x == 10);
//cout<< (x < 5 && x < 10);
cout<< (y < 5 || y < 10);
return 0;
}
C++ Conditions:
If condition:
Syntax: if (condition) {
}
Example: int x = 20; int y = 18;
if (x>y){
cout<<x<<" is greater than "<<y<<endl;
return 0;
}
If … else condition:
Syntax: if (condition) {
}
else {
}
If … else if … else condition:
Syntax: if (condition 1) {
}
else if (condition 2) {
}
else {
}
Example 01:
int CurrentTime;
cout<<"Please Enter the Current Time (24hr format) :"
cin>>CurrentTime;
if (time < 12) {
cout << "Good morning.";
} else if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
Example 02: Find the larger value from the three integers entered by user from
the keyboard.
#include <iostream>
using namespace std;
int main() {
int a, b,c;
cout << "Please enter three values :";
cin>>a>>b>>c;
return 0;
}
Working with strings:
To work with strings, first include the string library:
#include <string>
Concatenation: using +
Append: firstVariable.append(lastVariable)
String length: variable_Name.size() or variable_Name.length()
Access to string: variable_Name[index]
Change the string characters: variable_Name[index] = 'new_Char'
User input string: getline(cin, variable_Name) to get full strings
Special characters: \', \" , and \\
Example:
#include <iostream>
#include <string>
using namespace std;
int main(){
string firstName = "RAIN MAN"
string lastName = "RAJA"
string firstName = firstName + lastName;
cout << fullName;
// cout<<firstName <<" "<<lastName<<endl;
// string fullName = firstName.append(lastName);
// cout<<fullName;
// cout<<firstName.append(lastName);
// string x = "100";
// string y = "333";
// string z = x+y;
// cout<<z;
// cout<<firstName.size();
// cout<<fullName.length()<<"\n";
// cout<<fullName[4]<<endl;
// fullName[15]='Z';
// cout<<fullName;
// string fullName;
// cout << "Type your full name: ";
// // cin>>fullName;
// getline (cin, fullName);
// cout << "Your name is: " << fullName<<endl;
return 0;
}
Example:
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int a = 100;
cout<<log10(a);
return 0;
}