Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Fundamentals of Computer Programming Lab 8: While Loops, Lab Reports of Acting

Two programming examples for students in csci 101: one using a switch statement and another using functions. The document also introduces the concept of while loops and provides an example of a program that uses a while loop to read in a list of non-negative integers until a negative integer is entered, and then outputs the sum, average, maximum, and minimum of these integers. The document also includes an exercise for students to write a bank account program using a while loop.

Typology: Lab Reports

Pre 2010

Uploaded on 11/08/2009

koofers-user-prx
koofers-user-prx 🇺🇸

10 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Fundamentals of Computer Programming Lab 8: While Loops and more Lab Reports Acting in PDF only on Docsity! CSCI 101: Fundamentals of Computer Programming Lab 8: While Loops Fall 2005 Program Tracing Tracing a program is a fundamental test in understanding a programming language. Listed here are two programs (switch statements and functions), which you should be able to trace out the output. Program 1: int num; int alpha = 10; scanf( “%d “, &num); switch (num) { case 3 : alpha++; break; case 4 : alpha += 2; break; case 8 : alpha += 3; default : alpha += 4; } printf (“%d \n“,alpha ); Program 2: #include <stdio.h> int Try( int*, int*, int* ); int TryAgain(int, int) ; int main() { int x=1 ,y=2, z=3, D; D = Try(&x,&y,&z); printf (“%d %d %d %d\n”, x , y , z, D ); D = TryAgain(x+y, y+z); printf (“%d %d %d %d\n”, x , y , z, D ); return 0; } int Try( int *x, int *y, int *z ) { int x1; x1 = *x + 2; *x += x1++; *y = *y * 3; *z = x1 + *y + *z ; printf(“%d\n“ , x1); return (x1); } int TryAgain(int x, int y) { int z ; z = x + y ; return (z); } Loops Introduction So far the programs we have written involved only commands that run once. However, the real power of computer programs lies in the ability to perform a big number of calculations in a short time. One of the ways to harness this capability is by using loops. Loops are general repetition statements that allow the same commands in a program to be run over and over again. An example of a loop is the while loop. The general format of a while loop is as follows: while (expression) { commands; … } The program will evaluate the expression in the parentheses after while to decide whether to run the commands in the brackets following while. If the expression evaluates as true, the program shall execute the commands in the brackets following while. However, instead of going on to the next command when all the commands in the brackets have been executed, the program will re-evaluate the expression in the parentheses following the while keyword, and if true, will execute the commands in the brackets again. This process is repeated until the expression in the parentheses after while evaluates to false, in which case the program will ignore all the commands in the bracket, and move on to the next command. The while keyword, the expression in the parentheses, and the commands in the brackets make up the while loop. Hence it is very important that the value of the expression in the parentheses following the while keyword be one that can be changed in the while loop. Otherwise, if the expression evaluates to true the first time round, the loop will become an infinite loop that will not stop running. Loops Example We shall illustrate the while loop with a program example. This program will read in a list of non-negative integers until a negative integer is entered. It will then output the sum, average,
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved