SlideShare a Scribd company logo
1 of 5
UNIVERSITI TUN HUSSEIN ONN MALAYSIA
             FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING
             BTI 10202: COMPUTER PROGRAMMING


 LAB 6 : Control Statements – Part 2 (Loops)
 NAME    : _____________________________
 MATRICS NO.: _______________ DATE : _____                                                  MARK


Objectives: Students be able to develop a repetition statements or program by using the iteration
              structure (loops).
                 i. while loop statement
                 ii. do…while loop statement
                 iii. for loop statement
Theory:
a.   while                                                         Example:
     i.       Syntax of while loop (one statement):                /*Total up from marks 1 to marks 5.*/
              while (condition- Code to execute while the          while(count<=5) /* count from 1 until 5*/
              condition is true)                                   {
              {                                                         printf(“Key in the marks (%d) =”,
                    Statement;                                     count);
              }                                                         scanf(“%d”, &marks);
                                                                        total=total+marks; /*add the marks
     ii.      Syntax of while loop (> one statement):              one by one*/
              while(condition- repeated until becomes false)            count+=1; /*add the count*/
              {                                                    }
                    Statement 1;
                    Statement 2;
                    :
                    :
                    Statement n;
              }


b.   do…while loops are useful for things that want to loop at     Example:
     least once                                                    /* Total up from marks 1 until marks 5*/
     Notice that the condition is tested at the end of the block        do
     instead of the beginning, so the block will be executed at         {
     least once                                                              printf(“Key in the
     Syntax of do…while loop:                                           marks(%d)=”,count);
           do                                                                scanf(“%d”, &marks);
           {                                                                 total=total+marks; ‘* total up the
                Statement 1;                                            marks one by one*/
                :                                                            count+=1; /*add count*/




                                                                                                   1
:                                                       }
               Statements n;                                           while(count<=5); /*repeat until count
          }                                                            reach 5*/
          while(condition);


c.   for condition tells the program that while the conditional   Example:
     expression is true the loop should continue to repeat        /*Total up from marks 1 until marks 5*/
     itself.                                                           for(count=1;count<=5;count+=1)
     Syntax of for loop:                                          /*can write as ++count*/
           for(initial value; condition; update counter)               {
           {                                                                printf(“key in the
                Statement(s);                                     marks(%d)=”,count);
           }                                                                scanf(“%d”, &marks);
                                                                            total=total+marks; /* total up the
                                                                       marks one by one*/
                                                                       }
Flowchart for loop structures




d.   Nested loops

     Example:
     #include<stdio.h>
     #include<conio.h>

     main()
     {
         int row,column,num;
         do




                                                                                                   2
{
               printf(“nEnter the number of row of your triangle (1-30 only):n”);
               scanf(“%d”,&num);
          }
          while(num<=0||num>30); /*cannot more than 30 or less than 0*/

          printf(“Your trianglen”);
          for(row=1;row<=num;++row)
          {
               printf(“n”);
               for(column=1;column<=(row*2-1);column++)
               {
                    printf(“*”);
                    }
                    }
                    getch();
          }



Exercise:

1.   What is the output of following ‘for’ loop source code?

          #include<stdio.h>
     #include<conio.h>
                                                         Output

          main()
          {
       int i;
          printf("This is a for loopn");
          for(i = -5; i <= 0; i = i +1)
          printf("%d ", i);
          getch();
          }




                                                                                      3
2. Rewrite the ‘for’ loop program in Exercise 1 using ‘while’ loop statement to give the same
   output.

     While statement




3. Type the example source code for the nested loops (at theory: d section) and observe the
output. Explain the program in detail.

         Output:




                                                                                              4
5

More Related Content

What's hot

C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)SURBHI SAROHA
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSaraswathiRamalingam
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloadingmohamed sikander
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Hazwan Arif
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04hassaanciit
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]Abhishek Sinha
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 

What's hot (20)

C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
Functions
FunctionsFunctions
Functions
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Function recap
Function recapFunction recap
Function recap
 
Functions
FunctionsFunctions
Functions
 
Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Intro to c chapter cover 1 4
Intro to c chapter cover 1 4
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
Labsheet2
Labsheet2Labsheet2
Labsheet2
 
C語言基本資料型別與變數
C語言基本資料型別與變數C語言基本資料型別與變數
C語言基本資料型別與變數
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 

Viewers also liked

Purpose elaborate
Purpose elaboratePurpose elaborate
Purpose elaboratealish sha
 
Advert planning
Advert planningAdvert planning
Advert planningReganROFL
 
The Krass Family 2010
The Krass Family 2010The Krass Family 2010
The Krass Family 2010Mike Krass
 
Tarot: Card Interpretation
Tarot: Card InterpretationTarot: Card Interpretation
Tarot: Card InterpretationJohnna Russell
 
Businesssystempresentation101409 100107132037 Phpapp01
Businesssystempresentation101409 100107132037 Phpapp01Businesssystempresentation101409 100107132037 Phpapp01
Businesssystempresentation101409 100107132037 Phpapp01Rodan + Fields Dermatologists
 
Cover Presentation Robert Benson
Cover Presentation Robert BensonCover Presentation Robert Benson
Cover Presentation Robert BensonRob Benson
 
โครงงานครั้งที่ 2 (Guava Cake)
โครงงานครั้งที่ 2 (Guava Cake)โครงงานครั้งที่ 2 (Guava Cake)
โครงงานครั้งที่ 2 (Guava Cake)NattAA
 
Necesidades de comunicación e información en el paciente refworks uniform
Necesidades de comunicación e información en el paciente refworks uniformNecesidades de comunicación e información en el paciente refworks uniform
Necesidades de comunicación e información en el paciente refworks uniformMercedes Bueno
 
第5回淑女会 〜facebook講座〜
第5回淑女会 〜facebook講座〜第5回淑女会 〜facebook講座〜
第5回淑女会 〜facebook講座〜新一 佐藤
 
K2 atomic structure
K2 atomic structureK2 atomic structure
K2 atomic structureRathiga Sri
 
Infusionsoft: How to prepare a file to import
Infusionsoft: How to prepare a file to importInfusionsoft: How to prepare a file to import
Infusionsoft: How to prepare a file to importMarvin Libron
 
Avastusõpe Tartu Kivilinna Gümnaasiumis
Avastusõpe Tartu Kivilinna GümnaasiumisAvastusõpe Tartu Kivilinna Gümnaasiumis
Avastusõpe Tartu Kivilinna GümnaasiumisPiret Jõul
 
Building Google Authorship Authority
Building Google Authorship AuthorityBuilding Google Authorship Authority
Building Google Authorship AuthorityMarvin Libron
 
Presentation1
Presentation1Presentation1
Presentation1scorbi2
 
Transcricion e traducion new
Transcricion e traducion newTranscricion e traducion new
Transcricion e traducion newjuanapardo
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8alish sha
 
Natalia_Tarabanova_cmf2013
Natalia_Tarabanova_cmf2013Natalia_Tarabanova_cmf2013
Natalia_Tarabanova_cmf2013Evgeny Vasyuk
 

Viewers also liked (20)

Purpose elaborate
Purpose elaboratePurpose elaborate
Purpose elaborate
 
Advert planning
Advert planningAdvert planning
Advert planning
 
The Krass Family 2010
The Krass Family 2010The Krass Family 2010
The Krass Family 2010
 
Tarot: Card Interpretation
Tarot: Card InterpretationTarot: Card Interpretation
Tarot: Card Interpretation
 
Businesssystempresentation101409 100107132037 Phpapp01
Businesssystempresentation101409 100107132037 Phpapp01Businesssystempresentation101409 100107132037 Phpapp01
Businesssystempresentation101409 100107132037 Phpapp01
 
Cover Presentation Robert Benson
Cover Presentation Robert BensonCover Presentation Robert Benson
Cover Presentation Robert Benson
 
โครงงานครั้งที่ 2 (Guava Cake)
โครงงานครั้งที่ 2 (Guava Cake)โครงงานครั้งที่ 2 (Guava Cake)
โครงงานครั้งที่ 2 (Guava Cake)
 
Necesidades de comunicación e información en el paciente refworks uniform
Necesidades de comunicación e información en el paciente refworks uniformNecesidades de comunicación e información en el paciente refworks uniform
Necesidades de comunicación e información en el paciente refworks uniform
 
Vocabulary cards
Vocabulary cardsVocabulary cards
Vocabulary cards
 
第5回淑女会 〜facebook講座〜
第5回淑女会 〜facebook講座〜第5回淑女会 〜facebook講座〜
第5回淑女会 〜facebook講座〜
 
K2 atomic structure
K2 atomic structureK2 atomic structure
K2 atomic structure
 
Infusionsoft: How to prepare a file to import
Infusionsoft: How to prepare a file to importInfusionsoft: How to prepare a file to import
Infusionsoft: How to prepare a file to import
 
Avastusõpe Tartu Kivilinna Gümnaasiumis
Avastusõpe Tartu Kivilinna GümnaasiumisAvastusõpe Tartu Kivilinna Gümnaasiumis
Avastusõpe Tartu Kivilinna Gümnaasiumis
 
Building Google Authorship Authority
Building Google Authorship AuthorityBuilding Google Authorship Authority
Building Google Authorship Authority
 
الرسم المظوري
الرسم المظوريالرسم المظوري
الرسم المظوري
 
Presentation1
Presentation1Presentation1
Presentation1
 
Transcricion e traducion new
Transcricion e traducion newTranscricion e traducion new
Transcricion e traducion new
 
Damcalifornia
DamcaliforniaDamcalifornia
Damcalifornia
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
Natalia_Tarabanova_cmf2013
Natalia_Tarabanova_cmf2013Natalia_Tarabanova_cmf2013
Natalia_Tarabanova_cmf2013
 

Similar to Lab 6 (20)

Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
3. control statement
3. control statement3. control statement
3. control statement
 
Session 3
Session 3Session 3
Session 3
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Loop control structure
Loop control structureLoop control structure
Loop control structure
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
Looping
LoopingLooping
Looping
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
if,loop,switch
if,loop,switchif,loop,switch
if,loop,switch
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Iteration
IterationIteration
Iteration
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Lec 10
Lec 10Lec 10
Lec 10
 

More from alish sha

T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9alish sha
 
July 2014 theory exam (theory)
July 2014 theory exam (theory)July 2014 theory exam (theory)
July 2014 theory exam (theory)alish sha
 
Accounting basic equation
Accounting basic equation Accounting basic equation
Accounting basic equation alish sha
 
It 302 computerized accounting (week 2) - sharifah
It 302   computerized accounting (week 2) - sharifahIt 302   computerized accounting (week 2) - sharifah
It 302 computerized accounting (week 2) - sharifahalish sha
 
It 302 computerized accounting (week 1) - sharifah
It 302   computerized accounting (week 1) - sharifahIt 302   computerized accounting (week 1) - sharifah
It 302 computerized accounting (week 1) - sharifahalish sha
 
What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)alish sha
 
Lab 5 2012/2012
Lab 5 2012/2012Lab 5 2012/2012
Lab 5 2012/2012alish sha
 
Test 1 alish schema 1
Test 1 alish schema 1Test 1 alish schema 1
Test 1 alish schema 1alish sha
 
Lab 6 sem ii_11_12
Lab 6 sem ii_11_12Lab 6 sem ii_11_12
Lab 6 sem ii_11_12alish sha
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&aalish sha
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&aalish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Attn list test
Attn list testAttn list test
Attn list testalish sha
 
Carry markdam31303
Carry markdam31303Carry markdam31303
Carry markdam31303alish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Final project
Final projectFinal project
Final projectalish sha
 
Dti2143 dam31303 lab sheet 8
Dti2143 dam31303 lab sheet 8Dti2143 dam31303 lab sheet 8
Dti2143 dam31303 lab sheet 8alish sha
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7alish sha
 

More from alish sha (20)

T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9T22016 – how to answer with ubs 9
T22016 – how to answer with ubs 9
 
July 2014 theory exam (theory)
July 2014 theory exam (theory)July 2014 theory exam (theory)
July 2014 theory exam (theory)
 
Accounting basic equation
Accounting basic equation Accounting basic equation
Accounting basic equation
 
It 302 computerized accounting (week 2) - sharifah
It 302   computerized accounting (week 2) - sharifahIt 302   computerized accounting (week 2) - sharifah
It 302 computerized accounting (week 2) - sharifah
 
It 302 computerized accounting (week 1) - sharifah
It 302   computerized accounting (week 1) - sharifahIt 302   computerized accounting (week 1) - sharifah
It 302 computerized accounting (week 1) - sharifah
 
What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)What are the causes of conflicts (Bahasa Malaysia)
What are the causes of conflicts (Bahasa Malaysia)
 
Lab 5 2012/2012
Lab 5 2012/2012Lab 5 2012/2012
Lab 5 2012/2012
 
Lab sheet 1
Lab sheet 1Lab sheet 1
Lab sheet 1
 
Test 1 alish schema 1
Test 1 alish schema 1Test 1 alish schema 1
Test 1 alish schema 1
 
Lab 6 sem ii_11_12
Lab 6 sem ii_11_12Lab 6 sem ii_11_12
Lab 6 sem ii_11_12
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&a
 
Test 1 skema q&a
Test 1 skema q&aTest 1 skema q&a
Test 1 skema q&a
 
Final project
Final projectFinal project
Final project
 
Final project
Final projectFinal project
Final project
 
Attn list test
Attn list testAttn list test
Attn list test
 
Carry markdam31303
Carry markdam31303Carry markdam31303
Carry markdam31303
 
Final project
Final projectFinal project
Final project
 
Final project
Final projectFinal project
Final project
 
Dti2143 dam31303 lab sheet 8
Dti2143 dam31303 lab sheet 8Dti2143 dam31303 lab sheet 8
Dti2143 dam31303 lab sheet 8
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
 

Lab 6

  • 1. UNIVERSITI TUN HUSSEIN ONN MALAYSIA FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING BTI 10202: COMPUTER PROGRAMMING LAB 6 : Control Statements – Part 2 (Loops) NAME : _____________________________ MATRICS NO.: _______________ DATE : _____ MARK Objectives: Students be able to develop a repetition statements or program by using the iteration structure (loops). i. while loop statement ii. do…while loop statement iii. for loop statement Theory: a. while Example: i. Syntax of while loop (one statement): /*Total up from marks 1 to marks 5.*/ while (condition- Code to execute while the while(count<=5) /* count from 1 until 5*/ condition is true) { { printf(“Key in the marks (%d) =”, Statement; count); } scanf(“%d”, &marks); total=total+marks; /*add the marks ii. Syntax of while loop (> one statement): one by one*/ while(condition- repeated until becomes false) count+=1; /*add the count*/ { } Statement 1; Statement 2; : : Statement n; } b. do…while loops are useful for things that want to loop at Example: least once /* Total up from marks 1 until marks 5*/ Notice that the condition is tested at the end of the block do instead of the beginning, so the block will be executed at { least once printf(“Key in the Syntax of do…while loop: marks(%d)=”,count); do scanf(“%d”, &marks); { total=total+marks; ‘* total up the Statement 1; marks one by one*/ : count+=1; /*add count*/ 1
  • 2. : } Statements n; while(count<=5); /*repeat until count } reach 5*/ while(condition); c. for condition tells the program that while the conditional Example: expression is true the loop should continue to repeat /*Total up from marks 1 until marks 5*/ itself. for(count=1;count<=5;count+=1) Syntax of for loop: /*can write as ++count*/ for(initial value; condition; update counter) { { printf(“key in the Statement(s); marks(%d)=”,count); } scanf(“%d”, &marks); total=total+marks; /* total up the marks one by one*/ } Flowchart for loop structures d. Nested loops Example: #include<stdio.h> #include<conio.h> main() { int row,column,num; do 2
  • 3. { printf(“nEnter the number of row of your triangle (1-30 only):n”); scanf(“%d”,&num); } while(num<=0||num>30); /*cannot more than 30 or less than 0*/ printf(“Your trianglen”); for(row=1;row<=num;++row) { printf(“n”); for(column=1;column<=(row*2-1);column++) { printf(“*”); } } getch(); } Exercise: 1. What is the output of following ‘for’ loop source code? #include<stdio.h> #include<conio.h> Output main() { int i; printf("This is a for loopn"); for(i = -5; i <= 0; i = i +1) printf("%d ", i); getch(); } 3
  • 4. 2. Rewrite the ‘for’ loop program in Exercise 1 using ‘while’ loop statement to give the same output. While statement 3. Type the example source code for the nested loops (at theory: d section) and observe the output. Explain the program in detail. Output: 4
  • 5. 5