-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLecture2.c
179 lines (155 loc) · 2.97 KB
/
Lecture2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// get bit
int num=7; int bitNum=1;
00000011 &
00000001
00000001
00000010
int bitValue=(num>>bitNum)&1;
int bitValue=(num&(1<<bitNum))>>bitNum;
////////////////////////////////////////////////////////////////
// clear bit
int num=7; int bitNum=1;
00000111 &
00000010
~(00000010)-->1111101
num&= ~(1<<bitNum);
//////////////////////////////////////////////////////////////
/****************conditional statements***********************/
2- switch statement
switch(variable)
{
case constant_value1 : // must be unique, can't be variable,(int char)
statement1;
statement2;
break; // optional
case constant_value2 :
statement1;
statement2;
break;
default : //optional, can be placed anywhere in the switch, one defaualt is allowed
statement1;
statement2;
break;
}
// nesting is allowed
int n=2;
switch(n)
{
case 1:
printf("one");
break;
case 2:
printf("two");
break;
default:
printf("wrong number");
break;
}
/******************************** loop in c **********************************/
printf("1");
printf("1");
printf("1");
printf("1");
printf("1");
1- for loop
syntax
for(start ; condition ; action )
{
/* body of loop*/
}
1)start
2)check on condition
3)body of loop
4)action
5)repeat steps 2) to 4) until condition be false
int i;
for(i=0 ; i<100 ; i++)
{
printf("1");
}
i=0
0<5 true
printf("1");
i=1
i<5
printf("1");
i=2
i=5
5<5 false ---> loop is finished
2- while loop 3- do while loop
syntax
while(condition)
{
/*body of loop*/
}
int n=0;
while(n!=5)
{
printf("try again\n");
scanf("%d",&n);
}
printf("ok");
syntax
do
{
/* body of loop */
}while(condition);
do
{
if(n!=0)
{
printf("try again\n");
}
scanf("%d",&n);
}while(n!=5);
/////////////////////////////////////////////////////////////////////
/********************* break , continue ***************************************/
break to exit loop
continue skip current iteration
while(1)
{
scanf("%d",&n);
if(n==12)
break;
}
for(i=0;i<5 ; i++)
{
if(i==3)
continue;
printf("%d",i);
}
/*************************** Array **********************************************/
array : group of data have the same type
syntax:
type array_name[length];
length----> constant , can't be variable /* arr[n] not allowed */
ex: int arr[5];
intialization
int arr1[]={1,2,3,4,5};
or int arr1[5]={1,2,3,4,5};
int arr[3];
arr[3]={1,2,3}; /* not allowed */
for(i=0;i<5,i++)
{
arr1[i]=1;
}
i: index of arr1 0--->arr_length-1
printf("%d",arr[3]); // garbge value
scanf("%d",&arr[0]);
for(i=0; i<arr_size;i++)
{
scanf("%d",&arr[i]);
}
char string[10];
scanf("%s",&string); or scanf("%s",string);
char string[]="Ahmed mohamed ";
for(i=0; i<sizeof(string)/sizeof(string[0]);i++)
{
printf("%d",string[i]);
}
char s[5]={'A','h','m','e','d'};
for(i=0; i<5;i++)
{
printf("%d",s[i]);
}
printf("%s",s);