Top Banner
Statements Lecture 4 October 29, 2019 (Lecture 4) Statements October 29, 2019 1 / 27
99

Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Jun 27, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Statements

Lecture 4

October 29, 2019

(Lecture 4) Statements October 29, 2019 1 / 27

Page 2: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Outline

1 Statementsthe switch statement

2 Loops - iteration statementsThe while statementThe do-while statementThe for statement

3 JumpsThe break statementThe continue statementThe goto statement

(Lecture 4) Statements October 29, 2019 2 / 27

Page 3: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Statements

Statement is terminated by ;

Statement consisting only of the semicolon is empty statement.Selection statement:

Selection statement: if or if-else,

Switch statement: switch-case.

Iteration statement - loops:

for, while,do-while

Jump statements (unconditional program branching)

continue,break,return,goto,

(Lecture 4) Statements October 29, 2019 3 / 27

Page 4: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Statements

Statement is terminated by ;

Statement consisting only of the semicolon is empty statement.Selection statement:

Selection statement: if or if-else,Switch statement: switch-case.

Iteration statement - loops:

for, while,do-while

Jump statements (unconditional program branching)

continue,break,return,goto,

(Lecture 4) Statements October 29, 2019 3 / 27

Page 5: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Statements

Statement is terminated by ;

Statement consisting only of the semicolon is empty statement.Selection statement:

Selection statement: if or if-else,Switch statement: switch-case.

Iteration statement - loops:for, while,do-while

Jump statements (unconditional program branching)

continue,break,return,goto,

(Lecture 4) Statements October 29, 2019 3 / 27

Page 6: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Statements

Statement is terminated by ;

Statement consisting only of the semicolon is empty statement.Selection statement:

Selection statement: if or if-else,Switch statement: switch-case.

Iteration statement - loops:for, while,do-while

Jump statements (unconditional program branching)continue,break,return,goto,

(Lecture 4) Statements October 29, 2019 3 / 27

Page 7: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

The switch statement allows to branch the program based on thevalue of the expression of the integer types.

The switch statement has the form:

switch(expression){case constant_1:

statements_1;break;

case constant_2:statements_2;

break;...

default:statements_3;

}

Constants are of the same type as the expression.

(Lecture 4) Statements October 29, 2019 4 / 27

Page 8: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

The switch statement allows to branch the program based on thevalue of the expression of the integer types.The switch statement has the form:

switch(expression){case constant_1:

statements_1;break;

case constant_2:statements_2;

break;...

default:statements_3;

}

Constants are of the same type as the expression.

(Lecture 4) Statements October 29, 2019 4 / 27

Page 9: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

The switch statement allows to branch the program based on thevalue of the expression of the integer types.The switch statement has the form:

switch(expression){case constant_1:

statements_1;break;

case constant_2:statements_2;

break;...

default:statements_3;

}

Constants are of the same type as the expression.(Lecture 4) Statements October 29, 2019 4 / 27

Page 10: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

A switch statement is often easier to read and faster than a cascadedif statement.

Controlling expression - the switch must be followed by an integerexpression. Characters are treated as integers. Floating-pointnumbers and strings don’t qualify.Case labels - constant expressions can’t contain variables or functioncalls.

5 is a constant,5 + 10 is a constant expression,n+ 10 isn’t a constant expression.

Duplicate case labels aren’t allowed.switch statement is a form of ”computed jump” - when thecontrolling expression is evaluated, control jumps to the case labelmatching the value of the switch expression.A case label is nothing more than a marker indicating a positionwithin the switch. Without break (or some other jump statement),control will flow from one case into the next.

(Lecture 4) Statements October 29, 2019 5 / 27

Page 11: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

A switch statement is often easier to read and faster than a cascadedif statement.Controlling expression - the switch must be followed by an integerexpression. Characters are treated as integers. Floating-pointnumbers and strings don’t qualify.

Case labels - constant expressions can’t contain variables or functioncalls.

5 is a constant,5 + 10 is a constant expression,n+ 10 isn’t a constant expression.

Duplicate case labels aren’t allowed.switch statement is a form of ”computed jump” - when thecontrolling expression is evaluated, control jumps to the case labelmatching the value of the switch expression.A case label is nothing more than a marker indicating a positionwithin the switch. Without break (or some other jump statement),control will flow from one case into the next.

(Lecture 4) Statements October 29, 2019 5 / 27

Page 12: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

A switch statement is often easier to read and faster than a cascadedif statement.Controlling expression - the switch must be followed by an integerexpression. Characters are treated as integers. Floating-pointnumbers and strings don’t qualify.Case labels - constant expressions can’t contain variables or functioncalls.

5 is a constant,5 + 10 is a constant expression,n+ 10 isn’t a constant expression.

Duplicate case labels aren’t allowed.switch statement is a form of ”computed jump” - when thecontrolling expression is evaluated, control jumps to the case labelmatching the value of the switch expression.A case label is nothing more than a marker indicating a positionwithin the switch. Without break (or some other jump statement),control will flow from one case into the next.

(Lecture 4) Statements October 29, 2019 5 / 27

Page 13: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

A switch statement is often easier to read and faster than a cascadedif statement.Controlling expression - the switch must be followed by an integerexpression. Characters are treated as integers. Floating-pointnumbers and strings don’t qualify.Case labels - constant expressions can’t contain variables or functioncalls.

5 is a constant,

5 + 10 is a constant expression,n+ 10 isn’t a constant expression.

Duplicate case labels aren’t allowed.switch statement is a form of ”computed jump” - when thecontrolling expression is evaluated, control jumps to the case labelmatching the value of the switch expression.A case label is nothing more than a marker indicating a positionwithin the switch. Without break (or some other jump statement),control will flow from one case into the next.

(Lecture 4) Statements October 29, 2019 5 / 27

Page 14: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

A switch statement is often easier to read and faster than a cascadedif statement.Controlling expression - the switch must be followed by an integerexpression. Characters are treated as integers. Floating-pointnumbers and strings don’t qualify.Case labels - constant expressions can’t contain variables or functioncalls.

5 is a constant,5 + 10 is a constant expression,

n+ 10 isn’t a constant expression.

Duplicate case labels aren’t allowed.switch statement is a form of ”computed jump” - when thecontrolling expression is evaluated, control jumps to the case labelmatching the value of the switch expression.A case label is nothing more than a marker indicating a positionwithin the switch. Without break (or some other jump statement),control will flow from one case into the next.

(Lecture 4) Statements October 29, 2019 5 / 27

Page 15: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

A switch statement is often easier to read and faster than a cascadedif statement.Controlling expression - the switch must be followed by an integerexpression. Characters are treated as integers. Floating-pointnumbers and strings don’t qualify.Case labels - constant expressions can’t contain variables or functioncalls.

5 is a constant,5 + 10 is a constant expression,n+ 10 isn’t a constant expression.

Duplicate case labels aren’t allowed.switch statement is a form of ”computed jump” - when thecontrolling expression is evaluated, control jumps to the case labelmatching the value of the switch expression.A case label is nothing more than a marker indicating a positionwithin the switch. Without break (or some other jump statement),control will flow from one case into the next.

(Lecture 4) Statements October 29, 2019 5 / 27

Page 16: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

A switch statement is often easier to read and faster than a cascadedif statement.Controlling expression - the switch must be followed by an integerexpression. Characters are treated as integers. Floating-pointnumbers and strings don’t qualify.Case labels - constant expressions can’t contain variables or functioncalls.

5 is a constant,5 + 10 is a constant expression,n+ 10 isn’t a constant expression.

Duplicate case labels aren’t allowed.

switch statement is a form of ”computed jump” - when thecontrolling expression is evaluated, control jumps to the case labelmatching the value of the switch expression.A case label is nothing more than a marker indicating a positionwithin the switch. Without break (or some other jump statement),control will flow from one case into the next.

(Lecture 4) Statements October 29, 2019 5 / 27

Page 17: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

A switch statement is often easier to read and faster than a cascadedif statement.Controlling expression - the switch must be followed by an integerexpression. Characters are treated as integers. Floating-pointnumbers and strings don’t qualify.Case labels - constant expressions can’t contain variables or functioncalls.

5 is a constant,5 + 10 is a constant expression,n+ 10 isn’t a constant expression.

Duplicate case labels aren’t allowed.switch statement is a form of ”computed jump” - when thecontrolling expression is evaluated, control jumps to the case labelmatching the value of the switch expression.

A case label is nothing more than a marker indicating a positionwithin the switch. Without break (or some other jump statement),control will flow from one case into the next.

(Lecture 4) Statements October 29, 2019 5 / 27

Page 18: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

A switch statement is often easier to read and faster than a cascadedif statement.Controlling expression - the switch must be followed by an integerexpression. Characters are treated as integers. Floating-pointnumbers and strings don’t qualify.Case labels - constant expressions can’t contain variables or functioncalls.

5 is a constant,5 + 10 is a constant expression,n+ 10 isn’t a constant expression.

Duplicate case labels aren’t allowed.switch statement is a form of ”computed jump” - when thecontrolling expression is evaluated, control jumps to the case labelmatching the value of the switch expression.A case label is nothing more than a marker indicating a positionwithin the switch. Without break (or some other jump statement),control will flow from one case into the next.

(Lecture 4) Statements October 29, 2019 5 / 27

Page 19: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

(Lecture 4) Statements October 29, 2019 6 / 27

Page 20: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switch

(Lecture 4) Statements October 29, 2019 7 / 27

Page 21: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Selection statement - switchfor(int i=0;i<7;i++)switch(i){

case 0:printf("zero\n");

break;case 2:case 4:

printf("%d - even number\n", i);break;case 1:case 3:case 5:

printf("%d - odd number\n", i);break;default:

printf("%d is greater then 5 \n", i);}

(Lecture 4) Statements October 29, 2019 8 / 27

Page 22: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Loops - iteration statements

A loop is a statement whose job is to to repeatedly execute someother statement (the loop body).

In C, every loop has a controlling expression.Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

If the expression is true (a value is nonzero) the loops continues toexecute.

C provides three iterations statement:while, for, do-while.The while statement is used for loops whose controlling expression istested before the body of the loop is executed.

The do-while statement is used if the expression is tested after theloop body is executed.

The for statement is convenient for loops that increment ordecrement a counting variable.

The comma operator is used in for statement.

(Lecture 4) Statements October 29, 2019 9 / 27

Page 23: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Loops - iteration statements

A loop is a statement whose job is to to repeatedly execute someother statement (the loop body).

In C, every loop has a controlling expression.

Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

If the expression is true (a value is nonzero) the loops continues toexecute.

C provides three iterations statement:while, for, do-while.The while statement is used for loops whose controlling expression istested before the body of the loop is executed.

The do-while statement is used if the expression is tested after theloop body is executed.

The for statement is convenient for loops that increment ordecrement a counting variable.

The comma operator is used in for statement.

(Lecture 4) Statements October 29, 2019 9 / 27

Page 24: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Loops - iteration statements

A loop is a statement whose job is to to repeatedly execute someother statement (the loop body).

In C, every loop has a controlling expression.Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

If the expression is true (a value is nonzero) the loops continues toexecute.

C provides three iterations statement:while, for, do-while.The while statement is used for loops whose controlling expression istested before the body of the loop is executed.

The do-while statement is used if the expression is tested after theloop body is executed.

The for statement is convenient for loops that increment ordecrement a counting variable.

The comma operator is used in for statement.

(Lecture 4) Statements October 29, 2019 9 / 27

Page 25: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Loops - iteration statements

A loop is a statement whose job is to to repeatedly execute someother statement (the loop body).

In C, every loop has a controlling expression.Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

If the expression is true (a value is nonzero) the loops continues toexecute.

C provides three iterations statement:while, for, do-while.The while statement is used for loops whose controlling expression istested before the body of the loop is executed.

The do-while statement is used if the expression is tested after theloop body is executed.

The for statement is convenient for loops that increment ordecrement a counting variable.

The comma operator is used in for statement.

(Lecture 4) Statements October 29, 2019 9 / 27

Page 26: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Loops - iteration statements

A loop is a statement whose job is to to repeatedly execute someother statement (the loop body).

In C, every loop has a controlling expression.Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

If the expression is true (a value is nonzero) the loops continues toexecute.

C provides three iterations statement:while, for, do-while.

The while statement is used for loops whose controlling expression istested before the body of the loop is executed.

The do-while statement is used if the expression is tested after theloop body is executed.

The for statement is convenient for loops that increment ordecrement a counting variable.

The comma operator is used in for statement.

(Lecture 4) Statements October 29, 2019 9 / 27

Page 27: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Loops - iteration statements

A loop is a statement whose job is to to repeatedly execute someother statement (the loop body).

In C, every loop has a controlling expression.Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

If the expression is true (a value is nonzero) the loops continues toexecute.

C provides three iterations statement:while, for, do-while.The while statement is used for loops whose controlling expression istested before the body of the loop is executed.

The do-while statement is used if the expression is tested after theloop body is executed.

The for statement is convenient for loops that increment ordecrement a counting variable.

The comma operator is used in for statement.

(Lecture 4) Statements October 29, 2019 9 / 27

Page 28: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Loops - iteration statements

A loop is a statement whose job is to to repeatedly execute someother statement (the loop body).

In C, every loop has a controlling expression.Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

If the expression is true (a value is nonzero) the loops continues toexecute.

C provides three iterations statement:while, for, do-while.The while statement is used for loops whose controlling expression istested before the body of the loop is executed.

The do-while statement is used if the expression is tested after theloop body is executed.

The for statement is convenient for loops that increment ordecrement a counting variable.

The comma operator is used in for statement.

(Lecture 4) Statements October 29, 2019 9 / 27

Page 29: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Loops - iteration statements

A loop is a statement whose job is to to repeatedly execute someother statement (the loop body).

In C, every loop has a controlling expression.Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

If the expression is true (a value is nonzero) the loops continues toexecute.

C provides three iterations statement:while, for, do-while.The while statement is used for loops whose controlling expression istested before the body of the loop is executed.

The do-while statement is used if the expression is tested after theloop body is executed.

The for statement is convenient for loops that increment ordecrement a counting variable.

The comma operator is used in for statement.

(Lecture 4) Statements October 29, 2019 9 / 27

Page 30: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

Loops - iteration statements

A loop is a statement whose job is to to repeatedly execute someother statement (the loop body).

In C, every loop has a controlling expression.Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

If the expression is true (a value is nonzero) the loops continues toexecute.

C provides three iterations statement:while, for, do-while.The while statement is used for loops whose controlling expression istested before the body of the loop is executed.

The do-while statement is used if the expression is tested after theloop body is executed.

The for statement is convenient for loops that increment ordecrement a counting variable.

The comma operator is used in for statement.(Lecture 4) Statements October 29, 2019 9 / 27

Page 31: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The while statement has the form:while ( expression ) statement

int i = 1;while ( i < 10 ) // controlling expression

i = i * 2; //loop body

When a while statement is executed, the controlling expression isevaluated first.If its value is nonzero (true), the loop is executed and the expressionis tested again.The controlling expression eventually has value zero.

(Lecture 4) Statements October 29, 2019 10 / 27

Page 32: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The while statement has the form:while ( expression ) statement

int i = 1;while ( i < 10 ) // controlling expression

i = i * 2; //loop body

When a while statement is executed, the controlling expression isevaluated first.If its value is nonzero (true), the loop is executed and the expressionis tested again.The controlling expression eventually has value zero.

(Lecture 4) Statements October 29, 2019 10 / 27

Page 33: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The while statement has the form:while ( expression ) statement

int i = 1;while ( i < 10 ) // controlling expression

i = i * 2; //loop body

When a while statement is executed, the controlling expression isevaluated first.

If its value is nonzero (true), the loop is executed and the expressionis tested again.The controlling expression eventually has value zero.

(Lecture 4) Statements October 29, 2019 10 / 27

Page 34: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The while statement has the form:while ( expression ) statement

int i = 1;while ( i < 10 ) // controlling expression

i = i * 2; //loop body

When a while statement is executed, the controlling expression isevaluated first.If its value is nonzero (true), the loop is executed and the expressionis tested again.

The controlling expression eventually has value zero.

(Lecture 4) Statements October 29, 2019 10 / 27

Page 35: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The while statement has the form:while ( expression ) statement

int i = 1;while ( i < 10 ) // controlling expression

i = i * 2; //loop body

When a while statement is executed, the controlling expression isevaluated first.If its value is nonzero (true), the loop is executed and the expressionis tested again.The controlling expression eventually has value zero.

(Lecture 4) Statements October 29, 2019 10 / 27

Page 36: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The while statement has the form:while ( expression ) statement

int i = 1;while ( i < 10 ) // controlling expression

i = i * 2; //loop body

When a while statement is executed, the controlling expression isevaluated first.If its value is nonzero (true), the loop is executed and the expressionis tested again.The controlling expression eventually has value zero.

(Lecture 4) Statements October 29, 2019 10 / 27

Page 37: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

int n = 5;while (n < 7) {

printf("A. n = %d\n", n);n++;printf("B. n = %d\n", n);

}printf("End n = %d\n", n);

A. n = 5B. n = 6A. n = 6B. n = 7End n = 7

n = 5;while (n++ < 7) {

printf("C. n = %d\n", n);}printf("End n = %d\n", n);

C. n = 6C. n = 7End n = 8

(Lecture 4) Statements October 29, 2019 11 / 27

Page 38: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

int n = 5;while (n < 7) {

printf("A. n = %d\n", n);n++;printf("B. n = %d\n", n);

}printf("End n = %d\n", n);

A. n = 5B. n = 6A. n = 6B. n = 7End n = 7

n = 5;while (n++ < 7) {

printf("C. n = %d\n", n);}printf("End n = %d\n", n);

C. n = 6C. n = 7End n = 8

(Lecture 4) Statements October 29, 2019 11 / 27

Page 39: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

int n = 5;while (n < 7) {

printf("A. n = %d\n", n);n++;printf("B. n = %d\n", n);

}printf("End n = %d\n", n);

A. n = 5B. n = 6A. n = 6B. n = 7End n = 7

n = 5;while (n++ < 7) {

printf("C. n = %d\n", n);}printf("End n = %d\n", n);

C. n = 6C. n = 7End n = 8

(Lecture 4) Statements October 29, 2019 11 / 27

Page 40: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The controlling expression is false when a while loop terminates.

The body of a while loop may not be executed at all. The controllingexpression is is tested before the loop body is executed.

Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

A while statement won’t terminate if the controlling expression hasalways a nonzero value.

Infinite loops:

while (1)statement;

A while statement of this form will execute forever unless its bodycontains a statement that transfers control out of the loop (break,goto, return).

(Lecture 4) Statements October 29, 2019 12 / 27

Page 41: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The controlling expression is false when a while loop terminates.

The body of a while loop may not be executed at all. The controllingexpression is is tested before the loop body is executed.

Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

A while statement won’t terminate if the controlling expression hasalways a nonzero value.

Infinite loops:

while (1)statement;

A while statement of this form will execute forever unless its bodycontains a statement that transfers control out of the loop (break,goto, return).

(Lecture 4) Statements October 29, 2019 12 / 27

Page 42: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The controlling expression is false when a while loop terminates.

The body of a while loop may not be executed at all. The controllingexpression is is tested before the loop body is executed.

Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

A while statement won’t terminate if the controlling expression hasalways a nonzero value.

Infinite loops:

while (1)statement;

A while statement of this form will execute forever unless its bodycontains a statement that transfers control out of the loop (break,goto, return).

(Lecture 4) Statements October 29, 2019 12 / 27

Page 43: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The controlling expression is false when a while loop terminates.

The body of a while loop may not be executed at all. The controllingexpression is is tested before the loop body is executed.

Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

A while statement won’t terminate if the controlling expression hasalways a nonzero value.

Infinite loops:

while (1)statement;

A while statement of this form will execute forever unless its bodycontains a statement that transfers control out of the loop (break,goto, return).

(Lecture 4) Statements October 29, 2019 12 / 27

Page 44: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The controlling expression is false when a while loop terminates.

The body of a while loop may not be executed at all. The controllingexpression is is tested before the loop body is executed.

Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

A while statement won’t terminate if the controlling expression hasalways a nonzero value.

Infinite loops:

while (1)statement;

A while statement of this form will execute forever unless its bodycontains a statement that transfers control out of the loop (break,goto, return).

(Lecture 4) Statements October 29, 2019 12 / 27

Page 45: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The while statement

The controlling expression is false when a while loop terminates.

The body of a while loop may not be executed at all. The controllingexpression is is tested before the loop body is executed.

Each time the loop body is executed (an iteration of the loop), thecontrolling expression is evaluated.

A while statement won’t terminate if the controlling expression hasalways a nonzero value.

Infinite loops:

while (1)statement;

A while statement of this form will execute forever unless its bodycontains a statement that transfers control out of the loop (break,goto, return).

(Lecture 4) Statements October 29, 2019 12 / 27

Page 46: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The do-while statement

The do-while statement has the form:do statement while ( expression );

The do-while statement is just a while statement whose controllingexpression is evaluated after each execution of the loop body.

Whan a do statement is executed, the loop body is executed first,then the controlling expression is evaluated.If the value of the expression is nonzero (true), the loop body isexecuted again.Execution of the do statement terminates when the controllingexpression has the value zero (false) after the loop body has beenexecuted.

(Lecture 4) Statements October 29, 2019 13 / 27

Page 47: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The do-while statement

The do-while statement has the form:do statement while ( expression );The do-while statement is just a while statement whose controllingexpression is evaluated after each execution of the loop body.

Whan a do statement is executed, the loop body is executed first,then the controlling expression is evaluated.If the value of the expression is nonzero (true), the loop body isexecuted again.Execution of the do statement terminates when the controllingexpression has the value zero (false) after the loop body has beenexecuted.

(Lecture 4) Statements October 29, 2019 13 / 27

Page 48: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The do-while statement

The do-while statement has the form:do statement while ( expression );The do-while statement is just a while statement whose controllingexpression is evaluated after each execution of the loop body.

Whan a do statement is executed, the loop body is executed first,then the controlling expression is evaluated.If the value of the expression is nonzero (true), the loop body isexecuted again.Execution of the do statement terminates when the controllingexpression has the value zero (false) after the loop body has beenexecuted.

(Lecture 4) Statements October 29, 2019 13 / 27

Page 49: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The do-while statement

The do-while statement has the form:do statement while ( expression );The do-while statement is just a while statement whose controllingexpression is evaluated after each execution of the loop body.

Whan a do statement is executed, the loop body is executed first,then the controlling expression is evaluated.

If the value of the expression is nonzero (true), the loop body isexecuted again.Execution of the do statement terminates when the controllingexpression has the value zero (false) after the loop body has beenexecuted.

(Lecture 4) Statements October 29, 2019 13 / 27

Page 50: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The do-while statement

The do-while statement has the form:do statement while ( expression );The do-while statement is just a while statement whose controllingexpression is evaluated after each execution of the loop body.

Whan a do statement is executed, the loop body is executed first,then the controlling expression is evaluated.If the value of the expression is nonzero (true), the loop body isexecuted again.

Execution of the do statement terminates when the controllingexpression has the value zero (false) after the loop body has beenexecuted.

(Lecture 4) Statements October 29, 2019 13 / 27

Page 51: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The do-while statement

The do-while statement has the form:do statement while ( expression );The do-while statement is just a while statement whose controllingexpression is evaluated after each execution of the loop body.

Whan a do statement is executed, the loop body is executed first,then the controlling expression is evaluated.If the value of the expression is nonzero (true), the loop body isexecuted again.Execution of the do statement terminates when the controllingexpression has the value zero (false) after the loop body has beenexecuted.

(Lecture 4) Statements October 29, 2019 13 / 27

Page 52: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement

The for statement is ideal for loops that have a ”counting” variable,but it’s versatile enough to be used for other kind of loops as well.

The for statement has the form

for(expression1 ; expression2 ; expression3)statement

Example:

for (int i = 0; i < 10; i++) // gnu11printf("%d * %d = %d", i, i, i*i);

When this for statement is executed, the variable i is initialized, theni is tested, then the message is printed, then i is incremented, thenthe condition is tested again.

The loop body will be executed 10 times.

(Lecture 4) Statements October 29, 2019 14 / 27

Page 53: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement

The for statement is ideal for loops that have a ”counting” variable,but it’s versatile enough to be used for other kind of loops as well.

The for statement has the form

for(expression1 ; expression2 ; expression3)statement

Example:

for (int i = 0; i < 10; i++) // gnu11printf("%d * %d = %d", i, i, i*i);

When this for statement is executed, the variable i is initialized, theni is tested, then the message is printed, then i is incremented, thenthe condition is tested again.

The loop body will be executed 10 times.

(Lecture 4) Statements October 29, 2019 14 / 27

Page 54: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement

The for statement is ideal for loops that have a ”counting” variable,but it’s versatile enough to be used for other kind of loops as well.

The for statement has the form

for(expression1 ; expression2 ; expression3)statement

Example:

for (int i = 0; i < 10; i++) // gnu11printf("%d * %d = %d", i, i, i*i);

When this for statement is executed, the variable i is initialized, theni is tested, then the message is printed, then i is incremented, thenthe condition is tested again.

The loop body will be executed 10 times.

(Lecture 4) Statements October 29, 2019 14 / 27

Page 55: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement

The for statement is ideal for loops that have a ”counting” variable,but it’s versatile enough to be used for other kind of loops as well.

The for statement has the form

for(expression1 ; expression2 ; expression3)statement

Example:

for (int i = 0; i < 10; i++) // gnu11printf("%d * %d = %d", i, i, i*i);

When this for statement is executed, the variable i is initialized, theni is tested, then the message is printed, then i is incremented, thenthe condition is tested again.

The loop body will be executed 10 times.

(Lecture 4) Statements October 29, 2019 14 / 27

Page 56: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement

The for statement is ideal for loops that have a ”counting” variable,but it’s versatile enough to be used for other kind of loops as well.

The for statement has the form

for(expression1 ; expression2 ; expression3)statement

Example:

for (int i = 0; i < 10; i++) // gnu11printf("%d * %d = %d", i, i, i*i);

When this for statement is executed, the variable i is initialized, theni is tested, then the message is printed, then i is incremented, thenthe condition is tested again.

The loop body will be executed 10 times.

(Lecture 4) Statements October 29, 2019 14 / 27

Page 57: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement

The for statement is closely related to the while statement.

Except in a few rare cases, a for loop

for (l_init; l_cond; l_expr)statement

can always be replaced by an equivalent while loop:

l_init;while (l_cond) {

statementl_expr;

}

l init - the first expression in a for statement is usually an assignment.

l expr - the third expression in a for statement is usually anincrement/decrement expression.

(Lecture 4) Statements October 29, 2019 15 / 27

Page 58: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement

The for statement is closely related to the while statement.

Except in a few rare cases, a for loop

for (l_init; l_cond; l_expr)statement

can always be replaced by an equivalent while loop:

l_init;while (l_cond) {

statementl_expr;

}

l init - the first expression in a for statement is usually an assignment.

l expr - the third expression in a for statement is usually anincrement/decrement expression.

(Lecture 4) Statements October 29, 2019 15 / 27

Page 59: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement

The for statement is closely related to the while statement.

Except in a few rare cases, a for loop

for (l_init; l_cond; l_expr)statement

can always be replaced by an equivalent while loop:

l_init;while (l_cond) {

statementl_expr;

}

l init - the first expression in a for statement is usually an assignment.

l expr - the third expression in a for statement is usually anincrement/decrement expression.

(Lecture 4) Statements October 29, 2019 15 / 27

Page 60: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement

The for statement is closely related to the while statement.

Except in a few rare cases, a for loop

for (l_init; l_cond; l_expr)statement

can always be replaced by an equivalent while loop:

l_init;while (l_cond) {

statementl_expr;

}

l init - the first expression in a for statement is usually an assignment.

l expr - the third expression in a for statement is usually anincrement/decrement expression.

(Lecture 4) Statements October 29, 2019 15 / 27

Page 61: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - idioms

Counting up from 0 to n− 1

for (int i=0; i<n; i++) ;

Counting up from 1 to n

for (int i=1; i<=n; i++) ;

Counting down from n− 1 to 0

for (int i=n-1; i>=0; i--) ;

Counting down from n to 1

for (int i=n; i>0; i--) ;

A controlling expression needs to be true at the beginning of the loop,then later become false so that loop can terminate.

A test such as i == n doesn’t make much sense, because it won’t betrue initially.

(Lecture 4) Statements October 29, 2019 16 / 27

Page 62: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - idioms

Counting up from 0 to n− 1

for (int i=0; i<n; i++) ;

Counting up from 1 to n

for (int i=1; i<=n; i++) ;

Counting down from n− 1 to 0

for (int i=n-1; i>=0; i--) ;

Counting down from n to 1

for (int i=n; i>0; i--) ;

A controlling expression needs to be true at the beginning of the loop,then later become false so that loop can terminate.

A test such as i == n doesn’t make much sense, because it won’t betrue initially.

(Lecture 4) Statements October 29, 2019 16 / 27

Page 63: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - idioms

Counting up from 0 to n− 1

for (int i=0; i<n; i++) ;

Counting up from 1 to n

for (int i=1; i<=n; i++) ;

Counting down from n− 1 to 0

for (int i=n-1; i>=0; i--) ;

Counting down from n to 1

for (int i=n; i>0; i--) ;

A controlling expression needs to be true at the beginning of the loop,then later become false so that loop can terminate.

A test such as i == n doesn’t make much sense, because it won’t betrue initially.

(Lecture 4) Statements October 29, 2019 16 / 27

Page 64: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - idioms

Counting up from 0 to n− 1

for (int i=0; i<n; i++) ;

Counting up from 1 to n

for (int i=1; i<=n; i++) ;

Counting down from n− 1 to 0

for (int i=n-1; i>=0; i--) ;

Counting down from n to 1

for (int i=n; i>0; i--) ;

A controlling expression needs to be true at the beginning of the loop,then later become false so that loop can terminate.

A test such as i == n doesn’t make much sense, because it won’t betrue initially.

(Lecture 4) Statements October 29, 2019 16 / 27

Page 65: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - idioms

Counting up from 0 to n− 1

for (int i=0; i<n; i++) ;

Counting up from 1 to n

for (int i=1; i<=n; i++) ;

Counting down from n− 1 to 0

for (int i=n-1; i>=0; i--) ;

Counting down from n to 1

for (int i=n; i>0; i--) ;

A controlling expression needs to be true at the beginning of the loop,then later become false so that loop can terminate.

A test such as i == n doesn’t make much sense, because it won’t betrue initially.

(Lecture 4) Statements October 29, 2019 16 / 27

Page 66: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - idioms

Counting up from 0 to n− 1

for (int i=0; i<n; i++) ;

Counting up from 1 to n

for (int i=1; i<=n; i++) ;

Counting down from n− 1 to 0

for (int i=n-1; i>=0; i--) ;

Counting down from n to 1

for (int i=n; i>0; i--) ;

A controlling expression needs to be true at the beginning of the loop,then later become false so that loop can terminate.

A test such as i == n doesn’t make much sense, because it won’t betrue initially.

(Lecture 4) Statements October 29, 2019 16 / 27

Page 67: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - comma operatorint main(void){

const int FIRST = 46;const int NEXT = 20;

for (int ounces=1, cost=FIRST; ounces < 17;ounces++, cost += NEXT)

printf("%5d$%4.2f\n", ounces , cost /100.0);return 0;

}

(Lecture 4) Statements October 29, 2019 17 / 27

Page 68: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - omitting expressions

The for statement is very flexible.

Some for loops may not need all three of the expressions thatnormally control the loop.

int i = 10;for ( ; i > 0; i--)

printf ("%d\n", i);

for (int i = 10; i > 0;)printf ("%d\n", i--);

int i = 10for (; i > 0;) // while (i>0)

printf ("%d\n", i--);

Infinite loop:

for ( ; ; )printf ("%s ", Infinity);

(Lecture 4) Statements October 29, 2019 18 / 27

Page 69: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - omitting expressions

The for statement is very flexible.Some for loops may not need all three of the expressions thatnormally control the loop.

int i = 10;for ( ; i > 0; i--)

printf ("%d\n", i);

for (int i = 10; i > 0;)printf ("%d\n", i--);

int i = 10for (; i > 0;) // while (i>0)

printf ("%d\n", i--);

Infinite loop:

for ( ; ; )printf ("%s ", Infinity);

(Lecture 4) Statements October 29, 2019 18 / 27

Page 70: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - omitting expressions

The for statement is very flexible.Some for loops may not need all three of the expressions thatnormally control the loop.

int i = 10;for ( ; i > 0; i--)

printf ("%d\n", i);

for (int i = 10; i > 0;)printf ("%d\n", i--);

int i = 10for (; i > 0;) // while (i>0)

printf ("%d\n", i--);

Infinite loop:

for ( ; ; )printf ("%s ", Infinity);

(Lecture 4) Statements October 29, 2019 18 / 27

Page 71: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - omitting expressions

The for statement is very flexible.Some for loops may not need all three of the expressions thatnormally control the loop.

int i = 10;for ( ; i > 0; i--)

printf ("%d\n", i);

for (int i = 10; i > 0;)printf ("%d\n", i--);

int i = 10for (; i > 0;) // while (i>0)

printf ("%d\n", i--);

Infinite loop:

for ( ; ; )printf ("%s ", Infinity);

(Lecture 4) Statements October 29, 2019 18 / 27

Page 72: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - omitting expressions

The for statement is very flexible.Some for loops may not need all three of the expressions thatnormally control the loop.

int i = 10;for ( ; i > 0; i--)

printf ("%d\n", i);

for (int i = 10; i > 0;)printf ("%d\n", i--);

int i = 10for (; i > 0;) // while (i>0)

printf ("%d\n", i--);

Infinite loop:

for ( ; ; )printf ("%s ", Infinity);

(Lecture 4) Statements October 29, 2019 18 / 27

Page 73: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - omitting expressions

The for statement is very flexible.Some for loops may not need all three of the expressions thatnormally control the loop.

int i = 10;for ( ; i > 0; i--)

printf ("%d\n", i);

for (int i = 10; i > 0;)printf ("%d\n", i--);

int i = 10for (; i > 0;) // while (i>0)

printf ("%d\n", i--);

Infinite loop:

for ( ; ; )printf ("%s ", Infinity);

(Lecture 4) Statements October 29, 2019 18 / 27

Page 74: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - the null statement

The null statement is good for writing loops whose bodies are empty.

#include <stdio.h>int main(void) {

int n, k = 1;scanf("%d", &n);

for (int i = n + 1; --i > 1; k *= i);

printf("Factorial (%d)=%d\n", n, k);return 0;

}

Factorial(12)=479001600 - Last correctly calculated using the typeint.

Factorial(13)=1932053504 - wrong, Factorial(13)= 6227020800

(Lecture 4) Statements October 29, 2019 19 / 27

Page 75: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - the null statement

The null statement is good for writing loops whose bodies are empty.

#include <stdio.h>int main(void) {

int n, k = 1;scanf("%d", &n);

for (int i = n + 1; --i > 1; k *= i);

printf("Factorial (%d)=%d\n", n, k);return 0;

}

Factorial(12)=479001600 - Last correctly calculated using the typeint.

Factorial(13)=1932053504 - wrong, Factorial(13)= 6227020800

(Lecture 4) Statements October 29, 2019 19 / 27

Page 76: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - the null statement

The null statement is good for writing loops whose bodies are empty.

#include <stdio.h>int main(void) {

int n, k = 1;scanf("%d", &n);

for (int i = n + 1; --i > 1; k *= i);

printf("Factorial (%d)=%d\n", n, k);return 0;

}

Factorial(12)=479001600 - Last correctly calculated using the typeint.

Factorial(13)=1932053504 - wrong, Factorial(13)= 6227020800

(Lecture 4) Statements October 29, 2019 19 / 27

Page 77: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The for statement - the null statement

The null statement is good for writing loops whose bodies are empty.

#include <stdio.h>int main(void) {

int n, k = 1;scanf("%d", &n);

for (int i = n + 1; --i > 1; k *= i);

printf("Factorial (%d)=%d\n", n, k);return 0;

}

Factorial(12)=479001600 - Last correctly calculated using the typeint.

Factorial(13)=1932053504 - wrong, Factorial(13)= 6227020800

(Lecture 4) Statements October 29, 2019 19 / 27

Page 78: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The break statement - exiting from a loop

The break statement makes it possible to write a loop with an exitpoint in the middle or with more then one exit points.

Example:

for(int t=0; t <100; t++){printf("%d", t);if (10 == t)

break;}

printf ("t = %d\n", t);

A break statement transfer control out of the innermost enclosingwhile, do-while, for or switch statement.

(Lecture 4) Statements October 29, 2019 20 / 27

Page 79: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The break statement - exiting from a loop

The break statement makes it possible to write a loop with an exitpoint in the middle or with more then one exit points.

Example:

for(int t=0; t <100; t++){printf("%d", t);if (10 == t)

break;}

printf ("t = %d\n", t);

A break statement transfer control out of the innermost enclosingwhile, do-while, for or switch statement.

(Lecture 4) Statements October 29, 2019 20 / 27

Page 80: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The break statement - exiting from a loop

The break statement makes it possible to write a loop with an exitpoint in the middle or with more then one exit points.

Example:

for(int t=0; t <100; t++){printf("%d", t);if (10 == t)

break;}

printf ("t = %d\n", t);

A break statement transfer control out of the innermost enclosingwhile, do-while, for or switch statement.

(Lecture 4) Statements October 29, 2019 20 / 27

Page 81: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The break statement - exiting from a loop

(Lecture 4) Statements October 29, 2019 21 / 27

Page 82: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The break statement - exiting from a loop

When loops are nested, the break statement can escape only onelevel of nesting.

while (...){switch (...){...

break;...}

}

(Lecture 4) Statements October 29, 2019 22 / 27

Page 83: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The break statement - exiting from a loop

When loops are nested, the break statement can escape only onelevel of nesting.

while (...){switch (...){...

break;...}

}

(Lecture 4) Statements October 29, 2019 22 / 27

Page 84: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The continue statementThe continue statement makes it possible to skip part of the loopiteration without jumping out of the loop.

break transfers control just past the end of a loop, while continuetransfers control to a point just before the end of the loop body.break - control leaves the loop,continue - control remains inside the loop.

double sum = 0.0;for(int i=0; i < 10; ++i){

printf("Enter a n%d: ",i);double nmbr;scanf("%lf" ,&nmbr);if(nmbr < 0.0)

continue;sum += nmbr;}

printf("Sum = %.2lf\n",sum);}

(Lecture 4) Statements October 29, 2019 23 / 27

Page 85: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The continue statementThe continue statement makes it possible to skip part of the loopiteration without jumping out of the loop.break transfers control just past the end of a loop, while continuetransfers control to a point just before the end of the loop body.

break - control leaves the loop,continue - control remains inside the loop.

double sum = 0.0;for(int i=0; i < 10; ++i){

printf("Enter a n%d: ",i);double nmbr;scanf("%lf" ,&nmbr);if(nmbr < 0.0)

continue;sum += nmbr;}

printf("Sum = %.2lf\n",sum);}

(Lecture 4) Statements October 29, 2019 23 / 27

Page 86: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The continue statementThe continue statement makes it possible to skip part of the loopiteration without jumping out of the loop.break transfers control just past the end of a loop, while continuetransfers control to a point just before the end of the loop body.break - control leaves the loop,

continue - control remains inside the loop.

double sum = 0.0;for(int i=0; i < 10; ++i){

printf("Enter a n%d: ",i);double nmbr;scanf("%lf" ,&nmbr);if(nmbr < 0.0)

continue;sum += nmbr;}

printf("Sum = %.2lf\n",sum);}

(Lecture 4) Statements October 29, 2019 23 / 27

Page 87: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The continue statementThe continue statement makes it possible to skip part of the loopiteration without jumping out of the loop.break transfers control just past the end of a loop, while continuetransfers control to a point just before the end of the loop body.break - control leaves the loop,continue - control remains inside the loop.

double sum = 0.0;for(int i=0; i < 10; ++i){

printf("Enter a n%d: ",i);double nmbr;scanf("%lf" ,&nmbr);if(nmbr < 0.0)

continue;sum += nmbr;}

printf("Sum = %.2lf\n",sum);}

(Lecture 4) Statements October 29, 2019 23 / 27

Page 88: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The continue statementThe continue statement makes it possible to skip part of the loopiteration without jumping out of the loop.break transfers control just past the end of a loop, while continuetransfers control to a point just before the end of the loop body.break - control leaves the loop,continue - control remains inside the loop.

double sum = 0.0;for(int i=0; i < 10; ++i){

printf("Enter a n%d: ",i);double nmbr;scanf("%lf" ,&nmbr);if(nmbr < 0.0)

continue;sum += nmbr;}

printf("Sum = %.2lf\n",sum);}

(Lecture 4) Statements October 29, 2019 23 / 27

Page 89: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The continue statement

(Lecture 4) Statements October 29, 2019 24 / 27

Page 90: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The goto statement

break and continue are restricted.

The target of a break is a point just beyond the end of enclosing loop.The target of a continue is a point just before the end of loop.

The goto statement is capable of jumping to any statement in afunction, provided that the statement has a label.

A label is an identifier placed at the beginning of a statement

identifier : statement

A statement may have more than one label.

The goto statement has the form:

goto identifier ;

The goto statement is useful for exiting from nested loops.

(Lecture 4) Statements October 29, 2019 25 / 27

Page 91: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The goto statement

break and continue are restricted.The target of a break is a point just beyond the end of enclosing loop.

The target of a continue is a point just before the end of loop.

The goto statement is capable of jumping to any statement in afunction, provided that the statement has a label.

A label is an identifier placed at the beginning of a statement

identifier : statement

A statement may have more than one label.

The goto statement has the form:

goto identifier ;

The goto statement is useful for exiting from nested loops.

(Lecture 4) Statements October 29, 2019 25 / 27

Page 92: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The goto statement

break and continue are restricted.The target of a break is a point just beyond the end of enclosing loop.The target of a continue is a point just before the end of loop.

The goto statement is capable of jumping to any statement in afunction, provided that the statement has a label.

A label is an identifier placed at the beginning of a statement

identifier : statement

A statement may have more than one label.

The goto statement has the form:

goto identifier ;

The goto statement is useful for exiting from nested loops.

(Lecture 4) Statements October 29, 2019 25 / 27

Page 93: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The goto statement

break and continue are restricted.The target of a break is a point just beyond the end of enclosing loop.The target of a continue is a point just before the end of loop.

The goto statement is capable of jumping to any statement in afunction, provided that the statement has a label.

A label is an identifier placed at the beginning of a statement

identifier : statement

A statement may have more than one label.

The goto statement has the form:

goto identifier ;

The goto statement is useful for exiting from nested loops.

(Lecture 4) Statements October 29, 2019 25 / 27

Page 94: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The goto statement

break and continue are restricted.The target of a break is a point just beyond the end of enclosing loop.The target of a continue is a point just before the end of loop.

The goto statement is capable of jumping to any statement in afunction, provided that the statement has a label.

A label is an identifier placed at the beginning of a statement

identifier : statement

A statement may have more than one label.

The goto statement has the form:

goto identifier ;

The goto statement is useful for exiting from nested loops.

(Lecture 4) Statements October 29, 2019 25 / 27

Page 95: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The goto statement

break and continue are restricted.The target of a break is a point just beyond the end of enclosing loop.The target of a continue is a point just before the end of loop.

The goto statement is capable of jumping to any statement in afunction, provided that the statement has a label.

A label is an identifier placed at the beginning of a statement

identifier : statement

A statement may have more than one label.

The goto statement has the form:

goto identifier ;

The goto statement is useful for exiting from nested loops.

(Lecture 4) Statements October 29, 2019 25 / 27

Page 96: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The goto statement

break and continue are restricted.The target of a break is a point just beyond the end of enclosing loop.The target of a continue is a point just before the end of loop.

The goto statement is capable of jumping to any statement in afunction, provided that the statement has a label.

A label is an identifier placed at the beginning of a statement

identifier : statement

A statement may have more than one label.

The goto statement has the form:

goto identifier ;

The goto statement is useful for exiting from nested loops.

(Lecture 4) Statements October 29, 2019 25 / 27

Page 97: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The goto statement

break and continue are restricted.The target of a break is a point just beyond the end of enclosing loop.The target of a continue is a point just before the end of loop.

The goto statement is capable of jumping to any statement in afunction, provided that the statement has a label.

A label is an identifier placed at the beginning of a statement

identifier : statement

A statement may have more than one label.

The goto statement has the form:

goto identifier ;

The goto statement is useful for exiting from nested loops.

(Lecture 4) Statements October 29, 2019 25 / 27

Page 98: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The goto statement

while (...){switch (...){

...goto loop_done; // break won’t work here...

}}

loop_done: ...

(Lecture 4) Statements October 29, 2019 26 / 27

Page 99: Statements - AGH University of Science and Technologygawronski/2019_pp/lecture_04.pdfStatements Statement is terminated by; Statement consisting only of the semicolon is empty statement.

The goto statement

while (f>0){for (i=1;i <101;i++){

for (j=1;j<51;j++){...if (troubles)

goto help;...}...

}...

}help: ...

(Lecture 4) Statements October 29, 2019 27 / 27