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

Lec16.pptx problem specifications computer

  • 1.
  • 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 structurescontrol 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 thestatements in the same order in which they are written in the program Entry point Exit point Flowchart Copyright @ IT Series
  • 5.
    Selection  A controlstructure 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 controlstructure 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 ifstatement 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> usingnamespace 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> usingnamespace 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 simplestselection 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 tomake 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 © ITSeries #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 isused 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 ifstatement 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 @ ITSeries 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 oneof 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 usingswitch 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.
  • 21.
    Loop  The controlstructure 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 loopis 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 loopexecutes 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 @ ITSeries cout<<n<<“t”; cout<<n<<“t”;
  • 28.
     It isa 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 candefine 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