Multiple conditions ◦ If-else if-else if- …. Select from alternativ e values of a variable ◦ switch-case ◦ Values should be constant not expression: i, ◦ Values & Variables should be int or char
Each switch-case can be rewritten by If-else ◦ if-else version of switch-case in the previous slide if(variable == value1)} <statements 1> <statements 2> } else if(variable == value2){ <statements 2> }
If(variable == value1) switch(variable){ { case value1: <statements 1> <statements 1> } break; else if( variable == value2 ) case value2: { <statements 2> <statements 2> break; } default: else{ <statements 3> <statements 3> } }
Get the operation (contains +,-,*,/,\) as a character (variable name: op )! Get two input numbers (variable names: num1, num2 ). Compute and display the expression of: num1 (op) num2 + - * /or\
Header file: <ctype. ype.h> Function Work Of Function isalnum Tests whether a character is alphanumeric or not isalpha Tests whether a character is aplhabetic or not iscntrl Tests whether a character is control or not isdigit Tests whether a character is digit or not isgraph Tests whether a character is grahic or not islower Tests whether a character is lowercase or not isprint Tests whether a character is printable or not ispunct Tests whether a character is punctuation or not isspace Tests whether a character is white space or not isupper Tests whether a character is uppercase or not isxdigit Tests whether a character is hexadecimal or not tolower Converts to lowercase if the character is in uppercase toupper Converts to uppercase if the character is in lowercase
#include <stdio.h> switch(opr){ #include <stdlib.h> case '+': int main(void){ res = opd1 + opd2; int res, opd1, opd2; break; char opr; case '-': printf("Operand1 : "); res = opd1 - opd2; scanf("%d", &opd1); break; printf("Operand2 : "); case '/': scanf("%d", &opd2); res = opd1 / opd2; printf("Operator : "); break; scanf(" %c", &opr);
case '*': res = opd1 * opd2; break; default: printf("Invalid operator \n"); return -1; } printf("%d %c %d = %d\n", opd1, opr, opd2, res); return 0; }
If( (variable == value1) || switch(variable){ (variable == value2) case value1: ){ case value2: <statements 1> <statements 1> } break; else if case value3: (variable == value3) <statements 2> { } <statements 2> }
if-else is more powerful than switch-case switch-case is only for checking the values of a variable and the values must be constant Some if-else cannot be rewritten by switch-case double var1, var2; if(var1 <= 1.1) <statements 1> if(var1 == var2) <statements 2>
bool b; //b = x && y switch (x){ case 0: b = 0; break; case 1: switch(y){ case 0: b = 0; break; case 1: b = 1; break; } break; }
All values used in case should be different switch(i){ //Error case 1: … case 2: … case 1: …
All values must be value, not expression of variables switch(i){ //Error case j: … case 2: … case k+10: …
While Do while For
Example: Write a program that read 3 integer and compute average It is easy. 3 scanf, an addition, a division and, a printf Example: Write a program that read 3000 integer and compute average ?? 3000 scanf !!! Example: Write a program that read n integer and compute average N??? scanf Repetition in algorithms
A loop is a group of instructions the computer executes repeatedly while some loop-continuation condition remains true. We have discussed two means of repetition: Counter-controlled repetition Sentinel-controlled repetition Counter-controlled repetition is sometimes called definite repetition because we know in advance exactly how many times the loop will be executed. Sentinel-controlled repetition is sometimes called indefinite repetition because it’s not known in advance how many times the loop will be executed.
In counter-controlled repetition, a control variable is used to count the number of repetitions. Counter-controlled repetition requires: The initial value of the control variable. The increment (or decrement) by which the control variable is modified each time through the loop. The condition that tests for the final value of the control variable (i.e., whether looping should continue).
When we know the number of iteration Average of 10 number Initialize counter = 0 Initialize other variables While (counter < number of loop repetition) do something (e.g. read input, take sum) counter = counter + 1
Consider the following problem statement: A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz.
Sentinel values are used to control repetition when: The precise number of repetitions is not known in advance, and The loop includes statements that obtain data each time the loop is performed. The sentinel is entered after all regular data items have been supplied to the program. Sentinels must be distinct from regular data items.
When we do NOT know the number of iteration But we know, when loop terminates E.g. Average of arbitrary positive numbers ending with <0 Get first input as n While (n is not sentinel) do something (sum, …) get the next input as n if (there is not any valid input) then S1 else S2
Consider the following problem: Develop a class averaging program that will process an arbitrary number of grades each time the program is run. One way to solve this problem is to use a special value called a sentinel value (also called a signal value, a dummy value, or a flag value ) to indicate “end of data entry.”
The user types in grades until all legitimate grades have been entered. The user then types the sentinel value to indicate that the last grade has been entered. Clearly, the sentinel value must be chosen so that it cannot be confused with an acceptable input value.
Repetition is performed by loops Put all statements to repeat in a loop Don’t loop to infinity Stop the repetition Based on some conditions (counter, sentinel) C has 3 statements for loops while statement do-while statement for statement
A loop tests a condition, and if the condition exists, it performs an action. Then it tests the condition again. If the condition still exists, the action is repeated. This continues until the condition no longer exists YES x < y? Process A
while ( <expression> ) <statements>
<stdio.h> ربراک زا ار n ددع هک دیسیونب یا همانرب n دنک پاچ ار. دادعا و دریگب0 ات int main(void){ int n, number; number = 0; printf("Enter n: "); scanf("%d", &n); while(number <= n){ printf("%d \n", number); number++; } return 0; }
#include <stdio.h> int main(void){ int negative_num, positive_num; int number; negative_num = positive_num = 0; printf("Enter Zero to stop \n"); printf("Enter next number: "); scanf("%d", &number); while(number != 0){ if(number > 0) positive_num++; else negative_num++; printf("Enter next number: "); scanf("%d", &number); } printf("The number of positive numbers = %d\n", positive_num); printf("The number of negative numbers = %d\n", negative_num); return 0; }
Consider a program segment designed to find the first power of 3 larger than 100. When the following while repetition statement finishes executing, product will contain the desired answer: product = 3; while while ( product <= ( product <= 100 ) { ) { product = product = 3 * product; } /* end while */ /* end while */
do <statements> while (<expression>);
#include <stdio.h> int main(void){ int n; double number, sum; printf("Enter n > 0: "); scanf("%d", &n); if(n < 1){printf("wrong input"); return -1;} sum = 0; number = 0.0; do{ number++; sum += number / (number + 1.0); }while(number < n); printf("sum = %f\n", sum); return 0; }
#include <stdio.h> int main(void){ int negative_num=0, positive_num=0; int number; printf("Enter Zero to stop \n"); do{ printf("Enter next number: "); scanf("%d", &number); if(number > 0) positive_num++; else if(number < 0) negative_num++; }while(number != 0); printf("The number of positive numbers = %d\n", positive_num); printf("The number of negative numbers = %d\n", negative_num); return 0; }
for(<expression1>;<expression2>; <expression3>) <statements>
counter ← 1 , sum ← 0 int x, sum, i; false sum = 0; counter < 6 for (i = 1 ; i < 6 ; i++) { true input n scanf (“%d”,&x ); sum ← sum + n sum = sum + x; } counter++ printf (“%d”,sum ); output sum
num Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); _
num 1 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); _
num 1 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); _
num 1 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); 1 _
num 2 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); 1 _
num 2 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); 1 _
num 2 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); 1 2 _
num 3 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); 1 2 _
num 3 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); 1 2 _
num 3 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); 1 2 3 _
num 4 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); 1 2 3 _
num 4 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); 1 2 3 _
4 Example: for (num = 1; num <= 3; num++ ) printf (“%d \ t”, num); printf (“have come to exit \ n”); 1 2 3 have come to exit_
int grade, count, i; double average, sum; sum = 0; printf("Enter the number of students: "); scanf("%d", &count); for(i = 0; i < count; i++){ ", (i + 1)); printf("Enter the grade of %d-th student: scanf("%d", &grade); sum += grade; } average = sum / count; printf("The average of your class is %0.3f\n", average); return 0; }
int n, number; printf("Enter n: "); scanf("%d", &n); <= n; number++) for(number = 1; number 0) if((number % 2) == printf("%d \n", number); return 0; }
int n, number; printf("Enter n: "); scanf("%d", &n); for(number = 2; number <= n; number += 2) printf("%d \n", number); return 0; }
The following examples show methods of varying the control variable in a for statement. Vary the control variable from 1 to 100 in increments of 1 . for for ( i = ( i = 1; i <= 100 100; i++ ) Vary the control variable from 100 to 1 in increments of -1 (decrements of 1 ). for for ( i = ( i = 100 100; i >= 1; i-- -- ) Vary the control variable from 7 to 77 in steps of 7 . for for ( i = ( i = 7; i <= 77 77; i += 7 ) Vary the control variable from 20 to 2 in steps of -2 . for for ( i = ( i = 20 20; i >= 2; i -= = 2 ) Vary the control variable over the following sequence of values: 2 , 5 , 8 , 11 , 14 , 17 . for for ( j = ( j = 2; j <= 17 17; j += 3 ) Vary the control variable over the following sequence of values: 44 , 33 , 22 , 11 , 0 . for for ( j = ( j = 44 44; j >= 0; j -= = 11 11 )
Expression1 and Expression3 can be any number of expressions for(i = 0, j = 0; i < 10; i++, j--) Expression2 at most should be a single expression for(i = 0, j = 0; i < 10, j > -100; i++, j--) //ERROR Any expression can be empty expression for(;i<10;t++) for(;;)//infinite loop
The three expressions in the for statement are optional. for(;;) One may omit expression1 if the control variable is initialized elsewhere in the program. If expression2 is omitted, C assumes that the condition is true, thus creating an infinite loop. expression3 may be omitted if the increment is calculated by statements in the body of the for statement or if no increment is needed.
<statement> in loops can be empty while(<expression>) ; E.g., while(i++ <= n) ; for(<expression1>; <expression2>;<expression3>); E.g., for(i = 0; i < 10; printf("%d\n",i), i++) ;
<statement> in loops can be loop itself while(<expression0>) for(<expression1>; <expression2>;<expression3>) <statements> for(<expression1>; <expression2>;<expression3>) do <statements> while(<expression>);
A program that takes n and m and prints *** ….* (m * in each line) *** ….* … *** ….* (n lines)
Recommend
More recommend