Week 3.docx

5 Pages
Unlock Document

Electrical & Computer Engineer
ECE 15
Alon Orlitsky

1. Switch Function a. Executes one or more of several options i. int i; printf(“dame un numero positive: “); scanf(“%d”, &i); switch (i) { case 1: printf(“es uno\n”); break; case 2: printf(“es dos\n”); break; default: printf(“invalid\n”); } 1. The switch program will read the input, and try to match it to a specific case. For example, if the input is 1, the “case 1” is matched and the statement for that case is executed. If no case is matched, it will resort to the default case. 2. The “break” function tells the program to terminate the switch function once a case is matched. Without it, the function will execute all statements after the one that is matched. For example, if the input was two, the function would execute both the “case 2” and the “default” statement. This incidence is called “fall-through.” count25.c b. This function works only on “int” and “char” value types, not double or float. 2. Conditional (? :) a. Format: expression1 ? expression2 : expression 3 i. if expression 1 is: 1. true, then expression 2 is evaluated. 2. false, then expression 3 is evaluated. ii. The conditional can also be used in a “printf” function. For example: 1. printf(n%2 ? “odd” : “even”); 2. printf(“You have %d item%s.”, n, (n == 1) ? “ “ : “s”); b. Its precedence is lower than that of regular operators (=, -, *, /, %, …) c. Multiple conditionals will be associated form right to left. For example: i. c1 ? v1 : c2 ? v2 : v0; becomes c1 ? v1 : (c2 ? v2 : v0); 1. Means that if c1, then v1, otherwise if c2, then v2, otherwise v0. ordinal.c 3. Conditional vs If-Else a. Often interchangeable, but the conditional is more concise. However, conditionals only work with integers and characters. b. “If-Else” function is often more versatile, because it can fit many statements within one function. 4. Loops a. With loops, a code segment can be executed repeatedly b. Each loop execution is an iteration, and will execute as long as the expression is true. c. Three types of loops: d. While i. Format: while(expression) statement; 1. if true, then the statement is executed, and repeats until the expression becomes false. ii. int n, i = 1 printf( “Number of lines: “); scanf(“%d”, &n); while (i <= n) { printf(“%d]n”, i); i++; } 1. This function will ask for a number of lines (n). If the incremented integer (i) is less than or equal to “n”, it will print out the current line number, and then increment the integer (i) by 1. When the integer becomes greater than “n”, then the function stops executing. iii. Ex: Prompt user for a positive integer, repeat while user complies. When input is non-positive, output the average of all previous (positive) numbers.
More Less

Related notes for ECE 15

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.