0% found this document useful (0 votes)
14 views14 pages

Lecture 04

This document outlines the course details for Computer Programming (CSC-113) taught by Mehreen Tariq, focusing on decision control statements in C++. It explains various types of decision-making statements such as if, if/else, switch, and nested if/else statements, along with their syntax and functionality. Additionally, it provides examples of C++ programs that demonstrate the use of if/else and switch statements to determine the corresponding day of the week and check for leap years.

Uploaded by

M T
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)
14 views14 pages

Lecture 04

This document outlines the course details for Computer Programming (CSC-113) taught by Mehreen Tariq, focusing on decision control statements in C++. It explains various types of decision-making statements such as if, if/else, switch, and nested if/else statements, along with their syntax and functionality. Additionally, it provides examples of C++ programs that demonstrate the use of if/else and switch statements to determine the corresponding day of the week and check for leap years.

Uploaded by

M T
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/ 14

Computer

Programming

• Course Code: CSC-113


• Course Instructor: Mehreen Tariq
• Email: [email protected]
Decision Controls
The decision control statements are the decision-making statements that decides
the order of execution of statements based on the conditions. In the decision-
making statements the programmer specify which conditions are to be executed or
tested with the statements to be executed if the condition is true or false.

1. If statement
2. If/else statement
If
3. If/elseif/else statement Condition
4. Switch statement
5. Nested if/else statement
Switch Statement

• The switch statement evaluates an integral expression and chooses one of several
execution paths based on the expression's value.
• In other words, a Switch statement provides a convenient way of selecting among
a (possibly large) number of fixed alternatives.
Syntax Switch Statement
switch (expression)
{

case constant1:
// statements
break;
case constant2:
// statements
break;
. . .
default:
// default statements
}
How switch statements work:

• 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.
The break keyword:
• When C++ reaches a break keyword, it break out of the switch block.
• This will stop the execution of more code and case testing inside the block.
• When match is found, and the job is done, it's time for a break. There is no need
for more testing.
• A break can save a lot of execution time as it ignores the execution of all the rest
of the code in the switch block.
The default keyword:

• The default code specifies some code to run if there is no case match.
• The default keyword must be used as the last statement in the switch, and it does
not need a break.
Write a program in C++ to input a number between 1 to
7 and print the corresponding day of a week (day name)
using if else statment.
• #include <iostream> • else if(n==4)
• using namespace std; • { cout<<"Wednesday";}
• int main() • else if(n==5)
• { int n; • { cout<<"Thursday"; }
• cout<<"Enter a number "; • else if(n==6)
• cin>>n; • { cout<<"Friday”;}
• if(n==1) • else if(n==7)
• { cout<<"Sunday"; } • { cout<<"Saturday"; }
• else if(n==2) • else
• { cout<<"Monday"; } • { cout<<"Invalid number"; }
• else if(n==3) • return 0;}
• { cout<<"Tuesday";}
Write a program in C++ to input a number between 1 to
7 and print the corresponding day of a week (day name)
using Switch statment.
• #include<iostream> • case 3:
• using namespace std; • cout<<"Wednesday";
• int main() • break;
• case 4:
• { int day;
• cout<<"Thursday";
• cout<<"\nEnter the Day's number :";
• break;
• cin>>day; • case 5:
• switch (day) • cout<<"Friday";
• { case 1: • break;
• cout<<"Monday"; • case 6:
• break; • cout<<"Saturday";
• break;
• case 2:
• case 7:
• cout<<"Tuesday";
• cout<<"Sunday";
• break; • break; } return 0; }
Nested if/else Statement
Sometimes, we need to use an if statement inside another if statement. This is
known as nested if statement.

Think of it as multiple layers of if statements. There is a first, outer if statement, and


inside it is another, inner if statement. Its syntax is:
// outer if statement
if (condition1)
{
// statements
// inner if statement
if (condition2)
{
// statements
}
}
Leap Year Not Leap Year

1968 1971

2004 2006

2012 2010

1200 1700

1600 1800

2000 1900
Leap Year Example
#include <iostream> // all other years are not leap years
using namespace std; else {
int main() { cout << year << " is not a leap year.";
int year; }
cout << "Enter a year: ";
cin >> year; return 0;
// leap year if perfectly divisible by 400 }
if (year % 400 == 0) {
cout << year << " is a leap year.“ ; }
// not a century year and divided by 4
else if (year % 100! == 0 && year % 4 == 0) {
cout << year << " is not a leap year.";
}
Reference:
Tony Gaddis, “Starting out with C++”, 6th Edition, Pearson -> Chapter 01

You might also like