Dr. R. Z. Khan, Associate Professor, Department of Computer Science

Size: px
Start display at page:

Download "Dr. R. Z. Khan, Associate Professor, Department of Computer Science"

Transcription

1 ALIGARH MUSLIM UNIVERSITY Department of Computer Science Course: CSM-102: Programming & Problem Solving Using C Academic Session UNIT-2: Handout-3 Topic: Control Structures (Selection & Repetition) Dr. R. Z. Khan, Associate Professor, Department of Computer Science Objectives: To Know syntax and use of if, if else, Nested if, if else if else, switch selection statements. To learn about relational and logical operators of C language and their use. To learn about order of execution of relational and logical operators with others operators. To learn about use of increment operator ++, and decrement operator --. To learn about syntax of wile, do-while, for statements (loops). To know working of above loops with examples. To learn how to use above concepts in problem solving. Selection Structures: One-Way Selection using the if-statement: Syntax: if (expression) Statement; If the expression is true, then the statement is executed; otherwise the statement is ignored. For example, let marks be an integer variable. To check the value of marks to see if it is more than 75 and print PASS if it is more than 75 we would write if (marks > 75) printf ( PASS\n ) ; If marks is less than or equal to 75 then the print statement is ignored and nothing is printed. 1

2 If suppose that in addition to printing PASS we would like to add 5 bonus marks then we would write if (marks > 75) marks = marks + 5 ; printf ( PASS\n ) ; As seen above if there is more than one statement (compound statement) to be done after checking the expression result, then the statements should be enclosed between parenthesis. If there is only one statement (simple statement) to be executed after if then the parenthesis becomes optional (you can remove it). Note that there is no semicolon at the end of the if or else statement. Two-Way Selection Using the if else statement: Syntax: if (expression) statement 1; else statement 2; If the expression is true, then the statement 1 is executed; otherwise the statement 2 is executed if expression is false. For example, to find the larger of two variables x and y and print the answer and assign the larger value to max variable and smaller value to min variable, we would write : if ( x > y ) max = x ; min = y ; printf ( x is BIG\n ) ; else max = y ; min = x ; printf ( y is BIG\n ) ; Check for the parenthesis used for more than one statement after if and else. 2

3 Relational and Logical operators: The most commonly used relational operators are: > for greater than >= for greater than or equal to < for less than <= for less than or equal to == for equal to!= for not equal to The most commonly used logical operators are: && for binary AND for binary OR! for binary NOT These operators are used in the expression of if statement. The expression having these operators is called Logical expression. Examples: To check if the value of the integer variable x is more than 5 and less than 10 or equal to 15, we write if ( x > 5 && x < 10 x == 15 ) To check if the value of the float variable a is not equal to 1.5 or less than or equal to 2.5 but more than or equal to 1.5, we write if ( a!= 1.5 a <= 2.5 && a >= 1.5 ) 3

4 Multiple Alternative Decisions using if ladder: Syntax: if ( expression 1 ) statement 1; else if ( expression 2 ) statement 2;.. else if ( expression n ) statement n; else statement e; The expressions in a multiple-alternative decision are evaluated in sequence until a true condition is reached. If an expression is true, the statement following it is executed, and the rest of the multiple-alternative decision is skipped. If an expression is false, the statement following it is skipped, and the next expression is checked. If all the expressions are false, the statement e following the final else is executed. For example, to check the value of the variable marks and print the message accordingly, we would write if (marks >= 75) printf ( Distinction\n ) ; else if (marks >= 60) printf ( Pass\n ) ; else if (marks >= 50) printf ( Average\n ) ; else printf ( Fail\n ) ; Nested if statements: There can be if statements inside if and else statements which can be used to implement decisions with several alternatives. For example, if ( x > 2.5 ) p = p + 1 ; if ( x < 3.5 ) n = n + 1; else 4

5 else m = m + 1; if ( x > 1.5 ) s = s + 1 ; else t = t + 1; In the above example, if x is greater than 2.5 then 1 is added to p and it is checked if x is less than 3.5. If it is so, then 1 is added to n. If x is more than 3.5, 1 is added to m. If x is less than 2.5, the execution comes directly to the else part without doing anything to p, n, m and it is checked if x is more than 1.5. If it is so 1 is added to s else 1 is added to t. Multiple selections using the switch statement: Syntax: switch (expression) case value 1: statement 1; break ; case value 2: statement 2; break ; : : case value n: statement n; break ; default: statement e; Here it is checked whether the value of expression matches any of the case values. For example, to check the value of the character variable color and print the message accordingly, we write switch (color) case R : printf ( red\n ) ; break ; 5

6 case B : printf ( blue\n ) ; break ; case Y : printf ( yellow\n ) ; break ; default: printf ( black\n ) ; break ; The single quotes R, B, Y are used because of the color being character variable. If color is R then red is printed and the other cases are not checked, however if color is not red then other cases are checked until a match is found. If no match is found then the default value of black is printed. Operator Precedence: Operator Precedence! (not), +, -, & (address of) Ist (i.e highest) Note Above are Unary Operators which have a single operand. *, /, % IInd +, _ IIIrd <, <=, >=, > IVth == (equality operator),!= (not equal) Vth && (logical and operator ) VIth (logical OR operator ) VIIth = (assignment operator) VIIIth (i.e Lowest) 6

7 UExamples:U Use of Control Structures (Selection Structures): Example#1: /**************************************************************** Write a program that calculates and displays the reciprocal of an integer, both as a common fraction and a decimal fraction. A typical output line would be: The reciprocal of 2 is 1/2 or The program should display a Reciprocal undefined message for an input of zero. ****************************************************************/ void main () int number; float reciprocal; printf("please Enter an integer number: "); scanf("%d",&number); if (number>0 number<0) reciprocal = 1.0/number; printf("\nthe reciprocal of %d is 1/%d or %0.3f",number,number,reciprocal); else printf("\nreciprocal undefined"); // end of main 7

8 Example#2: /**************************************************************** Write an interactive program that contains a compound if statement and that may be used to compute the area of a square (area=side 2 ) or a triangle (area=base*height/2) after prompting the user to type the first character of the figure name (S or T). ***************************************************************/ using If-statement. void main () int side, height, base, area; char D; printf("if you want area of a square press S or T for area of triangle :"); scanf("%c", &D); if (D=='S') printf("please input the side : "); scanf("%d", &side); area=side*side; printf("the area : %d\n", area); else if (D=='T') printf("enter base and height of the triangle :"); scanf("%d %d", &base, &height); area=(0.5)*base*height; printf("the area of triangle is : %d", area); else printf("dear!, Your choice must be either S or T only "); printf("\nplease try again!"); // end of main 8

9 Using Switch statement: void main( ) char choice; double side, height, base, area; printf("[to find the shape's area choose either 'S' for square, or 'T' for triangle] : "); printf("\nenter your choice to find the area : "); scanf("%c", &choice); switch(choice) case 'S': printf("\nenter its side: "); scanf("%lf", &side); area = (side * side); printf("the area of square is %.2f unit square\n", area); break; case 'T' : printf("\nenter the height and base: "); scanf("%lf %lf", &height, &base); area = (0.5) * height * base; printf(" The area of triangle is %.2f unit square\n", area); break; default: printf("sorry,your choice is not included \nplz try again "); // end of switch // end of main Use of increment operator ++, and decrement operator - - : i++ means i=i+1; i -- means i=i-1; (For above statements in first step initial value of i will be assigned and after that value of i will be incremented or decremented). ++i means i=i+1 ; -- i means i=i-1 ; (But in these, in first step initial value of i will be incremented or decremented and then value of i will be assigned ). 9

10 Examples: main() int i=2, r1 ; r1=i++; printf("value of r1 is : %d", r1); Sample Output: void main() int i=2, r1 ; r1 = ++i; printf("value of r1 is : %d", r1); Sample Output: 10

11 Looping Structures: While statement: Syntax: while (expression) statement A conditional loop is one whose continued execution depends on the value of a logical expression. In the above syntax as long as the expression is true the statement (or statements if enclosed within braces) is executed. When the expression evaluates to false the execution of the statement(s) stops and program continues with the other following statements. Example: Let number be an integer variable. while (number > 100) printf ( Enter a number\n ) ; scanf ( %d, &number) ; In the above example, as long as the entered value of number is greater than 100 the loop continues to ask to enter a value for number. Once a number less than or equal to 100 is entered it stops asking. If in the beginning itself the value of number is less than or equal to 100, then the loop is not at all executed. Note: In the while loop the expression is tested before the loop body statements are entered. So, if the expression is false in the beginning the loop body is never entered. do-while statement : Syntax: do Statement; while (expression) ; Here, the expression is tested after the statement(s) are executed. So, even if the expression is false in the beginning the loop body is entered at least once. Here also like the while statement, as long as the expression is true the statement (or statements if enclosed within braces) is executed. When the expression evaluates to false the execution of the statement(s) stops and program continues with the other following statements. 11

12 Example: The example of while loop can be written with do while loop as : do printf ( Enter a number\n ) ; scanf ( %d, &number) ; while (number > 100) ; In the above example, as long as the entered value of number is greater than 100 the loop continues to ask to enter a value for number. Once a number less than or equal to 100 is entered it stops asking. Note: In the conditional looping using while and do while the number of times the loop will be executed is not known at the start of the program. For-statement: Syntax: for (expression 1; expression 2; expression 3) Statement; In the iterative looping using for loop the number of times the loop will be executed is known at the start of the program. In the above syntax, the expression 1 is the initialization statement which assigns an initial value to the loop variable. The expression 2 is the testing expression where the loop variable value is checked. If it is true the loop body statement(s) are executed and if it is false the statements(s) are not executed. The expression 3 is usually an increment or decrement expression where the value of the loop variable is incremented or decremented. Example: for (count = 0 ; count < 10 ; count++) printf ( Enter a value\n ) ; scanf ( %d, &num) ; num = num + 10 ; printf ( NUM = %d\n, num) ; 12

13 In the above example the integer variable count is first assigned a value of 0, it is initialized. Then the value of count is checked if it is less than 10. If it is < 10 the loop body asks to enter a value for num. Then the value of count is incremented by 1 and again count is checked for less than 10. If it is true the loop body is again executed and then the count is incremented by 1 and checked for < 10. It keeps on doing like this until the count is >= 10. Then the loop stops. So, here the loop is executed 10 times. Note: The increment operation is written as ++ and decrement operation is written as --. The increment increases the value by 1 and decrement decreases the value by 1. How to generate number from 1 to 10 Using different loops: for while do-while for(n=1; n<=10; ++n) n=1; n=1; while(n<=10) do n=n+1; n=n+1; while(n<=10); Examples using different loops: Example#1: for-loop : void main() int num,square,limit; printf("please Input value of limit : "); scanf("%d", &limit); for (num=0; num < limit; ++num) square = num*num; printf("%5d %5d\n", num, square); // end of for // end of main 13

14 Sample Output: Example#2: /********************************************************** Program Showing a Sentinel-Controlled for loop. To compute the sum of a list of exam scores. ***********************************************************/ #include <stdio.h> #define SENTINEL -99 int main() int sum = 0, /* sum of scores input so far */ score; /* current score */ printf("enter first score (or %d to quit)> ", SENTINEL); for(scanf("%d", &score); score!= SENTINEL; scanf("%d", &score)) sum += score; printf("enter next score (%d to quit)> ", SENTINEL); // end of for loop printf("\nsum of exam scores is %d\n", sum); return (0); // end of main Sample Output: 14

15 Example#3: while-loop : /*Conditional loop Using while Statement */ #define RIGHT_SIZE 10 void main() int diameter; printf("please Input Balloon's diameter > "); scanf("%d",&diameter); while(diameter < RIGHT_SIZE) printf("keep blowing! \n"); printf("please Input new Balloon's diameter > "); scanf("%d", &diameter); // end of while loop printf("stop blowing! \n"); // end of main Sample Output: Example#4: do-while loop : /* do-while loop */ void main() 15

16 int num; do printf("please Input Positive numbers between ( < 1 or >= 10 ) : "); scanf("%d",&num); // end of do loop while (num < 1 num >=10) ; printf("dear, Sorry! Your number is out of specified range in program"); // end of main Sample Output: Example#5: Use of Increment and Decrement operators: /* Explanation on Increment, Decrement operators */ void main() int n, m, p, i = 3, j = 9; n = ++i * --j; printf("from above First given expression calculated n=%d i=%d j=%d\n", n, i, j); m = i + j--; printf("from above second given expression calculated m=%d i=%d j=%d\n",m,i,j); p = i + j; printf("from above Third given expression calculated p=%d i=%d j=%d\n", p, i, j); // end of main 16

17 Sample Output: Exercises Problem#1: Trace output of following program manually (Without using Computer): main( ) int j=0, x=0; switch(j-1) case 0 : case -1 : x += 1; break; case 1: case 2: case 3: x += 2; break; default: x += 3; // end of switch printf("x = %d\n", x); // end of main Problem#2: Write a program that reads three integer numbers and prints the highest among them. Problem#3: Write a program to find the roots of a quadratic equation. Consider the case of imaginary roots. 17

18 Problem#4: Write a program that prompts the user for the day number (0 to 6) and prints the day name (Saturday to Friday) using the switch statement. Problem#5: An electric power distribution company charges its domestic consumers as follows: Consumption Units Rate of Charge Rs per unit Rs. 100 plus Rs per unit excess of Rs. 230 plus Rs per unit excess of and above Rs. 390 plus Rs per unit excess of 600 Write C Program to read the customer number and power consumed and prints the amount to be paid by the customer. Problem#6: Write C-Program to read exam score of a student of AMU, Aligarh from keyboard and calculate latter grades based on following grading scheme: Score Grade >= 90 A >= 85 B+ >= 80 B >= 75 C+ >= 70 C >= 65 D+ >= 60 D <60 F 18

19 Problem#7: Write a program that reads a set of grades (between 0 to 100) and finds the following: a. Sum b. Average (Note: Solve using do-while). Problem#8: Write a program to generate Square, Cube, Square_Root of positive numbers from 1 to any Limit Entered by user. (Note: Solve using for loop). Problem#9: Write a program that reads the time and duration of a phone call in minutes and calculates the cost. Each minute costs 1.5 Rs. A discount of 20% is given if the time of the call is between 22:00 1:00 and a discount of 40% is given if the time of the call is between 1:00 8:00. Check the correctness of the time (0:00-23:59) and duration. Problem#10: There are 9870 people in a town of Farrukhabad District whose population is increases by 10% each year. Write a program that contents a loop that display the annual (yearly) population and determine how many years (count_year) it will take for the population just above 30,000 people. Problem#11: Write a program that reads the values of the variables a, b, and d then finds and prints the value of x using the following formula: a x = 5 + 3d b Problem#12: Find output of the following programs: 1). int main() 19

20 int x,i=1; for(x=-1; x<=10; x++) i++; if(x < 5) continue; else break; printf("khan"); printf( i=%d\n,i); return 0; 2). int main() int j=1; while(j <= 255) printf("%d\n", j); j++; return 0; 3). int main() int i=0; for(; i<=5; i++); printf("%d,", i); return 0; 4). int main() char str[]="c-program"; int a = 5; printf(a >10?"Ps\n":"%s\n", str); return 0; 20

21 5). int main() unsigned int i = 65535; /* Assume 2 byte integer*/ while(i++!= 0) printf("%d",++i); printf("\n"); return 0; 6). What will be the output of the program, if a short int is 2 bytes wide? int main() short int i = 0; for(i<=5 && i>=-1; ++i; i>0) printf("%u,", i); return 0; 7). int main() unsigned int i = 65536; /* Assume 2 byte integer*/ while(i!= 0) printf("%d",++i); printf("\n"); return 0; 8). 21

22 int main() int x=1, y=1; for(; y; printf("%d %d\n", x, y)) y = x++ <= 5; printf("\n"); return 0; 9). int main() int i = 5; while(i-- >= 0) printf("%d,", i); i = 5; printf("\n"); while(i-- >= 0) printf("%i,", i); while(i-- >= 0) printf("%d,", i); return 0; 10). int main() int i=3; switch(i) case 1: printf("hello\n"); case 2: printf("hi\n"); case 3: continue; default: printf("bye\n"); return 0; 22

23 11). int main() char j=1; while(j < 5) printf("%d, ", j); j = j+1; printf("\n"); return 0; 12). int main() int x, y, z; x=y=z=1; z = ++x ++y && ++z; printf("x=%d, y=%d, z=%d\n", x, y, z); return 0; 13). #include <stdio.h> int main(void) int x = 2; switch (x) case 1: printf( one\n ); case 2: printf( two\n ); case 3: printf( three\n ); return (0); 23

24 14). #include <stdio.h> int main (void) int x; for ( x = 1; x <= 3; x++) printf("amu\n"); printf("2012 1\n"); return (0); 15). #include <stdio.h> int main (void) int x; x = 30; if (x < 0 x > 15) printf("wrong\n"); else if (x < 10) printf("fail\n"); else printf("pass\n"); return (0); 16). void main() int i=4,j=-1, k=0, w, x, y, z; w = i j k; x = i && j && k; y = i j && k; z = i && j k; printf("\nw=%d x=%d y=%d z=%d",w, x, y,!z); 24

25 17). void main() int i, x=2; for(i=1;i<5;++i) if(i%2 == 1) x +=i; else x--; printf("x= %d\n",x);; printf("value of x is = %d\n", x); Answers: 1). i=8 2) ). 6 Explanation: Step 1: int i = 0; here variable i is an integer type and initialized to '0'. Step 2: for(; i<=5; i++); variable i=0 is already assigned in previous step. The semicolon at the end of this for loop tells, "there is no more statement is inside the loop". 25

26 Loop 1: here i=0, the condition in for(; 0<=5; i++) loop satisfies and then i is incremented by '1'(one) Loop 2: here i=1, the condition in for(; 1<=5; i++) loop satisfies and then i is incremented by '1'(one) Loop 3: here i=2, the condition in for(; 2<=5; i++) loop satisfies and then i is incremented by '1'(one) Loop 4: here i=3, the condition in for(; 3<=5; i++) loop satisfies and then i is increemented by '1'(one) Loop 5: here i=4, the condition in for(; 4<=5; i++) loop satisfies and then i is incremented by '1'(one) Loop 6: here i=5, the condition in for(; 5<=5; i++) loop satisfies and then i is incremented by '1'(one) Loop 7: here i=6, the condition in for(; 6<=5; i++) loop fails and then i is not incremented. Step 3: printf("%d,", i); here the value of i is 6. Hence the output is '6'. 4). C-program Explanation: Step 1: char str[]="c-program"; here variable str contains "C-program". Step 2: int a = 5; here variable a contains "5". Step 3: printf(a >10?"Ps\n":"%s\n", str); this statement can be written as if(a > 10) printf("ps\n"); else printf("%s\n", str); Here we are checking a > 10 means 5 > 10. Hence this condition will be failed. So it prints variable str. Hence the output is "C-program". 5). Infinite loop Explanation: 26

27 Here unsigned int size is 2 bytes. It varies from 0,1,2,3,... to Step 1:unsigned int i = 65535; Step 2: Loop 1: while(i++!= 0) this statement becomes while(65535!= 0). Hence the while(true) condition is satisfied. Then the printf("%d", ++i); prints '1'(variable 'i' is already increemented by '1' in while statement and now increemented by '1' in printf statement) Loop 2: while(i++!= 0) this statement becomes while(1!= 0). Hence the while(true) condition is satisfied. Then the printf("%d", ++i); prints '3'(variable 'i' is already increemented by '1' in while statement and now increemented by '1' in printf statement) The while loop will never stops executing, because variable i will never become '0'(zero). Hence it is an 'Infinite loop'. 6) Explanation: for(i<=5 && i>=-1; ++i; i>0) so expression i<=5 && i>=-1 initializes for loop. expression ++i is the loop condition. expression i>0 is the increment expression. In for( i <= 5 && i >= -1; ++i; i>0) expression i<=5 && i>=-1 evaluates to one. Loop condition always get evaluated to true. Also at this point it increases i by one. An increment_expression i>0 has no effect on value of i.so for loop get executed till the limit of integer (ie ) 7. No output. Explanation: Here unsigned int size is 2 bytes. It varies from 0,1,2,3,... to Step 1:unsigned int i = 65536; here variable i becomes '0'(zero). because unsigned int varies from 0 to Step 2: while(i!= 0) this statement becomes while(0!= 0). Hence the while(false) condition is not satisfied. So, the inside the statements of while loop will not get executed. 27

28 Hence there is no output. Note: Don't forget that the size of int should be 2 bytes. If you run the above program in GCC it may run infinite loop, because in Linux platform the size of the integer is 4 bytes. 8) ). 4, 3, 2, 1, 0, -1 4, 3, 2, 1, 0, -1 Explanation: Step 1: Initially the value of variable i is '5'. Loop 1: while(i-- >= 0) here i = 5, this statement becomes while(5-- >= 0) Hence the while condition is satisfied and it prints '4'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 2: while(i-- >= 0) here i = 4, this statement becomes while(4-- >= 0) Hence the while condition is satisfied and it prints '3'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 3: while(i-- >= 0) here i = 3, this statement becomes while(3-- >= 0) Hence the while condition is satisfied and it prints '2'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 4: while(i-- >= 0) here i = 2, this statement becomes while(2-- >= 0) Hence the while condition is satisfied and it prints '1'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 5: while(i-- >= 0) here i = 1, this statement becomes while(1-- >= 0) Hence the while condition is satisfied and it prints '0'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 6: while(i-- >= 0) here i = 0, this statement becomes while(0-- >= 0) Hence the while condition is satisfied and it prints '-1'. (variable 'i' is decremented by '1'(one) in previous while condition) Loop 7: while(i-- >= 0) here i = -1, this statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits. The output of first while loop is 4,3,2,1,0,-1 28

29 Step 2: Then the value of variable i is initialized to '5' Then it prints a new line character(\n). See the above Loop 1 to Loop 7. The output of second while loop is 4,3,2,1,0,-1 Step 3: The third while loop, while(i-- >= 0) here i = -1(because the variable 'i' is decremented to '-1' by previous while loop and it never initialized.). This statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits. Hence the output of the program is 4,3,2,1,0,-1 4,3,2,1,0,-1 10). Error: Misplaced continue Explanation: The keyword continue cannot be used in switch case. It must be used in for or while or do while loop. If there is any looping statement in switch case then we can use continue. 11). 1, 2, 3, 4 12). x=2, y=1, z=1 Explanation: Step 1: x=y=z=1; here the variables x,y, z are initialized to value '1'. Step 2: z = ++x ++y && ++z; becomes z = ( (++x) (++y && ++z) ). Here ++x becomes 2. So there is no need to check the other side because (Logical OR) condition is satisfied.(z = (2 ++y && ++z)). There is no need to process ++y && ++z. Hence it returns '1'. So the value of variable z is '1' Step 3: printf("x=%d, y=%d, z=%d\n", x, y, z); It prints "x=2, y=1, z=1". here x is increemented in previous step. y and z are not increemented. 13). one two 29

30 three 14). AMU AMU AMU ). wrong 16). w=1 x=0 y=1 z=0 17). x= 3 x= 2 x= 5 x= 4 Value of x is = 4 30

Chapter 5: Control Structures

Chapter 5: Control Structures Chapter 5: Control Structures In this chapter you will learn about: Sequential structure Selection structure if if else switch Repetition Structure while do while for Continue and break statements S1 2017/18

More information

Introduction. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

More information

DECISION CONTROL AND LOOPING STATEMENTS

DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL STATEMENTS Decision control statements are used to alter the flow of a sequence of instructions. These statements help to jump from one part of

More information

Module 4: Decision-making and forming loops

Module 4: Decision-making and forming loops 1 Module 4: Decision-making and forming loops 1. Introduction 2. Decision making 2.1. Simple if statement 2.2. The if else Statement 2.3. Nested if Statement 3. The switch case 4. Forming loops 4.1. The

More information

Prepared by: Shraddha Modi

Prepared by: Shraddha Modi Prepared by: Shraddha Modi Introduction In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. A program loop consist of two segments Body of

More information

Subject: PIC Chapter 2.

Subject: PIC Chapter 2. 02 Decision making 2.1 Decision making and branching if statement (if, if-, -if ladder, nested if-) Switch case statement, break statement. (14M) 2.2 Decision making and looping while, do, do-while statements

More information

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions. Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

More information

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION INTRODUCTION Decision Making and Looping Unit 5 In the previous lessons we have learned about the programming structure, decision making procedure, how to write statements, as well as different types of

More information

Lecture 3. More About C

Lecture 3. More About C Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants

More information

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); } C Program Output #include main () { printf ( Hi everyone\n ); Hi everyone #include main () { printf ( Hi everyone\n ); #include and main are Keywords (or Reserved Words) Reserved Words

More information

Decision Making and Loops

Decision Making and Loops Decision Making and Loops Goals of this section Continue looking at decision structures - switch control structures -if-else-if control structures Introduce looping -while loop -do-while loop -simple for

More information

Lecture 6. Statements

Lecture 6. Statements Lecture 6 Statements 1 Statements This chapter introduces the various forms of C++ statements for composing programs You will learn about Expressions Composed instructions Decision instructions Loop instructions

More information

"'"' .;, b) F or ( c = l ; c <= 10,"' c++ ) X { JJ. ~. Computer Programming I COME Midterm Examination. Fall November 2016

'' .;, b) F or ( c = l ; c <= 10,' c++ ) X { JJ. ~. Computer Programming I COME Midterm Examination. Fall November 2016 Group B ~. Computer Programming I COME 103 - Midterm Examination B Fall 2016 21 November 2016 1. Identify and correct the errors in each of the following statements. (Note: There may be more than one error.)

More information

SELECTION STATEMENTS:

SELECTION STATEMENTS: UNIT-2 STATEMENTS A statement is a part of your program that can be executed. That is, a statement specifies an action. Statements generally contain expressions and end with a semicolon. Statements that

More information

Government Polytechnic Muzaffarpur.

Government Polytechnic Muzaffarpur. Government Polytechnic Muzaffarpur. Name of the Lab: COMPUTER PROGRAMMING LAB (MECH. ENGG. GROUP) Subject Code: 1625408 Experiment: 1 Aim: Programming exercise on executing a C program. If you are looking

More information

UIC. C Programming Primer. Bharathidasan University

UIC. C Programming Primer. Bharathidasan University C Programming Primer UIC C Programming Primer Bharathidasan University Contents Getting Started 02 Basic Concepts. 02 Variables, Data types and Constants...03 Control Statements and Loops 05 Expressions

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1 CSE123 LECTURE 3-1 Program Design and Control Structures Repetitions (Loops) 1-1 The Essentials of Repetition Loop Group of instructions computer executes repeatedly while some condition remains true Counter-controlled

More information

Programming I Assignment 04. Branching I. # Student ID Student Name Grade (10) -

Programming I Assignment 04. Branching I. # Student ID Student Name Grade (10) - Programming I Assignment 04 Branching I # Student ID Student Name Grade (10) - Delivery Date 1. يتم تسليم التمرين محلوال في خالل أسبوعا من تاريخ التمرين و يتم حذف درجتين من التمرين عن كل أسبوع تأخير 2.

More information

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

n Group of statements that are executed repeatedly while some condition remains true

n Group of statements that are executed repeatedly while some condition remains true Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,

More information

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action. 3.0 Control Statements in C Statements The statements of a C program control the flow of program execution. A statement is a command given to the computer that instructs the computer to take a specific

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 08: Control Statements Readings: Chapter 6 Control Statements and Their Types A control

More information

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26 Day06 A Young W. Lim 2017-09-20 Wed Young W. Lim Day06 A 2017-09-20 Wed 1 / 26 Outline 1 Based on 2 C Program Control Overview for, while, do... while break and continue Relational and Logical Operators

More information

Structured programming. Exercises 3

Structured programming. Exercises 3 Exercises 3 Table of Contents 1. Reminder from lectures...................................................... 1 1.1. Relational operators..................................................... 1 1.2. Logical

More information

CS16 Exam #1 7/17/ Minutes 100 Points total

CS16 Exam #1 7/17/ Minutes 100 Points total CS16 Exam #1 7/17/2012 75 Minutes 100 Points total Name: 1. (10 pts) Write the definition of a C function that takes two integers `a` and `b` as input parameters. The function returns an integer holding

More information

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function

More information

C Programming Basics

C Programming Basics CSE 2421: Systems I Low-Level Programming and Computer Organization C Programming Basics Presentation B Read/Study: Reek 3.1-3.4, 4, and 5, Bryant 2.1.1-2.1.5 Gojko Babić 08/29/2017 C Programming Language

More information

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14 Day05 A Young W. Lim 2017-10-07 Sat Young W. Lim Day05 A 2017-10-07 Sat 1 / 14 Outline 1 Based on 2 Structured Programming (2) Conditions and Loops Conditional Statements Loop Statements Type Cast Young

More information

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps 1 True and False in C False is represented by any zero value The int expression having the

More information

COMP 208 Computers in Engineering

COMP 208 Computers in Engineering COMP 208 Computers in Engineering Lecture 14 Jun Wang School of Computer Science McGill University Fall 2007 COMP 208 - Lecture 14 1 Review: basics of C C is case sensitive 2 types of comments: /* */,

More information

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution 1. (20 points, 5 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling

More information

Programming Language A

Programming Language A Programming Language A Takako Nemoto (JAIST) 22 October Takako Nemoto (JAIST) 22 October 1 / 28 From Homework 2 Homework 2 1 Write a program calculate something with at least two integer-valued inputs,

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved. Chapter 4: Expressions Chapter 4 Expressions 1 Chapter 4: Expressions Operators Expressions are built from variables, constants, and operators. C has a rich collection of operators, including arithmetic

More information

MODULE 2: Branching and Looping

MODULE 2: Branching and Looping MODULE 2: Branching and Looping I. Statements in C are of following types: 1. Simple statements: Statements that ends with semicolon 2. Compound statements: are also called as block. Statements written

More information

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 4: Flow of Control Outline of this Lecture Examples of Statements in C Conditional Statements The if-else Conditional Statement

More information

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value. 3. EXPRESSIONS It is a sequence of operands and operators that reduce to a single value. Operator : It is a symbolic token that represents an action to be taken. Ex: * is an multiplication operator. Operand:

More information

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Topics

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

ECET 264 C Programming Language with Applications. C Program Control

ECET 264 C Programming Language with Applications. C Program Control ECET 264 C Programming Language with Applications Lecture 7 C Program Control Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 7 - Paul I. Lin

More information

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities SOLUTION OF CONTINUOUS INTERNAL EVALUATION TEST -1 Date : 27-02 2018 Marks:60 Subject & Code : Programming in C and Data Structures- 17PCD23 Name of faculty : Dr. J Surya Prasad/Mr. Naushad Basha Saudagar

More information

Chapter 2. Introduction to C language. Together Towards A Green Environment

Chapter 2. Introduction to C language. Together Towards A Green Environment Chapter 2 Introduction to C language Aim Aim of this chapter is to provide the fundamentals for basic program construction using C. Objectives To understand the basic information such as keywords, character

More information

Decision Making and Branching

Decision Making and Branching INTRODUCTION Decision Making and Branching Unit 4 In the previous lessons we have learned about the programming structure, data types, declaration of variables, tokens, constants, keywords and operators

More information

C Programming Class I

C Programming Class I C Programming Class I Generation of C Language Introduction to C 1. In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming Language) 2. In 1970, Ken Thompson created a language

More information

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10 EECE.2160: ECE Application Programming Spring 2016 Exam 1 February 19, 2016 Name: Section (circle 1): 201 (8-8:50, P. Li) 202 (12-12:50, M. Geiger) For this exam, you may use only one 8.5 x 11 double-sided

More information

Programming for Electrical and Computer Engineers. Loops

Programming for Electrical and Computer Engineers. Loops Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly

More information

Programming for Engineers Iteration

Programming for Engineers Iteration Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 5 Structured Program Development Department of Computer Engineering How to develop

More information

More examples for Control statements

More examples for Control statements More examples for Control statements C language possesses such decision making capabilities and supports the following statements known as control or decision-making statements. 1. if statement 2. switch

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 5 - Structured Program Development Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad How to develop a program? Requirements Problem Analysis

More information

Dept. of CSE, IIT KGP

Dept. of CSE, IIT KGP Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of

More information

Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes:

Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes: CMPE108, Homework-2 (C Fundamentals, Expressions, and Selection Structure.) Student Nr:... Name,Surname:...:... CMPE108 Group:... Signature... Please print this homework, and solve all questions on the

More information

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs PDS Lab Section 16 Autumn-2017 Tutorial 3 C Programming Constructs This flowchart shows how to find the roots of a Quadratic equation Ax 2 +Bx+C = 0 Start Input A,B,C x B 2 4AC False x If 0 True B x 2A

More information

Introduction to Programming

Introduction to Programming Introduction to Programming ( 數 ) Lecture 4 Spring 2005 March 11, 2005 Topics Review of if statement The switch statement Repetition and Loop Statements For-Loop Condition-Loop Reading: Chap. 5.7~ Chap.

More information

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

Control Structure: Loop

Control Structure: Loop Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure 1 Loop Structure Condition is tested first

More information

Conditional Statement

Conditional Statement Conditional Statement 1 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition Also called Branching How do we specify conditions?

More information

do, while and for Constructs

do, while and for Constructs Programming Fundamentals for Engineers 0702113 4. Loops do, while and for Constructs Muntaser Abulafi Yacoub Sabatin Omar Qaraeen 1 Loops A loop is a statement whose job is to repeatedly execute some other

More information

Name Roll No. Section

Name Roll No. Section Indian Institute of Technology, Kharagpur Computer Science and Engineering Department Class Test I, Autumn 2012-13 Programming & Data Structure (CS 11002) Full marks: 30 Feb 7, 2013 Time: 60 mins. Name

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 5, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Introduction to the C language Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) The C language

More information

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE 공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE LOOPS WHILE AND FOR while syntax while (expression) statement The expression is evaluated. If it is

More information

UNIT 2 PROBLEMS SOLVING USING C PROGRAMMING LANGUAGE

UNIT 2 PROBLEMS SOLVING USING C PROGRAMMING LANGUAGE UNIT 2 PROBLEMS SOLVING USING C PROGRAMMING LANGUAGE Structure Page Nos. 2.0 Introduction 22 2.1 Objectives 22 2.2 Execution of a C Program 23 2.3 Structure of a C Programme 23 2.4 Basic Components of

More information

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops. Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop

More information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

Computer Programming. Decision Making (2) Loops

Computer Programming. Decision Making (2) Loops Computer Programming Decision Making (2) Loops Topics The Conditional Execution of C Statements (review) Making a Decision (review) If Statement (review) Switch-case Repeating Statements while loop Examples

More information

Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs

Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs Morteza Noferesti Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs We want to solve a real problem by computers Take average, Sort, Painting,

More information

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

COP 2000 Introduction to Computer Programming Mid-Term Exam Review he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can

More information

Unit 3 Decision making, Looping and Arrays

Unit 3 Decision making, Looping and Arrays Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.

More information

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from INTRODUCTION TO C++ PROGRAM CONTROL Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Repetition Statement for while do.. while break and continue

More information

Chapter 1 & 2 Introduction to C Language

Chapter 1 & 2 Introduction to C Language 1 Chapter 1 & 2 Introduction to C Language Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 1 & 2 - Introduction to C Language 2 Outline 1.1 The History

More information

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

COMP 2001/2401 Test #1 [out of 80 marks]

COMP 2001/2401 Test #1 [out of 80 marks] COMP 2001/2401 Test #1 [out of 80 marks] Duration: 90 minutes Authorized Memoranda: NONE Note: for all questions, you must show your work! Name: Student#: 1. What exact shell command would you use to:

More information

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University Overview of C, Part 2 CSE 130: Introduction to Programming in C Stony Brook University Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the

More information

Structured Program Development

Structured Program Development Structured Program Development Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Introduction The selection statement if if.else switch The

More information

A control structure refers to the way in which the Programmer specifies the order of executing the statements

A control structure refers to the way in which the Programmer specifies the order of executing the statements Control Structures A control structure refers to the way in which the Programmer specifies the order of executing the statements The following approaches can be chosen depending on the problem statement:

More information

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

COP 3223 Introduction to Programming with C - Study Union - Spring 2018 COP 3223 Introduction to Programming with C - Study Union - Spring 2018 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

16.216: ECE Application Programming Fall 2011

16.216: ECE Application Programming Fall 2011 16.216: ECE Application Programming Fall 2011 Exam 2 Solution 1. (24 points, 6 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling

More information

Repetition Structures II

Repetition Structures II Lecture 9 Repetition Structures II For and do-while loops CptS 121 Summer 2016 Armen Abnousi Types of Control Structures Sequential All programs that we have written so far The statements inside a pair

More information

The C language. Introductory course #1

The C language. Introductory course #1 The C language Introductory course #1 History of C Born at AT&T Bell Laboratory of USA in 1972. Written by Dennis Ritchie C language was created for designing the UNIX operating system Quickly adopted

More information

Computers Programming Course 7. Iulian Năstac

Computers Programming Course 7. Iulian Năstac Computers Programming Course 7 Iulian Năstac Recap from previous course Operators in C Programming languages typically support a set of operators, which differ in the calling of syntax and/or the argument

More information

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Data Types Basic Types Enumerated types The type void Derived types

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++ CHAPTER 9 C++ 1. WRITE ABOUT THE BINARY OPERATORS USED IN C++? ARITHMETIC OPERATORS: Arithmetic operators perform simple arithmetic operations like addition, subtraction, multiplication, division etc.,

More information

Decision making with if Statement : - Control Statements. Introduction: -

Decision making with if Statement : - Control Statements. Introduction: - Control Statements Introduction: - Any C program if you consider, the set of statements are normally executed sequentially in the order in which they are written, and such programs have sequential structure

More information

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed C Overview C OVERVIEW Goals speed portability allow access to features of the architecture speed C fast executables allows high-level structure without losing access to machine features many popular languages

More information

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade; Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

More information

Chapter 3 Structured Program Development

Chapter 3 Structured Program Development 1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction

More information

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces

More information

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting Your factors.c and multtable.c files are due by Wednesday, 11:59 pm, to be submitted on the SoC handin page at http://handin.cs.clemson.edu.

More information

These are reserved words of the C language. For example int, float, if, else, for, while etc.

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

More information

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU CSE101-lec#12 Designing Structured Programs Introduction to Functions Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Designing structured programs in C: Counter-controlled repetition

More information

Operators and Control Flow. CS449 Fall 2017

Operators and Control Flow. CS449 Fall 2017 Operators and Control Flow CS449 Fall 2017 Running Example #include /* header file */ int main() { int grade, count, total, average; /* declaramons */ count = 0; /* inimalizamon */ total = 0;

More information

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock) C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 By the end of this lecture, you will be able to identify the

More information

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 1 By the end of this lecture, you will be able to identify

More information