0% found this document useful (0 votes)
12 views13 pages

Lecture 2

Uploaded by

abirkabir23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

Lecture 2

Uploaded by

abirkabir23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Introduction to Programming

Contents
▪ Type Casting
▪ Conditions
If
else
else if
▪ Switch
Type Casting
• Converting one datatype into another is known as type casting.

#include<iostream>
#include<iostream>
using namespace std;
using namespace std;
int main()
int main()
{
{
float a= 3.45; float b= 4.57;
float a= 3.45; float b= 4.57;
int z;
float z = a + b;
z= (int)a + (int) b;
cout<< z; //output 8.02
cout<< z; //output 7
return 0;
return 0;
}
}
Conditions
• C++ has the following conditional statements:
1. Use if to specify a block of code to be executed, if a specified
condition is true
2. Use else to specify a block of code to be executed, if the same
condition is false
3. Use else if to specify a new condition to test, if the first
condition is false
Conditions... (if)
• Use the if statement to specify a block of C++ code to be executed if
a condition is true.
• Syntax--
if (condition)
{
// block of code to be executed if the condition is true
}
Conditions... (if)
#include<iostream>
#include<iostream>​
using namespace std;
using namespace std;​
int main()
int main()​
{
{​
int a=15; int b=20;
if(15>10)​
if(a<b)
{​
{
cout<<"15 is greater than 10";​
cout<<"b is greater than a";
}​
}
return 0;​
return 0;
}
}
Conditions...(else)
• Use the else statement to specify a block of code to be executed if
the condition is false.
• Syntax-
if (condition)
{
// block of code to be executed if the condition is true
} else
{
// block of code to be executed if the condition is false
}
Conditions...(else)
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
int main() {
int main() {
int number = 90;
int number = 90;
if (number > 80)
if (number < 80) {
{
cout << "Hey I get A+";
cout << "Hey I get A+";
} else {
} else{
cout << "Hey I get A";
cout << "Hey I get A";
}
}
return 0;
return 0;
}
}
Conditions...(else if)
• Use the else if statement to specify a new condition if the first
condition is false.
• Syntax-
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is
true
} else {
// block of code to be executed if the condition1 is false and condition2 is
false
}
Conditions...(else if)
#include <iostream> #include <iostream>
using namespace std; using namespace std;

int main() { int main() {


int hours = 16; int hours = 20;
if (hours < 12) { if (hours < 12) {
cout << "Good morning."; cout << "Good morning.";
} else if (hours < 18) { } else if (hours < 18) {
cout << "Good Noon."; cout << "Good Noon.";
} else { } else {
cout << "Good evening."; cout << "Good evening.";
} }
return 0; return 0;
} }
Switch
• Use the switch statement to select one of many code blocks to be
executed.
• Syntax-
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Switch...
• The switch expression is evaluated once.
• The value of the expression is compared with the values of each case.
• If there is a match, the associated block of code is executed.
• The break and default keywords are optional.
• When C++ reaches a break keyword, it breaks out of the switch block.
• This will stop the execution of more code and case testing inside the block.
• When a match is found, and the job is done, it's time for a break. There is no
need for more testing.
• The default keyword specifies some code to run if there is no case match.
Switch...

#include <iostream> case 3:


using namespace std; cout << "Green: BBA";
break;
int main() { case 4:
int color = 4; cout << "Blue: CSE";
switch (color) { break;
case 1: default:
cout << "Merun: Law, English"; cout << "None";
break;
case 2: }
cout << "Yellow: EEE,COE"; return 0;
break; }

You might also like