Lab 4 - FL13 - ECE 198 JL - University of Illinois - Engineering Wiki

Google printf and pic k the firs t res ult to read

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ot; will have impac t on what is dis play ed on the s c reen. Google "printf" and pic k the firs t res ult to read about "printf". W rite y our ans wers in lab4/lab4.tx t file. Don't forget to c ommit it to y our s vn, otherwis e we will have nothing to grade. Looping Constructs in C In every programming language, thus als o in the C programming language, there are c irc ums tanc es where y ou want to do the s ame thing many times . For ins tanc e y ou want to print the s ame words ten times . You c ould ty pe ten printf func tion c alls , but it is eas ier to us e a loop. The only thing y ou have to do is to s etup a loop that ex ec utes the s ame printf func tion ten times . There are three bas ic ty pes of loops whic h are: “for loop” “while loop” “do while loop” W e a re inte ntiona lly going to skip introducing the "do w hile " loop. T he for loop The “for” loop, loops from one number to another number and inc reas es by a s pec ified value eac h time. It us es the following s truc ture: fr(tr vle edcniin udt vle o sat au; n odto; pae au) { saeet; ttmns .. . } Here is a s imple ex ample of the for loop. This C program prints "Hello W orld !" 3 times on the s c reen. #nld<ti.> icuesdoh itmi( n an) { iti n; } fr( =0 i<3 i+ oi ; ; +) { pit(HloWrd!n) rnf"el ol \"; } rtr 0 eun ; This "for" loop is equivalent to the following infinitely nes ted if s tatement: { } iti=0 n ; i ( <3 fi ) { pit(HloWrd\"; rnf"el ol!n) i+ +; i ( <3 fi ) { pit(HloWrd\"; rnf"el ol!n) i+ +; i ( <3 fi ) { pit(HloWrd\"; rnf"el ol!n) i+ + i ( <3 fi ) { .. . .. . } } } } .. . Let's trac e through what happens at eac h iteration: In ite ra tion 0 i=0(is cas o "o"cntut frt lue f fr osrc) Tepormte cek wehri<3 h rga hn hcs hte rsl =tu,hnetepormfo ge it telo eut re ec h rga lw os no h op pitHloWrd! rn el ol Tefrlo nwiceet i hnei=i1(bcuei+i teices vleta i mnind h o op o nrmns...
View Full Document

This document was uploaded on 03/22/2014.

Ask a homework question - tutors are online