0% found this document useful (0 votes)
5 views18 pages

Unit2 1 (Statements)

Chapter 2 of DIT1123 covers program control statements in C++, focusing on conditional execution, looping, and control flow alteration. It explains the use of if/else structures, including the ternary operator, else-if ladders, and switch statements with examples. The chapter concludes with a programming task to calculate student grades based on percentage criteria.

Uploaded by

mayurg0909
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)
5 views18 pages

Unit2 1 (Statements)

Chapter 2 of DIT1123 covers program control statements in C++, focusing on conditional execution, looping, and control flow alteration. It explains the use of if/else structures, including the ternary operator, else-if ladders, and switch statements with examples. The chapter concludes with a programming task to calculate student grades based on percentage criteria.

Uploaded by

mayurg0909
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/ 18

DIT1123- Object Oriented Programming with C++

CHAPTER 2
Program Control Statements

Kukutla Alekhya
Objectives

Conditional Execution: Control statements like if, else if, and else allow you to
selectively execute blocks of code based on certain conditions.

Looping: Control statements like for, while, and do-while allow you to execute a
block of code repeatedly.

Control Flow Alteration: Control statements like break, continue, and goto alter the
normal flow of execution in a program.
if/else Selection Structure
2

if statement to specify a block of C++ code to be executed if a condition


is true

if (condition) {
/ / block of code to be executed if the condition is true
}

©ISBATUNIVERSITY – 2023. 3/25/2020


if/else Selection Structure

Example
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}

©ISBATUNIVERSITY – 2023. 3/25/2020


if/else Selection Structure

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
}

©ISBATUNIVERSITY – 2023. 3/25/2020


if/else Selection Structure

Example
int time = 20;
if (time < 18)
{
cout << "Good day.";
}
else
{
cout << "Good evening.";
}
// Outputs "Good evening."
©ISBATUNIVERSITY – 2023. 3/25/2020
if/else Selection Structure
6

 Ternary conditional operator (?:)


🞑 Three arguments (condition, value if true, value if false)
 Code could be written:
cout << ( grade >= 60 ? “Passed” : “Failed” );

Condition Value if true Value if false

false true
grade >= 60

print “Failed” print “Passed”

©ISBATUNIVERSITY – 2023. 3/25/2020


#include <iostream>
using namespace std;
int main () {
int num = 11;
if (num % 2 == 0)
{
cout<<"It is even number";
}
else
{
cout<<"It is odd number";
}
return 0;
}
Syntax : else-if ladder Statement

if(condition1) if-else-if ladder statement


{ executes one condition from
//code to be executed if condition1 is true multiple statements.
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
else
{
//code to be executed if all the conditions are false
}
#include <iostream>
using namespace std;
int main () {
int num;
cout<<"Enter a number to check grade:";
cin>>num;
if (num <0 || num >100)
{
cout<<"wrong number";
}
else if(num >= 0 && num < 50){
cout<<"Fail";
}
else if (num >= 50 && num < 60)
{
cout<<"D Grade";
}
else if (num >= 60 && num < 70)
{
cout<<"C Grade";
}
else if (num >= 70 && num < 80)
{
cout<<"B Grade";
}
else if (num >= 80 && num < 90)
{
cout<<"A Grade";
}
else if (num >= 90 && num <= 100)
{
cout<<"A+ Grade";
}
}
Enter a number to check grade:66
C Grade
switch

switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C++.

switch(expression){
case value1:
//code to be executed;
break;
case value2:
//code to be executed;
break;
......

default:
//code to be executed if all cases are not matched;
break;
}
#include <iostream>

int main() {
char operation;
double num1, num2, result;

cout << "Enter an operation (+, -, *, /): ";


cin >> operation;

cout << "Enter two numbers: ";


cin >> num1 >> num2;

switch (operation) {
case '+':
result = num1 + num2;
cout << "Sum: " << result << endl;
break;
case '-':
result = num1 - num2;
cout << "Difference: " << result << endl;
break;

case '*':
result = num1 * num2;
cout << "Product: " << result << endl;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
cout << "Quotient: " << result << endl;
} else {
cout << "Error: Division by zero is not allowed." << endl;
}
break;

default:
cout << "Invalid operation." << endl;
break;
}

return 0;
}
To Do

Write a C++ program that prompts the user to enter the marks of a student in five subjects
(out of 100) and calculates the total marks and percentage. The program should then use an
else-if ladder to determine the grade of the student based on the following criteria:
 Percentage >= 90: Grade A+
 Percentage >= 80: Grade A
 Percentage >= 70: Grade B
 Percentage >= 60: Grade C
 Percentage >= 50: Grade D
 Percentage < 50: Grade F
Program Objectives

Conditional Statements (if, if-else, if-else if-else):


1. The program executes different blocks of code based on the evaluation of
the condition(s) specified in the if and else if statements.
2. If the condition is true, the corresponding block of code executes.
3. If none of the conditions are true in an if-else if-else ladder, the block of
code in the else statement executes (if present).
4. The outcome can vary depending on the conditions and logic used in the
statements.
Thank you

18

You might also like