0% found this document useful (0 votes)
18 views35 pages

Lec 4 - If Statement Switch Case

The document provides an introduction to control structures in C++, specifically focusing on the 'if' statement and 'switch case' statement. It explains the syntax, usage, and examples of these statements to handle conditional logic in programming. Additionally, it covers multi-condition checks using logical operators and the 'else if' construct for more complex decision-making.

Uploaded by

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

Lec 4 - If Statement Switch Case

The document provides an introduction to control structures in C++, specifically focusing on the 'if' statement and 'switch case' statement. It explains the syntax, usage, and examples of these statements to handle conditional logic in programming. Additionally, it covers multi-condition checks using logical operators and the 'else if' construct for more complex decision-making.

Uploaded by

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

Case

If Statement/ Switch

Introduction to Programming C++


If Statement
• The if statement in C++ helps the programmer in
comparing values and assigning certain conditions in the
code.

• In other words, a certain portion of the code will not be

Introduction to Programming C++


executed unless the condition in the “if statement “is true.

• In case the condition is false, the portion of the code that is


related to the “if statement” will not be executed.

2
Syntax
if (condition)
{
Desired code to be executed if the condition is true.
}

Introduction to Programming C++


In case the desired code is just one command, then you can
cancel the { }

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

Introduction to Programming C++


Example 1

• Write a code that will ask the user to enter a number.


1. Check if the number is less than 5, greater than 5 or equal to 5.
2. If the number is less than 5, print “the number is less than 5”.

Introduction to Programming C++


3. If the number is greater than 5, print “the number is greater
than 5”.
4. If the number is equal to 5, print “the number is equal to 5”.

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

Introduction to Programming C++


int num_1;
cout<<“Enter a number\n”; The user enters 4
cin>> num_1; Enter a number
if (num_1 <5) 4
cout<<“The number is less than 5\n”; The number is less than 5
if (num_1>5)
cout<<“The number is greater than 5\n”;
if (num_1==5) The user enters 5
cout<<“The number is equal to 5\n”; Enter a number
7
return 0; 5
The number is equal to 5
}
Else

• “else” can be used in your code for the last possible condition.

• An "else" statement in C++ is used in conjunction with an "if

Introduction to Programming C++


statement" to specify a block of code to be executed if the
condition specified in the "if statement" is false. The syntax of
an "else statement" is:

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”;

Introduction to Programming C++


cin>> num_1;
The user enters 4
if (num_1 <5)
cout<<“The number is less than 5\n”; Enter a number
else {
4
The number is less than 5
if (num_1>5)
cout<<“The number is greater than 5\n”;
else
cout<<“The number is equal to 5\n”;
The user enters 5
} Enter a number
10
return 0; 5
} The number is equal to 5
Example 3
• Write a code that will ask the user to enter two numbers.

• Check if the first number is less than the second number, or


greater than or equal to it.

Introduction to Programming C++


• If the first number is less than the second number, print “the
first number is less than the second number”.

• 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

The user enters 1 ,2


using namespace std; 1
int main () Enter the second number
{
2
int num_1, num_2;
The first number is less than the
cout<<“Enter the first number\n”;
second number

Introduction to Programming C++


cin>> num_1;
cout<<“Enter the second number\n”; Enter the first number

The user enters 3,2


cin>> num_2; 3
if (num_1 <num_2) Enter the second number
cout<<“The first number is less than the second number\n”; 2
else { The first umber is greater than
if (num_1>num_2) the second number
cout<<“The first number is greater than the second number\n”;
else
The user enters 8,8
Enter the first number
cout<<“The two numbers are equal\n”;
8
} 12
Enter the second number
return 0;
}
8
The two numbers are equal
Example 2 and 3
• In both examples (example 1 and example 2), notice that we didn’t
use {} after the if statements. That is because the if statement is
only followed by one command which was just a single cout for
our examples.

Introduction to Programming C++


• If the code was supposed to print a message for an if statement
and to add the numbers for example, in that case we would need
to use {}. Now we have two commands for the if statement not
just one.
• If (num1_<num_2)
{
cout<<“the first number is less than the second number\n”;
sum= num_1+num_2; 13
}
If Statement multi-condition
• In order to have several comparison statements in one if
statement you can use OR ( || in C++), or AND (&& in C++).

• For an if statement that has two conditions separated with &&,


then the if statement is considered to be true ONLY if BOTH

Introduction to Programming C++


conditions are true.

• For an if statement that has two conditions separated with ||,


then the if statement is considered to be true if ANY of the two
conditions is true even if the other condition is false.

• 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;

Introduction to Programming C++


cout<<“Enter the first number\n”;
The user enters 6,4
cin>> num_1;
Enter the first number
cout<<“Enter the second number\n”; 6
cin>> num_2; Enter the second number
4
sum= num_1 + num_2;
Not valid
if (num_1 >num_2 && sum<10)
cout<<sum;
else
The user enters 3, 5
Enter the first number
cout<<“not valid”; 16
3
return 0; Enter the second number
} 5
Not valid
Example 3
• In the previous example, the sum will only be printed if
both conditions in the if statement are true
(num_1>num_2 and the sum is < 10)

• In case any of the two conditions is not true then the if

Introduction to Programming C++


statement is considered to be false and will not be
executed.

• What happens if we change the && in the previous


example to ||?
17
Example 3
Output Screen
#include <iostream> The user enters 4, 5
using namespace std; Enter the first number
int main () 4
{ Enter the second number
5
9
int num_1, num_2, sum;

Introduction to Programming C++


cout<<“Enter the first number\n”;
The user enters 6,4
cin>> num_1;
Enter the first number
cout<<“Enter the second number\n”; 6
cin>> num_2; Enter the second number
4
sum= num_1 + num_2;
10
if (num_1 >num_2 || sum<10)
cout<<sum;
else
The user enters 3, 5
Enter the first number
cout<<“not valid”; 18
3
return 0; Enter the second number
} 5
Not valid
Example 3
• In the previous example, the sum will be printed if any of
the conditions in the if statement is true (num_1>num_2
or the sum is < 10)

• In case any of the two statements is true then the if

Introduction to Programming C++


statement is considered to be true and will be executed
even if the other condition is false.

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:

Introduction to Programming C++


20
The conditions are evaluated in order, and if a true condition is found, its corresponding code
block is executed and the rest of the conditions are skipped. If all conditions are false, the code
inside the final else clause, if present, will be executed.
If the user enters 5 for example, then the if statement in line 1.
is true, then the program skips line 2., 3. and 4. and doesn’t

Example 4 check them.


Output: the number is greater than 3

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;

Introduction to Programming C++


Output: the number is greater than 3
if (x>3)
cout<<“the number is greater than 3”;
else if (x<3)
cout<<“the number is less than 3”;
else
cout<<“the number is equal to 3”

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:

Introduction to Programming C++


22
Switch Case
• Here, expression is any valid expression that returns an
integer, char or an enumerated type.
• The case keyword is followed by a value to match
against the expression, and the code following the case
statement will be executed if the expression matches
that value.

Introduction to Programming C++


• The break statement is used to exit the switch case
statement and continue with the code after the switch
case statement.
• If no case matches the expression, the code in the
default case is executed. The default case is optional,
and if not provided, the switch case statement will
simply exit without executing any code if no case
matches the expression. 23
Practice
Write a code that will ask the user to enter a day number then
print on the screen according the following:
1 : Saturday
2 : Sunday

Introduction to Programming C++


3 : Monday
4 : Tuesday
5 : Wednesday
6 : Thursday
7 : Friday

24
Syntax
switch (variable_that_its_value_is_being_checked)
{
case value :
Desired code to be executed if the case is valid.
}

Introduction to Programming C++


Example: write a program that ask the user to enter a number if it’s 2
the program should print ( right) if not print (not valid)
int x=2;
switch (x)
{
case 2: cout<<“right”;
default: cout<<“not valid\n”;
25
}
default in switch, case is equivalent to else in if statements
Example 1
• Write a code that will ask the user to enter a letter grade in
capital letters.
• The job of the code is to print the “performance” equivalent
to that letter grade.
Let A is equivalent to Excellent.

Introduction to Programming C++


B is equivalent to V.good.
C is equivalent to Good.
D is equivalent to Pass.

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

Introduction to Programming C++


cout<<endl; grade has char type, if the grade
case ‘B’: cout<<“V.Good”;
was of int type then that would be The user enters a
(case 50: for example)
cout<<endl;
Enter the letter grade in capital letters
case ‘C’: cout<<“Good”; a
cout<<endl; Not valid letter grade
case ‘D’: cout<<“Pass”; Thank you
cout<<endl;
default: cout<<“Not valid letter grade”;
cout<<endl;
The user enters Z
}
cout<<“Thank you\n”; Enter the letter grade in capital letters
Z
27
return 0;
} Not valid letter grade
Thank you
Example 2
• If you notice in the previous example we only had an
equivalent performance for capital letters letter grades, when
the user entered (a) that was not valid.
• What if we want the code to accept capital and small letters
letter grades? This is a case similar to the || in “if” statements.

Introduction to Programming C++


So our code will actually accept A OR a, B OR b, etc….
• Write a code that will ask the user to enter a letter grade.
• The job of the code is to print what is the “performance”
equivalent to that letter grade.
 Let A or a is equivalent to Excellent.
 B or b is equivalent to V.good.
 C or c is equivalent to Good. 28
 D or d is equivalent to Pass.
Example 2
#include <iostream>
using namespace std;
int main () Output Screen
{ char grade;
cout<<“Enter the letter grade”;
The user enters B
cin>> grade;
switch (grade) Enter the letter grade
{ B
case ‘a’: V.Good
case ‘A’: cout<<“Excellent”; Thank you
cout<<endl;

Introduction to Programming C++


case ‘b’:
case ‘B’: cout<<“V.Good”; The user enters a
cout<<endl;
case ‘c’:
Enter the letter grade
a
case ‘C’: cout<<“Good”;
Excellent
cout<<endl;
Thank youx
case ‘d’:
case ‘D’: cout<<“Pass”;
cout<<endl;
default: cout<<“Not valid letter grade”; The user enters Z
cout<<endl; Enter the letter grade
} Z
29
cout<<“Thank you\n”; Not valid letter grade
return 0; } Thank you
Modulus operator in C++
• The modulus operator (%) is used to find the
remainder when one number is divided by another.
• Syntax: remainder = dividend % divisor;
• dividend: The number to be divided.
• divisor: The number by which the dividend is divided.

Introduction to Programming C++


• remainder: The result after dividing the dividend by
the divisor.
• The result of dividend % divisor is the remainder
after dividing the dividend by the divisor.
• The result of the modulus operation depends on
30
the sign of the divisor. For negative divisors, the
result will have the same sign as the divisor.
Example 3
Output Screen

#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;

Introduction to Programming C++


// Switch-case based on modulus result
switch (num1 % num2) {
case 0: cout << "The remainder is 0.” << endl; break;
case 1: cout << "The remainder is 1." << endl; break;
case 2: cout << "The remainder is 2." << endl; break;
case 3: cout << "The remainder is 3." << endl; break;
default: cout << "The remainder is larger than 3.”<< endl; break;
}
return 0;
}

31
Break

• “break” statement helps in skipping all the other case


statements that come after a valid case .

Introduction to Programming C++


• In other words, once a break happens, the program will exit the
switch case and will continue executing from the first line that
comes after the switch case.

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

Introduction to Programming C++


line. The code will not check for the
cout<<endl; other cases and will exit the switch
break; case. The user enters a
case ‘B’: cout<<“V.Good”;
cout<<endl; However, if the user enters B, then Enter the letter grade in capital letters
the first case is false, the second a
case ‘C’: cout<<“Good”; case is true, the code will print
Not valid letter grade
cout<<endl; V.Good and the cursor will move to
the next line. But the code will still Thank you
case ‘D’: cout<<“Pass”;
check the other cases following the
cout<<endl;
‘B’ case.
default: cout<<“Not valid letter grade”;
cout<<endl; The user enters Z
Enter the letter grade in capital letters
} Z
33
cout<<“Thank you\n”; Not valid letter grade
return 0; Thank you
}
Assignment - If-Else Statement
in C++
• In this exercise, you will write a program that uses an if-else
statement to determine if an integer input is positive, negative
or zero.

• Requirements

Introduction to Programming C++


• Create a main function that takes an integer input from the user.
• Use an if-else statement to check the value of the input:
• If the input is greater than 0, print "The number is positive."
• If the input is less than 0, print "The number is negative."
• If the input is equal to 0, print "The number is zero."
• The program should end by returning 0.
34
Summary
• Making Decisions in C++
• If Statement: Execute code only if condition is true
• Switch Case Statement: Choose from multiple
options based on expression value

Introduction to Programming C++


35

You might also like