Lec 4 - If Statement Switch Case
Lec 4 - If Statement Switch Case
If Statement/ Switch
2
Syntax
if (condition)
{
Desired code to be executed if the condition is true.
}
3
Introduction to Programming C++
Example
In this example, the condition x > 5 is true, so the code inside the if 4
statement std::cout << "x is greater than 5" << std::endl; will be executed
and the program will output: x is greater than 5.
Operators used for comparison
6
Example 1
Output Screen
#include <iostream> The user enters 7
using namespace std; Enter a number
int main () 7
{ The number is greater than 5
• “else” can be used in your code for the last possible condition.
8
Introduction to Programming C++
Example
9
In this example, the condition x > 5 is false, so the code inside the else block std::cout
<< "x is not greater than 5" << std::endl; will be executed and the program will output:
x is not greater than 5.
Example 2
Output Screen
#include <iostream> The user enters 7
using namespace std;
Enter a number
int main ()
7
{
The number is greater than 5
int num_1;
cout<<“Enter a number\n”;
• If the first number is greater than the second number, print “the
first number is greater than the second number”.
• If the first number is equal to the second number, print “the two 11
numbers are equal”.
Example 3
Output Screen
#include <iostream> Enter the first number
• Example, write a code that will only print out the sum if the first
14
number is greater than the second number and the sum is less
than 10. For any other case, print not valid.
Introduction to Programming C++
If Statement multi-condition
In this example, both conditions x > 2 and y > 2 are true, so the code inside the if statement 15
std::cout << "x is greater than 2 and y is greater than 2" << std::endl; will be executed and
the program will output: x is greater than 2 and y is greater than 2.
Example 3
Output Screen
#include <iostream> The user enters 5, 4
using namespace std; Enter the first number
int main () 5
{ Enter the second number
4
9
int num_1, num_2, sum;
19
else if
• else if is a conditional statement in C++ used to
specify multiple conditions.
• It's used after an if statement and acts as an
"else" clause to the previous condition.
• The syntax of else if is:
If the user enters 2 for example, the program will check line 1.
and it is false, then the program will check line 2. and it is true,
# include <iostream> the program will then skip 3. and 4. and will not check them.
using namespace std; Output: the number is less than 3
int main () If the user enters 11 for example, the program will check line 1.
{ and it is true, then the program will skip line 2., 3. and 4. (even
though line 3. would have been true if it was checked by the
int x; program, it is still not executed).
cin>>x;
21
Switch Case
• The switch case statement in C++ is a control structure
that allows you to choose from several options based on
the value of an expression.
• The syntax for a switch case statement is as follows:
24
Syntax
switch (variable_that_its_value_is_being_checked)
{
case value :
Desired code to be executed if the case is valid.
}
26
Example 1
#include <iostream>
using namespace std; Output Screen
int main ()
{ The user enters B
char grade;
cout<<“Enter the letter grade in capital letters\n”; Enter the letter grade in capital letters
cin>> grade; B
switch (grade) V.Good
Thank you
{ We have the letters in single
case ‘A’: cout<<“Excellent”; quotations (‘A’) because the variable
#include <iostream>
The user enters 10 3
using namespace std;
int main() {
Enter two numbers
10 3
int num1, num2;
The remainder is 1.
cout << "Enter two numbers: ";
cin >> num1 >> num2;
31
Break
32
Example 4
#include <iostream>
using namespace std;
Output Screen
int main ()
{
char grade;
The user enters B
cout<<“Enter the letter grade in capital letters\n”; Enter the letter grade in capital letters
cin>> grade; B
switch (grade) If the user enters A, then the first V.Good
{ case is true. Excellent will be printed Thank you
case ‘A’: cout<<“Excellent”; and the cursor will move to the next
• Requirements