Lab 6
Upcoming SlideShare
Loading in...5

Lab 6






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Lab 6 Lab 6 Document Transcript

  • 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 : _____ MARKObjectives: 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 statementTheory: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 structuresd. 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 statement3. Type the example source code for the nested loops (at theory: d section) and observe theoutput. Explain the program in detail. Output: 4
  • 5