ITE 6102 - Computer Programming 1 - VC - Sept 2 PDF
ITE 6102 - Computer Programming 1 - VC - Sept 2 PDF
Computer Programming 1
Virtual Class – September 2, 2020
if statement
if-else statement
if-else-if statement
switch/case statement
The if Statement
Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
Flow Diagram of if Statement
Example:
#include <iostream>
using namespace std;
int main() {
if (20 > 18) {
cout << "20 is greater than 18";
OUTPUT:
}
return 0; 20 is greater than 18
}
Example:
#include <iostream>
using namespace std;
int main() {
int x = 20;
int y = 18;
if (x > y) {
OUTPUT:
cout << "x is greater than y";
} x is greater than y
return 0;
}
The if else Statement
Use the if 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
}
Flow Diagram of if else Statement
Example:
#include <iostream>
using namespace std;
int main() {
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
OUTPUT:
cout << "Good evening.";
} Good evening.
return 0;
}
The if-else-if Statement
if-else-if statement is used when we need to check multiple conditions. In
this control structure we have only one “if” and one “else”, however we can
have multiple “else if” blocks.
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
}
Example:
#include <iostream>
using namespace std;
int main() {
int time = 22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20) { OUTPUT:
cout << "Good day.";
Good evening.
} else {
cout << "Good evening.";
}
return 0;
}
C++ Short Hand if else
Short Hand If...Else (Ternary Operator)
Syntax:
int main() {
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;
return 0;
}
C++ switch Statements
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 case Flow Diagram
C++ switch Statements
This is how it works:
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
The default keyword specifies some code to run if there is no case match:
Example:
#include <iostream>
using namespace std;
int main() {
int day = 4;
switch (day) {
case 6:
cout << "Today is Saturday";
break;
case 7:
cout << "Today is Sunday";
break;
default:
cout << "Looking forward to the Weekend";
}
return 0; }
OUTPUT:
Nesting of switch statements are allowed, which means you can have
switch statements inside another switch. However, nested switch
statements should be avoided as it makes program more complex and
less readable.
int main(){
char ch='b';
switch(ch) {
case 'd': cout<<"Case1 "; case 'y': cout<<"Case4 ";
break; break;
case 'b': cout<<"Case2 "; default: cout<<"Default ";
break; }
case 'x': cout<<"Case3 "; return 0;
break; }
Exercise No. 1:
int x = 50;
int y = 10;
if
(x > y)
{
cout << "Hello World";
}
Exercise No. 2:
Insert the missing parts to complete the following switch statement.
int day = 2;
switch (
){
1:
cout << "Saturday";
break;
2:
cout << "Sunday";
;
}
Answer:
int day = 2;
switch (day) {
case1:
cout << "Saturday";
break;
case 2:
cout << "Sunday";
break;
}
Thank you for listening.
If you have any questions please send a message thru LMS chat box or
email me at [email protected]