SlideShare une entreprise Scribd logo
1  sur  4
FP 201 – PROGRAMMING FUNDAMENTALS




LAB 5 : PROGRAM CONTROL
Learning Outcome

By the end of this lab, students should be able to :
• Describe the structure of for, while and do..while statements in C+
    + program
• Write program using looping statements

Theory/ Topics

•   Relational Operator (<, <=, >, >=)
•   Logical Operator (&&, ||)
•   Operator Precedence
•   Expression
•   Looping statements – for, while, do..while statements

Procedure :
Step 1: Type the programs given below
Step 2: Save the program as _________________
Step 3: Compile and run the program. Write the output


Activity 5A

#include<iostream>
using namespace std;
void main()
{
        int x;
        for(x=1; x<=5; x++)
        {         if (x==3)
                           continue;
                  cout<<x;
        }
        return 0;
}




                                                                    1
FP 201 – PROGRAMMING FUNDAMENTALS




Activity 5B

#include<iostream>
using namespace std;
void main()
{
        int x;
        for(x=1; x<=5; x++)
        {         if (x==3)
                           break;
                  cout<<x;
        }
        return 0;
}


Activity 5C

//Program that add an integer to itself for n of times.
#include <iostream>
using namespace std;
void main()
    {
  int number1, number2;
    int total = 0;
   cout << "Please enter 2 numbers: ";
   cin >> number1 >> number2;

 for(int i = 0; i < number2; ++i)
   {
   total += number1;
     }
  cout << "Total addition: " << total;
  }




                                                          2
FP 201 – PROGRAMMING FUNDAMENTALS




Activity 5D

The following program illustrate the syntax of do..while
statements.

#include <iostream>
using namespace std;
void main()
     {
     char ch;
     int n1, n2;
     do
        {
        cout << “Enter number 1 : “;
        cin >> n1;
        cout << “Enter number 2 : “;
        cout << “number 1 divide by number 2 : “ << n1/n2
         << “, remainder is : “ << n1%n2 << endl;
        cout << “nDo another ? (y/n) => “;
        cin >> ch;
          } while (ch != ‘n’);
     }


Activity 5E
The following program illustrate the syntax of while statements.

#include <iostream>
using namespace std;
void main()
{
         char ch = ‘y’;
         int n1, n2;

         while (ch != ‘n’)
         {
                  cout << “Enter number 1 : “;
                  cin >> n1;
                  cout << “Enter number 2 : “;



                                                                   3
FP 201 – PROGRAMMING FUNDAMENTALS



                  cout << “number 1 divide by number 2 : “ << n1/n2
                  << “, remainder is : “ << n1%n2 << endl;
                  cout << “nDo another ? (y/n) => “;
                  cin >> ch;
         }
}



Exercise 5.1

Write programs to print the following patterns:

                    1
                    12
                    123
                    1234

Exercise 5.2

Write a program by using while loops to calculate the parking fee based
on parking rate RM2.00 per hour for every customer. When there are
no more customers, your program should be able to sum the total
parking collection.
         Example of output:
         Enter parking hours: 2
         Parking fee: RM 4
         Continue? (y/n) => y
         Enter parking hours: 5
         Parking fee: RM 10

         Continue? (y/n) => n
         Total collection: 14




                                                                      4

Contenu connexe

Tendances

Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13
alish sha
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
rohassanie
 
Lab 10 sem ii_12_13
Lab 10 sem ii_12_13Lab 10 sem ii_12_13
Lab 10 sem ii_12_13
alish sha
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
emailharmeet
 
A1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_solA1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_sol
malasumathi
 
Programing array and function
Programing array and functionPrograming array and function
Programing array and function
rowntu
 

Tendances (20)

Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
 
Lab 10 sem ii_12_13
Lab 10 sem ii_12_13Lab 10 sem ii_12_13
Lab 10 sem ii_12_13
 
Do while loop
Do while loopDo while loop
Do while loop
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
 
เฉลยแบบฝึกหัดบทที่ 1
เฉลยแบบฝึกหัดบทที่ 1เฉลยแบบฝึกหัดบทที่ 1
เฉลยแบบฝึกหัดบทที่ 1
 
Lab 1
Lab 1Lab 1
Lab 1
 
Control flow Graph
Control flow GraphControl flow Graph
Control flow Graph
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
Computer science project work
Computer science project workComputer science project work
Computer science project work
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation
 
A1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_solA1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_sol
 
Compiling CAO: From Cryptographic Specifications to C Implementations
Compiling CAO: From Cryptographic Specifications to C ImplementationsCompiling CAO: From Cryptographic Specifications to C Implementations
Compiling CAO: From Cryptographic Specifications to C Implementations
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
up down Counter Verilog
up down Counter Verilogup down Counter Verilog
up down Counter Verilog
 
Programing array and function
Programing array and functionPrograming array and function
Programing array and function
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 

Similaire à Labsheet 5

Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
rohassanie
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
Syed Umair
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
rohassanie
 
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
Week 2PRG 218   Variables and Input and Output OperationsWrite.docxWeek 2PRG 218   Variables and Input and Output OperationsWrite.docx
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
melbruce90096
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
IIUM
 
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docxWeek 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
co4spmeley
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
Prabhu D
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10
alish sha
 

Similaire à Labsheet 5 (20)

Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
 
Labsheet2
Labsheet2Labsheet2
Labsheet2
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
 
Labsheet 4
Labsheet 4Labsheet 4
Labsheet 4
 
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
Week 2PRG 218   Variables and Input and Output OperationsWrite.docxWeek 2PRG 218   Variables and Input and Output OperationsWrite.docx
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
C-LOOP-Session-2.pptx
C-LOOP-Session-2.pptxC-LOOP-Session-2.pptx
C-LOOP-Session-2.pptx
 
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docxWeek 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
 
Python programming workshop session 4
Python programming workshop session 4Python programming workshop session 4
Python programming workshop session 4
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
4. programing 101
4. programing 1014. programing 101
4. programing 101
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
Labsheet1stud
Labsheet1studLabsheet1stud
Labsheet1stud
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10
 
Operating System Lab Manual
Operating System Lab ManualOperating System Lab Manual
Operating System Lab Manual
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 

Plus de rohassanie

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
rohassanie
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
rohassanie
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
rohassanie
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
rohassanie
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
rohassanie
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
rohassanie
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
rohassanie
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2)
rohassanie
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201
rohassanie
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
rohassanie
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
rohassanie
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
rohassanie
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
rohassanie
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
rohassanie
 

Plus de rohassanie (20)

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Lab ex 1
Lab ex 1Lab ex 1
Lab ex 1
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2)
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 

Labsheet 5

  • 1. FP 201 – PROGRAMMING FUNDAMENTALS LAB 5 : PROGRAM CONTROL Learning Outcome By the end of this lab, students should be able to : • Describe the structure of for, while and do..while statements in C+ + program • Write program using looping statements Theory/ Topics • Relational Operator (<, <=, >, >=) • Logical Operator (&&, ||) • Operator Precedence • Expression • Looping statements – for, while, do..while statements Procedure : Step 1: Type the programs given below Step 2: Save the program as _________________ Step 3: Compile and run the program. Write the output Activity 5A #include<iostream> using namespace std; void main() { int x; for(x=1; x<=5; x++) { if (x==3) continue; cout<<x; } return 0; } 1
  • 2. FP 201 – PROGRAMMING FUNDAMENTALS Activity 5B #include<iostream> using namespace std; void main() { int x; for(x=1; x<=5; x++) { if (x==3) break; cout<<x; } return 0; } Activity 5C //Program that add an integer to itself for n of times. #include <iostream> using namespace std; void main() { int number1, number2; int total = 0; cout << "Please enter 2 numbers: "; cin >> number1 >> number2; for(int i = 0; i < number2; ++i) { total += number1; } cout << "Total addition: " << total; } 2
  • 3. FP 201 – PROGRAMMING FUNDAMENTALS Activity 5D The following program illustrate the syntax of do..while statements. #include <iostream> using namespace std; void main() { char ch; int n1, n2; do { cout << “Enter number 1 : “; cin >> n1; cout << “Enter number 2 : “; cout << “number 1 divide by number 2 : “ << n1/n2 << “, remainder is : “ << n1%n2 << endl; cout << “nDo another ? (y/n) => “; cin >> ch; } while (ch != ‘n’); } Activity 5E The following program illustrate the syntax of while statements. #include <iostream> using namespace std; void main() { char ch = ‘y’; int n1, n2; while (ch != ‘n’) { cout << “Enter number 1 : “; cin >> n1; cout << “Enter number 2 : “; 3
  • 4. FP 201 – PROGRAMMING FUNDAMENTALS cout << “number 1 divide by number 2 : “ << n1/n2 << “, remainder is : “ << n1%n2 << endl; cout << “nDo another ? (y/n) => “; cin >> ch; } } Exercise 5.1 Write programs to print the following patterns: 1 12 123 1234 Exercise 5.2 Write a program by using while loops to calculate the parking fee based on parking rate RM2.00 per hour for every customer. When there are no more customers, your program should be able to sum the total parking collection. Example of output: Enter parking hours: 2 Parking fee: RM 4 Continue? (y/n) => y Enter parking hours: 5 Parking fee: RM 10 Continue? (y/n) => n Total collection: 14 4