700 likes | 717 Views
Learn how to specify conditions, work with operators, and utilize various statements for effective control flow in your programs. Explore if-else, switch, for, while, and do statements with examples and rules.
E N D
Outline • How to specify conditions? • Relational, Equality and Logical Operators • Statements • Statements: compound statement and empty statement • Select among alternative actions • The if and if-else statement • The switch statements • The conditional Operator • Achieve iterative actions • The while statement • The for statement • The do statement • The break and continue statements • Nested Flow of Control
//optional The switch Statement • multiway conditional statement • General form: switch ( switch_exp ) { case constant_exp1: statements; break; // optional ... case constant_expn: statements; break; // optional default: statements; break; }
The switch Statement • The effect of a switch: • Evaluate the switch_exp. • Go to the case label having a constant value that matches the value of the switch_exp. • If a match is not found, go to the default label. • If there is no default label, terminate the switch. • Terminate the switch when a break statement is encountered, or by “falling off the end”.
The switch Statement switch ( switch_exp ) { case constant_exp1: statements; break; // optional case constant_exp2 : statements; case constant_exp3 : statements; …../* no break */ case constant_expi : statements; break; …… case constant_expn: statements; break; // optional …… } Next statement;
The switch Statement switch ( switch_exp ) { case constant_exp1: statements; break; // optional …… case constant_expi : statements; break; …… case constant_expn: statements; break; // optional default: statements; break; } Next statement;
The switch Statement switch ( switch_exp ) { case constant_exp1: statements; break; // optional …… case constant_expi : statements; break; …… case constant_expn: statements; break; // optional } Next statement;
The switch Statement • Example #include <stdio.h> int main(void) { int x; x= 3; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); case 4: printf("case 4\n"); case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: break; } return 0; } % gcc switch.c % a.out case 3 case 4 case 5
The switch Statement • Example #include <stdio.h> int main(void) { int x; x= 3; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); break; case 4: printf("case 4\n"); break; case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: break; } return 0; } % gcc switch.c % a.out case 3
The switch Statement • Example #include <stdio.h> int main(void) { int x; x= 3; switch ( x+2 ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); break; case 4: printf("case 4\n"); break; case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: break; } return 0; } % gcc switch.c % a.out case 5
The switch Statement • Rules: • The expression in the parentheses following the keyword switch ( switch_exp) must be of integer type. • The constant expression following the case labels must be • an integer constant • unique
The switch Statement • Example #include <stdio.h> int main(void) { doulbe x; x= 3; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); break; default: break; } return 0; } % gcc switch1.c switch1.c: In function `main': switch1.c:6: error: switch quantity not an integer % The expression in the parentheses following the keyword switch must be of integer type.
The switch Statement • Example #include <stdio.h> int main(void) { int x; x= 3; switch ( x ) { case 1.0: printf("case 1.0\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); break; default: break; } return 0; } % gcc switch2.c switch2.c: In function `main': switch2.c:8: error: case label does not reduce to an integer constant • The constant expression following the case labels must be • an integer constant.
The switch Statement • Example #include <stdio.h> int main(void) { int x; x= 3; switch ( x ) { case 1: printf("case 1\n"); case 1: printf("Another case 1\n"); break; case 2: printf("case 2\n"); break; default: break; } return 0; } % gcc switch3.c switch3.c: In function `main': switch3.c:9: error: duplicate case value switch3.c:8: error: previously used here The constant expression following the case labels must all be unique
The switch Statement • Summary • The switch is a multiway conditional statement. • Evaluate the switch_exp. • Go to the case label having a constant value that matches the value of the switch_exp. • If a match is not found, go to the default label. • If there is no default label, terminate the switch. • Terminate the switch when a break statement is encountered, or by “falling off the end”. • switch_exp must be of integer type • case expressions must be of integer type and must all be unique
Outline • How to specify conditions? • Relational, Equality and Logical Operators • Statements • Statements: compound statement and empty statement • Select among alternative actions • The if and if-else statement • The switch statements • The conditional Operator • Achieve iterative actions • The while statement • The for statement • The do statement • The break and continue statements • Nested Flow of Control
The Conditional Operator ?: • The general form of a conditional expression: • expr1? expr2: expr3 • Semantics: • First, expr1 is evaluated. • If it is nonzero (true), then expr2 is evaluated, and this is the value of the conditional expression as a whole. • If expr1 is zero (false), then expr3 is evaluated, and this is the value of the conditional expression as a whole.
The Conditional Operator ?: • Examples: • expr1? expr2: expr3 • x=(y<z) ? y : z;
The Conditional Operator ?: • Precedence and Associativity • Precedence • Just above the assignment operators • Associativity: • Right to left
The Conditional Operator ?: • Examples: int a=1, b=2; double x=7.07; ) ( ( ) ( ) a == b ? a-1 : b+1 ) ) ( ( ) ( a - b < 0 ? x : a + b ) ( ( ) ) ( a - b > 0 ? x : a + b
The Conditional Operator ?: • Summary: • General form: expr1? expr2: expr3 • Semantics: • First, expr1 is evaluated. • If it is nonzero (true), then expr2 is evaluated, and this is the value of the conditional expression as a whole. • If expr1 is zero (false), then expr3 is evaluated, and this is the value of the conditional expression as a whole. • Precedence: Just above the assignment operators • Associativity: Right to left
Outline • How to specify conditions? • Relational, Equality and Logical Operators • Statements • Statements: compound statement and empty statement • Select among alternative actions • The if and if-else statement • The switch statements • The conditional Operator • Achieve iterative actions • The while statement • The for statement • The do statement • The break and continue statements • Nested Flow of Control
The if and if-else Statement • Summary • exp is enclosed by parentheses • Where appropriate, compound statements should be used to group a series of statements under the control of a single if expression • An if or if-else statement can be used as the statement part of another if or if-else statement. • an else attaches to the nearest if. If (expr) statement1 else statement2 If (expr) statement1
The switch Statement • Summary • The switch is a multiway conditional statement. • Evaluate the switch_exp. • Go to the case label having a constant value that matches the value of the switch_exp. • If a match is not found, go to the default label. • If there is no default label, terminate the switch. • Terminate the switch when a break statement is encountered, or by “falling off the end”. • switch_exp must be of integer type • case expressions must be of integer type and must all be unique
The Conditional Operator ?: • Summary: • General form: expr1? expr2: expr3 • Semantics: • First, expr1 is evaluated. • If it is nonzero (true), then expr2 is evaluated, and this is the value of the conditional expression as a whole. • If expr1 is zero (false), then expr3 is evaluated, and this is the value of the conditional expression as a whole. • Precedence: Just above the assignment operators • Associativity: Right to left
Outline • How to specify conditions? • Relational, Equality and Logical Operators • Statements • Statements: compound statement and empty statement • Select among alternative actions • The if and if-else statement • The switch statements • The conditional Operator • Achieve iterative actions • The while statement • The for statement • The do statement • The break and continue statements • Nested Flow of Control
The while Statement • Generalform while (expr) Statement Next statement • First expr is evaluated. • If expr is nonzero (true), then statement is executed and control is passed back to the beginning of the while loop. • Statement is repeatedly until expr is zero (false) • Then control passes to next statement.
The while Statement #include <stdio.h> int main(void) { int sum=0, i=1; while(i<=3){ sum=sum+i; i=i+1; } printf(“Sum=%d\n”,sum); return 0; } • expression: i<=3 • Relational operator <= • This expression yields • 1(true) or 0(false) • statement: sum=sum+i; i=i+1; A group of statements enclosed between { and }.
exp (i<=3): (1<=3)=true • Statements are executed • sum=sum+i=0+1=1 • i=i+1=1+1=2 • exp (i<=3): (2<=3)=true • Statements are executed • sum=sum+i=1+2=3 • i=i+1=2+1=3 • exp (i<=3): (3<=3)=true • Statements are executed • sum=sum+i=3+3=6 • i=i+1=3+1=4 • exp (i<=3): (4<=3)=false • while statement is done The while Statement sum=0 i=1 #include <stdio.h> int main(void) { int sum=0, i=1; while(i<=3){ sum=sum+i; i=i+1; } printf(“Sum=%d\n”,sum); return 0; } sum=1 i=2 sum=3 i=3 sum=6 i=4
The while statement • The while statement • First expr is evaluated. • If expr is nonzero (true), then statement is executed and control is passed back to the beginning of the while loop. • Statement is repeatedly until expr is zero (false) • Then control passes to next statement. while (expr) statement; next statement
Outline • How to specify conditions? • Relational, Equality and Logical Operators • Statements • Statements: compound statement and empty statement • Select among alternative actions • The if and if-else statement • The switch statements • The conditional Operator • Achieve iterative actions • The while statement • The for statement • The do statement • The break and continue statements • Nested Flow of Control
The for Statement • Generalform • Typically: • expr1 is used to initialize the loop. • expr2 is a logical expression controlling the iteration. • expr3 updates the variables used in expr2. for(expr1; expr2; expr3){ statement } next statement
The for Statement for(expr1; expr2; expr3){ statement } next statement • Semantics: • First expr1 is evaluated. • Then expr2 is evaluated. • If expr2 is nonzero (true), • then statement is executed, • expr3 is evaluated • control passes back to the beginning of the for loop again, except that evaluation of expr1 is skipped. • The process continues until expr2 is zero (false), at which point control passes to next statement.
The for Statement • Semantically equivalent to expr1; while(expr2){ statement expr3; } next statement for(expr1; expr2; expr3){ statement } next statement
The for Statement • for (expr1; expr2; expr3) • statement • Next statement • Example #include <stdio.h> int main(){ int k=14; int j; for (j=2; j<=k; ++j) { if (k % j == 0) printf("%d is a divisor of %d \n", j, k); } return 0; } What is the output? 2 is a divisor of 14 7 is a divisor of 14 14 is a divisor of 14
The for Statement • for (expr1; expr2; expr3) • statement • Next statement • Note 1 • for (expr1; expr2; expr3) • Semicolons are needed • Example: for ( i=0, i<n, i+=3) sum +=i; Incorrect
The for Statement • for (expr1; expr2; expr3) • statement • Next statement • Note 2 • Any of all of the expressions in a for statement can be missing, but the two semicolons must remain. • If expr1 is missing, no initialization step is performed as part of the for loop • When expr2 is mission, the rule is that the test is always true.
The for Statement • for (expr1; expr2; expr3) • statement • Next statement • Example of Note 2: • expr1 is missing • No initialization step is performed as part of the for loop i = 1; sum = 0; for (; i<=10; ++i) sum +=i; What the value of sum after the for loop?
The for Statement • for (expr1; expr2; expr3) • statement • Next statement • Example of Note 2: • expr2 is missing • The test is always true. i = 1; sum = 0; for (; ; ++i) sum +=i; How many iterations? infinity
The for Statement • for (expr1; expr2; expr3) • statement • Next statement • Note 3 • A for statement can be used as the statement part of an if, if-else, while or another for statement for (expr1; expr2; expr3) for(……) for(……) Next statement for (expr1; expr2; expr3) while(……) if …… else…… Next statement
The for Statement • for (expr1; expr2; expr3) • statement • Next statement • Example of note 3 #include <stdio.h> int main(){ int k=14; int j; for (j=2; j<=k; ++j) { if (k % j == 0) printf("%d is a divisor of %d \n", j, k); } return 0; }
The for Statement — Comma Operator • Example • Initialize two variables • sum, i • sum=0; • for (i=1; i<=n; ++i) • sum+=i; Can we put the initialization of these two variables in one expression?
The for Statement — Comma Operator • The Comma Operator • General Form: expr1, expr2 • expr1 is evaluated first, then expr2. • The comma expression as a whole has the values and type of its right operand.
The for Statement — Comma Operator • The Comma Operator • Precedence : lowest • Associativity: left to right • Example • a=0,b=1 • Value of this expression: 1 • Type: int
The for Statement — Comma Operator • The Comma Operator • Comma operator can be used for multiple initialization and multiple processing of indices in for statement • for (sum=0, i=1; i<=n; ++i) • sum+=i; • sum=0; • for (i=1; i<=n; ++i) • sum+=i; • for (sum=0, i=1; i<=n; sum +=i, ++i) • ;
The for Statement — Comma Operator • Example Precedence : lowest Associativity: left to right int i, j, k=3; double x = 3.3 ( ) ( ) ( ) ( ) i = 1 , j = 2 , ++ k + 1 ( ) ) ( ( ) ( ) k != 7 , ++ x * 2.0 + 1
The for Statement • Summary • The for Statement • The Comma Operator: expr1, expr2 • expr1 is evaluated first, then expr2. • The comma expression as a whole has the values and type of its right operand. • for (expr1; expr2; expr3) • statement • Next statement
Outline • How to specify conditions? • Relational, Equality and Logical Operators • Statements • Statements: compound statement and empty statement • Select among alternative actions • The if and if-else statement • The switch statements • The conditional Operator • Achieve iterative actions • The while statement • The for statement • The do statement • The break and continue statements • Nested Flow of Control
The do Statement • Generalform do statement while (expr); Next statement • Typically, expr is a logical expression controlling the iteration.
The do Statement • do • statement • while (expr); • Next statement • Semantics • First statement is executed, and expr is evaluated. • If the value of expr is nonzero (true), then control passes back to the beginning of the do statement, and process repeats itself. • When expr is zero (false), then control passes to next statement