Download presentation
Presentation is loading. Please wait.
1
For loops Increment ,decrement operations
Revision For loops Increment ,decrement operations
2
Why study C? Helps to understand fundamental aspects of programming.
Many popular software tools are written in C Has strongly influenced many other languages. Very concise language.
3
Simple program
4
C Data Types There are only a few basic data types in C: char int
float double short, long, signed and unsigned are additional qualifiers.
5
Arithmetic operations in C
6
Rules of operator precedence
7
Equality and Relational Operators
These operators are having lower priority then mathematical operators. The comma operator is having least priority. And its associativity is left to right . Equality operators have lower priority then logical operators.
8
Some programmer jargon
Source code: The program you are writing. Compile (build): Taking source code and making a program that the computer can understand. Executable: The compiled program that the computer can run. Library: Added functions for C programming which are bolted on to do certain tasks. Header file: Files ending in .h which are included at the start of source code.
9
Few tips C does not care much about spaces. Statements are considered ending when “;” encountered. Variables in C can be given any name made from numbers, letters and underlines which is not a keyword and does not begin with a number.
10
Few tips A character variable - has a single quote not a double quote. Ex char c=‘a’; unsigned means that an int or char value can only be positive. signed means that it can be positive or negative. long means that int, float or double have more precision (and are larger) short means they have less const means a variable which doesn't vary – useful for physical constants or things like pi
11
Few tips ++i means increment i then use it
i++ means use i then increment it we can use -- (subtract one) e.g. countdown--; += (add to a variable) e.g. a+= 5; -= (subtract from variable) e.g. num_living-= num_dead; *= (multiply a variable) e.g. no_bunnies*=2; /= (divide a variable) e.g. fraction/= divisor;
12
Loop A repetition structure allows the programmer to specify that an action is to be repeated while some condition remains true. There are three repetition structures in C, the while loop, the for loop, and the do-while loop.
13
The while Repetition Structure
Note:- The braces are not required if the loop body contains only a single statement. However, they are a good practice. first expr is evaluated. If it is nonzero (true), then statement is executed, and control is passed back to the beginning of the while loop. The body of the while loop, is executed repeatedly until expr is zero (false). At that point control passes to next statement. while ( expr ) { statement(s); } Next statement;
14
The for Statement for (expr1; expr2; expr3) { statement(s) }
next statement Note :- First expr1 is evaluated; typically expr1 is used to initialize the loop. Then expr2 is evaluated; if it is nonzero (true) then statement is executed, expr3 is evaluated, and 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. Advantage of for loop :- can do initialization, testing and indexing all at once on top of loop. It is used when the programmer knows how many times a set of statements are to be executed. Any or all of the expressions in a for statement can be missing, but the two semicolons must remain.
15
The do Statement The do statement is a variant of the while statement that tests its condition at the bottom of the loop. do {statement(s) } while (expr); Next statement
16
The break and continue Statements
The break statement causes an exit from the innermost enclosing loop or switch statement. The continue statement causes the current iteration of a loop to stop and the next iteration to begin immediately.
17
How Break and continue work
18
Program1 –give output #include<stdio.h> int main(){
int i,j ,m,n; i=j=2,3; while(--i && j++) printf("%d %d",i,j); i=2,3,4; j=(2,3,4); printf(“ \n%d”, i+j); return 0; }
19
Output1 1 3 6 Notes:- “The assignment of “3” value has no effect . Actually writing anything without any proper presemble would not have any effect. Like { 3;4;printf(“test”);} here the “3;4; would not give any error but also would not give any output. As they are valid tokens. -The multiple values cant be assigned at declaration time –it would be syntax error Ex int i=2,3, - invalid -When comma used inside the parenthesis , its priority is increased . So the j= (2,3,4) which as two comma , has higher priority to the second comma hence j=4 is assigned. *) variation – if i=j=3 ,Output would be – “ 2 4 and 1 5”
20
Program 2 #include <stdio.h> int main() { int i=1;
for(++ i;i<10;++ i) { printf(“%d ”,i); if (i==5) break; } printf(“\n”); for ( ; ++i ; ++i) { printf(“ %d ”,i ); if (i==8) break; } return 0;
21
output2 2 3 4 5 6 8 Note:- in for loop –
- the first part assignment operation is needed ,if any comparison like i>12 , is used , it would simply ignore it and not do any change in the value of the loop variable used . The middle should be expression which on evaluation should return true (non zero value). If any assignment statement used here, it would execute that statement and return true and would enter the loop. If nothing mentioned , it would by default consider status true and would enter loop. The last part should be increment or decrement , if it is any other format , it would simply ignore it and not do any change in the value of the loop variable used.
22
Problem 3 #include <stdio.h> int main() { int i=1;
for(i=0;i=-1;i=1) { printf(“%d”,i); if (i!=1) break; } return 0;
23
Output3 -1
24
Problem 6 #include <stdio.h> int main() { for(; ;)
{ printf(“%d”, 10); } return 0;
25
Output 6 ………infinite times
26
Problem 7 #include <stdio.h> int main() { int I;
for(i=0;i<=5 ;i++); printf(“%d”,i); return 0; }
27
Output 7 6
28
Problem 8 #include<stdio.h> int main(){ int i;
for(i=5;i<=15;i++) { while (i) if(i=i-1) { printf("i is %d \n ",i); continue; } } break; return 0;
29
Output 8 i is 4 i is 3 i is 2 i is 1
30
Problem 9 #include<stdio.h> void main(){ int k,num=30;
k=(num>5 ? (num<=10? 100:200):500); printf(“%d”\n”, num); return 0; }
31
Output 9 30
32
Problem 10 #include < stdio.h > int main() { int tally=0;
for(;;) { if(tally==10) break; printf("%d ",++tally); } return 0; }
33
Solution 10
34
P12 #include <stdio.h> void main() { int cnt=1; do {
{ printf("%d,",cnt); cnt+=1; }while(cnt>=10); printf("\nAfter loop cnt=%d",cnt); printf("\n"); }
35
S12 1, After loop cnt=2
36
p13 #include <stdio.h> void main() { int cnt=1;
while(cnt>=10) { printf("%d,",cnt); cnt+=1; } printf("\nAfter loop cnt=%d",cnt); printf("\n"); }
37
S13 After loop cnt=1
38
p14 #include <stdio.h> int main() { int i=1;
while(i<=10 && 1++) printf("Hello"); }
39
s14 Error: lvalue required as increment operand
(for the increment , we cannot have constant we need variable).
40
p15 #include <stdio.h> void main() { int a=10; switch(a){
{ int a=10; switch(a){ case 5+5: printf("Hello\n"); default: printf("OK\n"); } }
41
s15 Hello OK
42
p16 #include <stdio.h> void main() { short day=2; switch(day) {
{ case 2: || case 22: printf("%d nd",day); break; default: printf("%d th",day); break; } }
43
s16 Syntax Error We can not use || operator between case statements, case 2:||case 22: invalid
44
p17 #include <stdio.h> void main() { int x=12,y=7,z;
z=x!=4 || y==2; printf(“z= %d”, z); }
45
s17 1
46
P18- #include <stdio.h> void main(){
enum status {pass ,fail, rem}; enum status std1,std2,std3; std1=pass; std2=rem; std3=fail; printf(“ %d % d %d”, std1,std2,std3); }
47
s18
48
p19 #include <stdio.h> int main() { int i;
for(i=0; i< 5; i++) { if(i*i > 30 ) goto lbl; else printf("%d",i); lbl: printf("IHelp "); } return 0; }
49
s19 0IHelp 1IHelp 2IHelp 3IHelp 4IHelp (lbl label is declared within the for() body just after the if else statement and lbl label is executed after if or else body.)
50
P20 #include <stdio.h> int main() { int x; float y=7.0;
switch(x=y+1) { case 8: printf("It's Eight."); break; default: printf("Oops No choice here!!!"); } }
51
s20 It's Eight. (Here, x=y+1 will be 8 – because x is an integer variable so final value that will return through this expression is 8.)
52
s21 #include <stdio.h> int main() { int a=10; if(a==10) {
{ printf("Hello..."); break; printf("Ok"); } else printf("Hii"); return 0; }
53
021 Error : illegal break. A break statement can be used with looping and switch statements.
54
s22 #include <stdio.h> int main() { int pn=100; if(pn>20)
printf("Heyyyyy"); else printf("Hiiiii"); return 0; }
55
O22 Hiiiii printf("Hiiiii"); that is written after else , is treated as else part of inner if condition if(pn<20).
56
s23 Which of the following are incorrect statements? If int a=10.
1) if( a==10 ) printf("IncludeHelp"); 2) if( 10==a ) printf("IncludeHelp"); 3) if( a=10 ) printf("IncludeHelp"); 4) if( 10=a ) printf("IncludeHelp");
57
023 4 only.
58
S 24 char ch; ch=‘A’; printf(“The letter is”);
printf(“%c”,ch>=‘A’ && ch<=‘Z’ ? ch+’a’-’A’ : ch); printf(“Now the letter is “); printf(“%c\n”,ch>=‘A’ && ch<=‘Z’? ch : ch+’a’-’A’);
59
O24 The letter is a Now the letter is A
60
p25 int i=2; int j=i + (1,2,3,4,5); printf(“%d”,j);
61
025 7 Reason:- comma operator has left –right associativity .the left operand is evaluated first and result is discarded before right operand is evaluated .
62
Steps to implement “c” program
Writing a C code. {editors like gedit, vi} Compiling a C code. {gcc –c test.c –o test} Executing the object code. {./test}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.