# Lec4 Loops.pptnew1

• View
220

• Download
0

Embed Size (px)

### Text of Lec4 Loops.pptnew1

• 8/14/2019 Lec4 Loops.pptnew1

1/24

s

s

CS 1001 Programming in CLecture 4

More on Decision

Making and Loops inC

• 8/14/2019 Lec4 Loops.pptnew1

2/24

• 8/14/2019 Lec4 Loops.pptnew1

3/24

Logic Operators in if

Equal to if (x==10)

Not equal to if (x!=10) Less than if (x10)

Less than / equal to if (x=10)

• 8/14/2019 Lec4 Loops.pptnew1

4/24

Compound Operators

Logical AND if (x==1 && y==2)

Logical OR if (x==1 || y==2)

Logical NOT if (!x)

• 8/14/2019 Lec4 Loops.pptnew1

5/24

Single and Compound Statements

Single statements:

if (condition)

true_statement;

elsefalse_statement;

Multiple statements:

if (condition) {

}

else {

}

• 8/14/2019 Lec4 Loops.pptnew1

6/24

Nested If Statements

void main(void)

{int winner =3;

printf(and the winner of ICC is );

if (winner==1) printf(Pakistan);

else if (winner==2)

printf(England);

else if (winner==3)

printf(WI);

else

printf(Australia);

}

• 8/14/2019 Lec4 Loops.pptnew1

7/24

Conditional Statement

void main(void)

{

int num;

printf("Enter a value:");

scanf("%d", &num);

if (num < 0)

printf("The value is negative\n");

else if (num == 0)

printf("The value is zero\n");

else

printf("The value is positive\n");

return 0;

}

• 8/14/2019 Lec4 Loops.pptnew1

8/24

Switch Statements

Switch statements look like thisexample:

switch (expression) {

value_1 : statements_1; break;

value_2 : statements_2; break;

...

value_n : statements_n; break

default

• 8/14/2019 Lec4 Loops.pptnew1

9/24

switch (winner) {

case 1 : printf(Pakistan);

break;

case 2 : printf(England);

break;

.

value_n : statements_n; break

default:

• 8/14/2019 Lec4 Loops.pptnew1

10/24

Loops

Repeat a series of statements Not reasonable to copy statements

multiple time

Need a way to repeat a block of code

Loops allow repetition

• 8/14/2019 Lec4 Loops.pptnew1

11/24

The For Loop

Syntax: for (initialization;test;increment)

{

statements;

}

The for loop will first perform the initialization.Then, as long test is TRUE, it will execute

statements. After each execution, it willincrement.

• 8/14/2019 Lec4 Loops.pptnew1

12/24

For loop

Known number of iterations

for(count=1;count

• 8/14/2019 Lec4 Loops.pptnew1

13/24

For loop examples

for (x=20 ; x = 20; x -=10) from 80 to 20 in steps of -10

• 8/14/2019 Lec4 Loops.pptnew1

14/24

While loop

Unknown number of iterations

degree =0;

while (degrees

• 8/14/2019 Lec4 Loops.pptnew1

15/24

The While Loop

An example while loop looks like this:

void main(void)

{

char ch;

while (ch != Q || ch != q)

{

ch = getchar( );

}

}

• 8/14/2019 Lec4 Loops.pptnew1

16/24

The Do-while Loop

The do-while loop repeatedly executes ablock of code indicated by statements aslong as the conditional expressioncond_expr is true.

do {

statements;

} while (cond_expr);

• 8/14/2019 Lec4 Loops.pptnew1

17/24

A while for for

i=0;while(i

• 8/14/2019 Lec4 Loops.pptnew1

18/24

A for for while

for(; degree

• 8/14/2019 Lec4 Loops.pptnew1

19/24

Special Cases

You can have as many controlvariables as you want in loops. Thefollowing is fine:

for (x=0, y=0; x+y

• 8/14/2019 Lec4 Loops.pptnew1

20/24

A forever loop

Using forfor(;;)

{

printf(Would you please stop teaching C\n);

}

Using while

while(1)

{printf(Would you please stop teaching C\n);

}

• 8/14/2019 Lec4 Loops.pptnew1

21/24

Fahrenheit-to-Celsius conversionvoid main(void)

{int deg_c, deg_f;

deg_c = 0;

while (deg_c

• 8/14/2019 Lec4 Loops.pptnew1

22/24

int main()

{

int count = 1;

while (count

• 8/14/2019 Lec4 Loops.pptnew1

23/24

Continue & Break

The continue statement is used to forceprogram execution to the bottom of the loop,skipping any statements that come after it.

The breakstatement is used to haltexecution of a loop prior to the loops normaltest condition being met.

The exit statement causes the wholeprogram to terminate if it is called within themain program block.

• 8/14/2019 Lec4 Loops.pptnew1

24/24

Coding for readability

int main()

{

int i,x,y;float z;

.

.

if (x < 7)

{

y= 3;

z= 4.2;}

else if (x > 23)

{

y= 5;

z= 7.2

}

for (i= 1; i < 200; i++) {

for (j= 1; j < 200; j++ {

/* Inside both loops */

}

/* Inside only the first loop */

}

return 0;}

Right bracket

level with

statement whichstarted it

Always indent after

a left bracket

Start a

left bracket aftera statement

Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Science
Documents
Documents
Documents
Software
Documents
Documents
Documents
Education
Documents
Documents
Documents
Documents
Documents
Education
Science
Documents
Documents
Environment