statements - agh university of science and technologygawronski/2019_pp/lecture_04.pdfstatements...
TRANSCRIPT
Statements
Lecture 4
October 29, 2019
(Lecture 4) Statements October 29, 2019 1 / 27
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Selection statement - switch
(Lecture 4) Statements October 29, 2019 6 / 27
Selection statement - switch
(Lecture 4) Statements October 29, 2019 7 / 27
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
The break statement - exiting from a loop
(Lecture 4) Statements October 29, 2019 21 / 27
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
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
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
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
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
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
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
The continue statement
(Lecture 4) Statements October 29, 2019 24 / 27
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
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
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
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
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
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
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
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
The goto statement
while (...){switch (...){
...goto loop_done; // break won’t work here...
}}
loop_done: ...
(Lecture 4) Statements October 29, 2019 26 / 27
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