SlideShare a Scribd company logo
1 of 29
Problem Specification
• Control Structure
• if Statement
• if-else Statement
• if-else-if Statement
• Nested if Statement
• Switch Statement
• Difference between if-else-if and switch Statement
• While Loop
• Do while Loop
• For Loop
Copyright @ IT Series
Topics
Contents
 Control structures control the flow of execution in a program
 The control structures in C language are used to combine individual instruction into a single logical unit
 The logical unit has one entry point and one exit point
 There are three kinds of execution flow:
• Sequence
• Selection
• Repetition
Copyright © IT Series
Sequence
 Executes the statements in the same order in which they are written in the program
Entry point
Exit point
Flowchart
Copyright @ IT Series
Selection
 A control structure which chooses alternative to execute
 Executes a statement or set of statements based on a condition
 Also called decision-making structure or conditional
structure.
 Different types of selection structures in C language:
 If
 if-else
 if-else-if
 switch
Flowchart
Copyright @ IT Series
Exit point
Entry point
Repetition
 A control structure which executes a statement or set of statements repeatedly for a specific number of times
 Also called iteration structure or loop
 Different types of loops available in C language:
 while loop
 do-while loop
 for loop
Entry point
Exit point
Flowchart
Copyright @ IT Series
 The if statement is the primary selection control structure
 It is used to execute or skip a statement or set of statements by checking a condition
 The condition is given as a relational expression. e. g marks >=40
Syntax / General Form
The syntax of if statement for single statement:
if (condition)
statement;
The syntax for compound statements:
if (condition)
{
statement 1;
statement 2;

statement N;
}
if is a keyword in C
language. It is always
written in lowercase.
Block of statements
inside the braces is
called the body of the if
statement. If there is
only 1 statement in the
body, the { } may be
omitted.
Do not place ; after (condition)
Copyright @ IT Series
If Programs
#include <iostream>
using namespace std;
int main() {
int marks;
// Taking input for marks
cout << "Enter your marks: ";
cin >> marks;
// Checking if marks are 40 or more
if (marks >= 40) {
cout << "Congratulations! You have passed." << endl;
}
return 0;
}
Output:
Enter your marks: 65
Congratulations! You have passed.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int num1, num2;
// Input for the first number
cout << "Enter the first number: ";
cin >> num1;
// Input for the second number
cout << "Enter the second number: ";
cin >> num2;
// Checking if the second number is the square of the first number
if (num2 == (num1 * num1)) {
cout << num2 << " is the square of " << num1 << "." << endl;
}
return 0;
}
Output:
Enter the first number: 5
Enter the second number: 25
25 is the square of 5.
 The simplest selection structure but it is very limited in its use
 Statement or set of statements is executed if the condition is true
 But if the condition is false then nothing happens (no alternate action is performed)
 A user may want to:
Execute one statement or set of statements if the condition is true
Execute other statement or set of statements if the condition is false
 In this situation, simple if statement cannot be used effectively
 Solution  ‘if-else’ structure can be used to handle this kind of situation effectively
Example
A program should display Pass! if the student gets 40 or more marks
It should display Fail! if the student gets less than 40 marks
Simple if statement cannot be used to handle this situation
Copyright @ IT Series
 Used to make two-way decisions
 It executes one block of statement(s) when the condition is true and the other when it is false
 In any situation, one block is executed and the other is skipped
 Both blocks of statement can never be executed
 Both blocks of statements can never be skipped
Syntax / General Form
The syntax of if-else statement for single and compound statements:
Condition
Statement
Copyright © IT Series
cout<<“Number are equal”;
cout<<“Number are different”;
If-else Programs
Copyright © IT Series
Copyright @ IT
#include <iostream>
using namespace std;
int main() {
int number;
// Input for the number
cout << "Enter a number: ";
cin >> number;
// Checking if the number is even or odd using if-else structure
if (number % 2 == 0) {
cout << number << " is even." << endl;
} else {
cout << number << " is odd." << endl;
}
return 0;
}
Output:
Enter a number: 7
7 is odd.
Copyright © IT Series
#include <iostream>
using namespace std;
int main() {
int year;
// Input for the year
cout << "Enter a year: ";
cin >> year;
// Checking if the year is a leap year using if-else structure
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
cout << year << " is a leap year." << endl;
} else {
cout << year << " is not a leap year." << endl;
}
return 0;
}
Output:
Enter a year: 2024
2024 is a leap year.
 It is used when there are many options and only one block of statements should be executed on the basis of
a condition
Working of if-else-if
 The condition 1 is evaluated and the first block of statements is executed if the condition 1 is true
 The condition 2 is evaluated if the condition 1 is false. The second block of statements is executed if the
condition 2 is true
 The block of statements after the last else is executed if all conditions are false
Copyright © IT Series
if-else-if Programs
Copyright © IT Series
Copyright @ IT Series
#include <iostream>
using namespace std;
int main() {
int number;
// Input for the number
cout << "Enter a number: ";
cin >> number;
// Checking if the number is positive, negative, or zero using if-else-if structure
if (number > 0) {
cout << "The number is positive." << endl;
} else if (number < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}
return 0;
}
Output:
Enter a number: -8
The number is negative.
 An if statement within an if statement is called nested if statement
Syntax / General Form
Working of Nested if
 the condition of outer if is evaluated first. If it is true, the control enters the inner if block
 If the condition is false, the inner if is skipped and control directly moves to the else part of outer if
 If outer if is true, the control enters the inner if statement
 The inner if evaluated according to simple if statement
Copyright © IT Series
Copyright @ IT Series
Write a program that input three number. determine whether numbers are equal or not
using nested if structure.
#include <iostream>
using namespace std;
int main() {
int num1, num2, num3;
// Input for three numbers
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
// Checking if numbers are equal using nested if structure
if (num1 == num2) {
if (num2 == num3) {
cout << "All three numbers are equal." << endl;
} else {
cout << "First two numbers are equal, but the third number is different." << endl;
}
} else {
cout << "The numbers are not all equal." << endl;
}
return 0;
}
Output:
Enter three numbers: 5 5 5
All three numbers are equal.
 Select one of several alternatives when selection is based on the value of a single variable or an expression
 In C, the value of this expression may be of type int or char
 The switch statement is a better way of writing a program when a series of if-else-if occurs
Syntax / General Form
Copyright @ IT Series
Rules of using switch case in C/C++ program
• The case label must integer or character
• Each case label must be unique
• A switch statement can have only one default label
• The default label can be used anywhere in switch statement
• The case label must end with colon. The default label is optional
Copyright @ IT Series
#include <iostream>
using namespace std;
int main() {
int day;
// Input for the day of the week (1-7)
cout << "Enter a number corresponding to a day of the week (1-7): ";
cin >> day;
// Using switch statement to display the day of the week
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid input! Please enter a number between 1 and 7." <<
endl;
break;
}
return 0;
Output:
Enter a number corresponding to a day of the week (1-7): 3
Wednesday
Copyright @ IT Series
Loop
 The control structure that executes a statement or set of statements repeatedly is called loop.
 Loops are also known as iteration or repetition structure
 Loops are basically used for two purposes:
 They are used to execute a statement or number of statements for a specified number of
times
 Example: A user may display his name on screen for 10 times
 The loops are also used to get a sequence of values
 Example: A user may display a set of natural numbers from 1 to 10
 There are three types of loops available in C:
 while loop
 do-while loop
 for loop
 while loop is the simplest loop of C
 It executes one or more statements while the given condition remains true
 It is useful when the number of iterations is not known in advance
Syntax
 Condition is evaluated:
 if it is true, the statement(s) are executed, and then condition is evaluated again
 if it is false, the loop is exited
An iteration is
an execution of
the loop body.
while is a
keyword. It is
always written in
lowercase.
Loop body
Don’t put ; after (condition)
while is a pretest
loop (the condition
is evaluated before
the loop executes)
Copyright @ IT Series
Flowchart
while Loop Example
Copyright @ IT Series
int n;
n = 1;
while(n<=5)
{
printf(“Pakistann");
n++;
}
 Produces output:
Counter Variable
 A variable that is incremented or decremented each time a loop iterates
 It can be used to control the execution of the loop (as a loop control variable)
 It must be initialized before entering loop
 It may be incremented/decremented either inside the loop or in the loop test
n++; is the same as n= n + 1;
Don’t put ; after (condition)
Loop body
while is a pretest
loop (the condition
is evaluated before
the loop executes)
cout<<“Pakistann”;
Infinite loop
Copyright @ IT Series
 The loop must contain code to allow the condition to eventually become false so the loop can be exited
 Otherwise, you have an infinite loop
Example
int n; n = 1;
while(n<10)
cout<<n<<"t";
Infinite loop because n is always < 10
A loop that does not
stop is known as an
infinite loop
 This loop executes one or more statements while the given condition is true
 In this loop, the condition comes after the body of loop
 The loop body always executes at least once
Syntax:
 Execution continues as long as the condition is true; the loop is exited when the condition becomes false
 ; after (condition) is also required
 The loop executes at least once even if the condition is false in the beginning
An error occurs if the semicolon
is not used at the end of the
condition.
Loop body
do-while is a post
test loop (condition
is evaluated after
the loop executes)
Copyright @ IT Series
do-while Loop Example
Copyright @ IT Series
int n;
n = 1;
do
{
cout<<“Welcome to Cn”;
n++;
}
while(n<=5);
Loop body
do-while is a post
test loop (condition
is evaluated after
the loop executes)
Condition
Copyright @ IT Series
cout<<n<<“t”;
cout<<n<<“t”;
 It is a pretest loop that executes one or more statements for a specified number of times
 This loop is also called counter-controlled loop
 It is useful with counters or if precise number of iterations is known
Syntax / General Form Example
for (initialization; condition; increment/decrement)
{
statement 1;
statement 2;


statement N;
}
Flowchart
Copyright @ IT Series
cout<<“Welcom to C++n”;
 You can define variables in initialization code
for (int n=1; n <= 5; n++)
 Can omit initialization if already done
int n = 1;
for (; n <= 5; n++)
 Can omit update if done in loop body
for (n = 1; n<= 5;)
n++;
 The condition in for loop is mandatory. It cannot be omitted
int n=1;
for (;n<= 5;)
n++;
Copyright @ IT Series

More Related Content

Similar to Lec16.pptx problem specifications computer

Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2Warui Maina
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.pptTekle12
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysDevaKumari Vijay
 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
 
C_BASICS FOR C PROGRAMMER WITH SRIVATHS P
C_BASICS FOR C PROGRAMMER WITH  SRIVATHS PC_BASICS FOR C PROGRAMMER WITH  SRIVATHS P
C_BASICS FOR C PROGRAMMER WITH SRIVATHS Pamankr1234am
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic Manzoor ALam
 

Similar to Lec16.pptx problem specifications computer (20)

Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.ppt
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
Control Structures
Control StructuresControl Structures
Control Structures
 
C_BASICS FOR C PROGRAMMER WITH SRIVATHS P
C_BASICS FOR C PROGRAMMER WITH  SRIVATHS PC_BASICS FOR C PROGRAMMER WITH  SRIVATHS P
C_BASICS FOR C PROGRAMMER WITH SRIVATHS P
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
C operators
C operatorsC operators
C operators
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 

Recently uploaded

CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Recently uploaded (20)

CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

Lec16.pptx problem specifications computer

  • 2. • Control Structure • if Statement • if-else Statement • if-else-if Statement • Nested if Statement • Switch Statement • Difference between if-else-if and switch Statement • While Loop • Do while Loop • For Loop Copyright @ IT Series Topics Contents
  • 3.  Control structures control the flow of execution in a program  The control structures in C language are used to combine individual instruction into a single logical unit  The logical unit has one entry point and one exit point  There are three kinds of execution flow: • Sequence • Selection • Repetition Copyright © IT Series
  • 4. Sequence  Executes the statements in the same order in which they are written in the program Entry point Exit point Flowchart Copyright @ IT Series
  • 5. Selection  A control structure which chooses alternative to execute  Executes a statement or set of statements based on a condition  Also called decision-making structure or conditional structure.  Different types of selection structures in C language:  If  if-else  if-else-if  switch Flowchart Copyright @ IT Series Exit point Entry point
  • 6. Repetition  A control structure which executes a statement or set of statements repeatedly for a specific number of times  Also called iteration structure or loop  Different types of loops available in C language:  while loop  do-while loop  for loop Entry point Exit point Flowchart Copyright @ IT Series
  • 7.  The if statement is the primary selection control structure  It is used to execute or skip a statement or set of statements by checking a condition  The condition is given as a relational expression. e. g marks >=40 Syntax / General Form The syntax of if statement for single statement: if (condition) statement; The syntax for compound statements: if (condition) { statement 1; statement 2;  statement N; } if is a keyword in C language. It is always written in lowercase. Block of statements inside the braces is called the body of the if statement. If there is only 1 statement in the body, the { } may be omitted. Do not place ; after (condition) Copyright @ IT Series
  • 8. If Programs #include <iostream> using namespace std; int main() { int marks; // Taking input for marks cout << "Enter your marks: "; cin >> marks; // Checking if marks are 40 or more if (marks >= 40) { cout << "Congratulations! You have passed." << endl; } return 0; } Output: Enter your marks: 65 Congratulations! You have passed.
  • 9. #include <iostream> #include <cmath> using namespace std; int main() { int num1, num2; // Input for the first number cout << "Enter the first number: "; cin >> num1; // Input for the second number cout << "Enter the second number: "; cin >> num2; // Checking if the second number is the square of the first number if (num2 == (num1 * num1)) { cout << num2 << " is the square of " << num1 << "." << endl; } return 0; } Output: Enter the first number: 5 Enter the second number: 25 25 is the square of 5.
  • 10.  The simplest selection structure but it is very limited in its use  Statement or set of statements is executed if the condition is true  But if the condition is false then nothing happens (no alternate action is performed)  A user may want to: Execute one statement or set of statements if the condition is true Execute other statement or set of statements if the condition is false  In this situation, simple if statement cannot be used effectively  Solution  ‘if-else’ structure can be used to handle this kind of situation effectively Example A program should display Pass! if the student gets 40 or more marks It should display Fail! if the student gets less than 40 marks Simple if statement cannot be used to handle this situation Copyright @ IT Series
  • 11.  Used to make two-way decisions  It executes one block of statement(s) when the condition is true and the other when it is false  In any situation, one block is executed and the other is skipped  Both blocks of statement can never be executed  Both blocks of statements can never be skipped Syntax / General Form The syntax of if-else statement for single and compound statements: Condition Statement Copyright © IT Series cout<<“Number are equal”; cout<<“Number are different”;
  • 12. If-else Programs Copyright © IT Series Copyright @ IT #include <iostream> using namespace std; int main() { int number; // Input for the number cout << "Enter a number: "; cin >> number; // Checking if the number is even or odd using if-else structure if (number % 2 == 0) { cout << number << " is even." << endl; } else { cout << number << " is odd." << endl; } return 0; } Output: Enter a number: 7 7 is odd.
  • 13. Copyright © IT Series #include <iostream> using namespace std; int main() { int year; // Input for the year cout << "Enter a year: "; cin >> year; // Checking if the year is a leap year using if-else structure if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { cout << year << " is a leap year." << endl; } else { cout << year << " is not a leap year." << endl; } return 0; } Output: Enter a year: 2024 2024 is a leap year.
  • 14.  It is used when there are many options and only one block of statements should be executed on the basis of a condition Working of if-else-if  The condition 1 is evaluated and the first block of statements is executed if the condition 1 is true  The condition 2 is evaluated if the condition 1 is false. The second block of statements is executed if the condition 2 is true  The block of statements after the last else is executed if all conditions are false Copyright © IT Series
  • 15. if-else-if Programs Copyright © IT Series Copyright @ IT Series #include <iostream> using namespace std; int main() { int number; // Input for the number cout << "Enter a number: "; cin >> number; // Checking if the number is positive, negative, or zero using if-else-if structure if (number > 0) { cout << "The number is positive." << endl; } else if (number < 0) { cout << "The number is negative." << endl; } else { cout << "The number is zero." << endl; } return 0; } Output: Enter a number: -8 The number is negative.
  • 16.  An if statement within an if statement is called nested if statement Syntax / General Form Working of Nested if  the condition of outer if is evaluated first. If it is true, the control enters the inner if block  If the condition is false, the inner if is skipped and control directly moves to the else part of outer if  If outer if is true, the control enters the inner if statement  The inner if evaluated according to simple if statement Copyright © IT Series
  • 17. Copyright @ IT Series Write a program that input three number. determine whether numbers are equal or not using nested if structure. #include <iostream> using namespace std; int main() { int num1, num2, num3; // Input for three numbers cout << "Enter three numbers: "; cin >> num1 >> num2 >> num3; // Checking if numbers are equal using nested if structure if (num1 == num2) { if (num2 == num3) { cout << "All three numbers are equal." << endl; } else { cout << "First two numbers are equal, but the third number is different." << endl; } } else { cout << "The numbers are not all equal." << endl; } return 0; } Output: Enter three numbers: 5 5 5 All three numbers are equal.
  • 18.  Select one of several alternatives when selection is based on the value of a single variable or an expression  In C, the value of this expression may be of type int or char  The switch statement is a better way of writing a program when a series of if-else-if occurs Syntax / General Form Copyright @ IT Series
  • 19. Rules of using switch case in C/C++ program • The case label must integer or character • Each case label must be unique • A switch statement can have only one default label • The default label can be used anywhere in switch statement • The case label must end with colon. The default label is optional Copyright @ IT Series #include <iostream> using namespace std; int main() { int day; // Input for the day of the week (1-7) cout << "Enter a number corresponding to a day of the week (1-7): "; cin >> day; // Using switch statement to display the day of the week switch (day) { case 1: cout << "Monday" << endl; break; case 2: cout << "Tuesday" << endl; break; case 3: cout << "Wednesday" << endl; break; case 4: cout << "Thursday" << endl; break; case 5: cout << "Friday" << endl; break; case 6: cout << "Saturday" << endl; break; case 7: cout << "Sunday" << endl; break; default: cout << "Invalid input! Please enter a number between 1 and 7." << endl; break; } return 0; Output: Enter a number corresponding to a day of the week (1-7): 3 Wednesday
  • 20. Copyright @ IT Series
  • 21. Loop  The control structure that executes a statement or set of statements repeatedly is called loop.  Loops are also known as iteration or repetition structure  Loops are basically used for two purposes:  They are used to execute a statement or number of statements for a specified number of times  Example: A user may display his name on screen for 10 times  The loops are also used to get a sequence of values  Example: A user may display a set of natural numbers from 1 to 10  There are three types of loops available in C:  while loop  do-while loop  for loop
  • 22.  while loop is the simplest loop of C  It executes one or more statements while the given condition remains true  It is useful when the number of iterations is not known in advance Syntax  Condition is evaluated:  if it is true, the statement(s) are executed, and then condition is evaluated again  if it is false, the loop is exited An iteration is an execution of the loop body. while is a keyword. It is always written in lowercase. Loop body Don’t put ; after (condition) while is a pretest loop (the condition is evaluated before the loop executes) Copyright @ IT Series Flowchart
  • 23. while Loop Example Copyright @ IT Series int n; n = 1; while(n<=5) { printf(“Pakistann"); n++; }  Produces output: Counter Variable  A variable that is incremented or decremented each time a loop iterates  It can be used to control the execution of the loop (as a loop control variable)  It must be initialized before entering loop  It may be incremented/decremented either inside the loop or in the loop test n++; is the same as n= n + 1; Don’t put ; after (condition) Loop body while is a pretest loop (the condition is evaluated before the loop executes) cout<<“Pakistann”;
  • 24. Infinite loop Copyright @ IT Series  The loop must contain code to allow the condition to eventually become false so the loop can be exited  Otherwise, you have an infinite loop Example int n; n = 1; while(n<10) cout<<n<<"t"; Infinite loop because n is always < 10 A loop that does not stop is known as an infinite loop
  • 25.  This loop executes one or more statements while the given condition is true  In this loop, the condition comes after the body of loop  The loop body always executes at least once Syntax:  Execution continues as long as the condition is true; the loop is exited when the condition becomes false  ; after (condition) is also required  The loop executes at least once even if the condition is false in the beginning An error occurs if the semicolon is not used at the end of the condition. Loop body do-while is a post test loop (condition is evaluated after the loop executes) Copyright @ IT Series
  • 26. do-while Loop Example Copyright @ IT Series int n; n = 1; do { cout<<“Welcome to Cn”; n++; } while(n<=5); Loop body do-while is a post test loop (condition is evaluated after the loop executes) Condition
  • 27. Copyright @ IT Series cout<<n<<“t”; cout<<n<<“t”;
  • 28.  It is a pretest loop that executes one or more statements for a specified number of times  This loop is also called counter-controlled loop  It is useful with counters or if precise number of iterations is known Syntax / General Form Example for (initialization; condition; increment/decrement) { statement 1; statement 2;   statement N; } Flowchart Copyright @ IT Series cout<<“Welcom to C++n”;
  • 29.  You can define variables in initialization code for (int n=1; n <= 5; n++)  Can omit initialization if already done int n = 1; for (; n <= 5; n++)  Can omit update if done in loop body for (n = 1; n<= 5;) n++;  The condition in for loop is mandatory. It cannot be omitted int n=1; for (;n<= 5;) n++; Copyright @ IT Series