· 6 years ago · Nov 23, 2019, 12:02 AM
1
2--------------------------- KEY ---------------------------
3
4#######'s = New section
5
6[[[[[[]]]]]] discriptor [[[[[[]]]]]] = sub-catagory
7
8---------------------------
9 code here; = code snippet
10---------------------------
11
12--------------------------- END ---------------------------
13
14
15
16This document contains the following sections:
17
18- basic syntax (line 31)
19- data types (line 162)
20- variables (line 298)
21- constants (line 491)
22- operators (line 684)
23
24- decision making, (line 855)
25 'if, else if, else'
26 statements
27
28- loops (line 1031)
29
30
31
32###############################################################
33###############################################################
34###############################################################
35### Basic Syntax ##############################################
36###############################################################
37###############################################################
38###############################################################
39
40[[[[[[]]]]]] Tokens [[[[[[]]]]]]
41
42 A C program consists of various tokens and a token is either
43 a keyword, an identifier, a constant, a string literal
44 (thse will all be explained later on), or a symbol.
45 For example, the following C statement consists of five
46 tokens −
47
48 ----------
49 printf("Hello, World! \n");
50 ----------
51
52 The individual tokens are −
53
54 ---------------------------
55 printf
56 (
57 "Hello, World! \n"
58 )
59 ;
60 ---------------------------
61
62
63[[[[[[]]]]]] Semicolons [[[[[[]]]]]]
64
65 In a C program, the semicolon is a statement terminator.
66 That is, each individual statement must be ended with a semicolon.
67 It indicates the end of one logical entity.
68
69 Given below are two different statements −
70 ---------------------------
71 printf("Hello, World! \n");
72 return 0;
73 ---------------------------
74
75
76
77[[[[[[]]]]]] Comments [[[[[[]]]]]]
78
79 Comments are like helping text in your C program and they are
80 ignored by the compiler. They start with /* and terminate with
81 the characters */ as shown below −
82
83 ---------------------------
84 // this is a single line comment
85
86 /* This is a multi
87 line comment */
88 ---------------------------
89
90 You cannot have comments within comments and they do not occur
91 within a string or character literals.
92
93
94
95[[[[[[]]]]]] Identifiers [[[[[[]]]]]]
96
97 A C identifier is a name used to identify a variable, function,
98 or any other user-defined item. An identifier starts with a letter
99 A to Z, a to z, or an underscore '_' followed by zero or more
100 letters, underscores, and digits (0 to 9).
101
102 C does not allow punctuation characters such as @, $, and % within
103 identifiers. C is a case-sensitive programming language.
104 Thus, Manpower and manpower are two different identifiers in C.
105 Here are some examples of acceptable identifiers −
106
107 mohd zara abc move_name a_123
108 myname50 _temp j a23b9 retVal
109
110
111
112
113[[[[[[]]]]]] Keywords [[[[[[]]]]]]
114
115 The following list shows the reserved words in C.
116 These reserved words may not be used as constants or variables
117 or any other identifier names.
118
119
120 auto else long switch
121 break enum register typedef
122 case extern return union
123 char float short unsigned
124 const for signed void
125 continue goto sizeof volatile
126 default if static while
127 do int struct _Packed
128 double
129
130
131
132[[[[[[]]]]]] Whitespace in C [[[[[[]]]]]]
133
134 A line containing only whitespace, possibly with a comment, is
135 known as a blank line, and a C compiler totally ignores it.
136
137 Whitespace is the term used in C to describe blanks, tabs, newline
138 characters and comments. Whitespace separates one part of a statement
139 from another and enables the compiler to identify where one element
140 in a statement, such as int, ends and the next element begins.
141 Therefore, in the following statement −
142
143 ---------------------------
144 int age;
145 ---------------------------
146
147 there must be at least one whitespace character (usually a space)
148 between int and age for the compiler to be able to distinguish them.
149 On the other hand, in the following statement −
150
151 ---------------------------
152 fruit=apples+oranges; // get the total fruit
153 ---------------------------
154
155 no whitespace characters are necessary between fruit and =, or
156 between = and apples, although you are free to include some
157 if you wish to increase readability.
158
159###############################################################
160###############################################################
161###############################################################
162### Data Types ################################################
163###############################################################
164###############################################################
165###############################################################
166
167 Data types in c refer to an extensive system used for declaring
168 variables or functions of different types. The type of a variable
169 determines how much space it occupies in storage and how the bit
170 pattern stored is interpreted.
171
172 The types in C can be classified as follows −
173
174 1 Basic Types:
175 They are arithmetic types and are further classified into:
176 (a) integer types and
177 (b) floating-point types.
178
179 2 Enumerated types:
180 They are again arithmetic types and they are used to define
181 variables that can only assign certain discrete integer values
182 throughout the program.
183
184 3 The type void:
185 The type specifier void indicates that no value is available.
186
187 4 Derived types:
188 They include
189 (a) Pointer types,
190 (b) Array types,
191 (c) Structure types,
192 (d) Union types and
193 (e) Function types.
194
195 The array types and structure types are referred collectively
196 as the aggregate types. The type of a function specifies the
197 type of the function's return value. We will see the basic
198 types in the following section, where as other types will
199 be covered in the upcoming chapters.
200
201
202
203
204[[[[[[]]]]]] Integer Types [[[[[[]]]]]]
205
206 The following table provides the details of standard integer
207 types with their storage sizes and value ranges −
208
209
210 ______________________________________________________________
211 Type Storage size Value range
212 ______________________________________________________________
213 char 1 byte -128 to 127 OR 0 to 255
214 unsigned char 1 byte 0 to 255
215 signed char 1 byte -128 to 127
216 int 2 OR 4 bytes -32,768 to 32,767
217 OR -2,147,483,648 to 2,147,483,647
218
219 unsigned int 2 OR 4 bytes 0 to 65,535 OR 0 to 4,294,967,295
220 short 2 bytes -32,768 to 32,767
221 unsigned short 2 bytes 0 to 65,535
222 long 4 bytes -2,147,483,648 to 2,147,483,647
223 unsigned long 4 bytes 0 to 4,294,967,295
224 _______________________________________________________________
225
226 To get the exact size of a type or a variable on a particular
227 platform, you can use the sizeof operator.
228 The expressions sizeof(type) yields the storage size of the
229 object or type in bytes. Given below is an example to get the
230 size of int type on any machine −
231
232 ---------------------------
233 #include <stdio.h>
234 #include <limits.h>
235
236 int main() {
237 printf("Storage size for int : %d \n", sizeof(int));
238
239 return 0;
240 }
241 ---------------------------
242
243
244
245[[[[[[]]]]]] Floating-Point Types [[[[[[]]]]]]
246
247 The following table provide the details of standard floating-point
248 types with storage sizes and value ranges and their precision −
249
250 _______________________________________________________________
251 Type Storage Value range Precision
252 ________________size___________________________________________
253 float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
254 double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
255 long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places
256 _______________________________________________________________
257
258 The header file float.h defines macros that allow you to use
259 these values and other details about the binary representation
260 of real numbers in your programs. The following example prints
261 the storage space taken by a float type and its range values −
262
263 ---------------------------
264 #include <stdio.h>
265 #include <float.h>
266
267 int main() {
268 printf("Storage size for float : %d \n", sizeof(float));
269 printf("Minimum float positive value: %E\n", FLT_MIN );
270 printf("Maximum float positive value: %E\n", FLT_MAX );
271 printf("Precision value: %d\n", FLT_DIG );
272
273 return 0;
274 }
275 ---------------------------
276
277[[[[[[]]]]]] The void type [[[[[[]]]]]]
278
279 Function returns as void:
280 There are various functions in C which do not return any value
281 or you can say they return void. A function with no return value
282 has the return type as void. For example, void exit (int status);
283
284 Function arguments as void:
285 There are various functions in C which do not accept any parameter.
286 A function with no parameter can accept a void. For example,
287 int rand(void);
288
289 Pointers to void:
290 A pointer of type void * represents the address of an object,
291 but not its type. For example, a memory allocation function
292 void *malloc( size_t size ); returns a pointer to void which
293 can be casted to any data type.
294
295###############################################################
296###############################################################
297###############################################################
298### Variables #################################################
299###############################################################
300###############################################################
301###############################################################
302
303 A variable is nothing but a name given to a storage area that our
304 programs can manipulate. Each variable in C has a specific type,
305 which determines the size and layout of the variable's memory;
306 the range of values that can be stored within that memory; and
307 the set of operations that can be applied to the variable.
308
309 The name of a variable can be composed of letters, digits,
310 and the underscore character. It must begin with either a
311 letter or an underscore. Upper and lowercase letters are distinct
312 because C is case-sensitive. Based on the basic types explained
313 in the previous chapter, there will be the following basic
314 variable types −
315
316 1 char:
317 Typically a single octet(one byte). This is an integer type.
318
319 2 int:
320 The most natural size of integer for the machine.
321
322 3 float:
323 A single-precision floating point value.
324
325 4 double:
326 A double-precision floating point value.
327
328 5 void:
329 Represents the absence of type.
330
331 C programming language also allows to define various other types
332 of variables, which we will cover in subsequent chapters like
333 Enumeration, Pointer, Array, Structure, Union, etc. For this
334 chapter, let us study only basic variable types.
335
336[[[[[[]]]]]] Variable Definition in C [[[[[[]]]]]]
337
338 A variable definition tells the compiler where and how much
339 storage to create for the variable. A variable definition
340 specifies a data type and contains a list of one or more
341 variables of that type as follows −
342
343 ---------------------------
344 type variable_list;
345 ---------------------------
346
347 Here, type must be a valid C data type including
348 char, w_char, int, float, double, bool, or any user-defined object;
349 and variable_list may consist of one or more identifier names
350 separated by commas. Some valid declarations are shown here −
351
352 ---------------------------
353 int i, j, k;
354 char c, ch;
355 float f, salary;
356 double d;
357 ---------------------------
358
359 The line int i, j, k; declares and defines the variables i, j, and k;
360 which instruct the compiler to create variables named i, j and k of
361 type int.
362
363 Variables can be initialized (assigned an initial value) in their
364 declaration. The initializer consists of an equal sign followed by
365 a constant expression as follows −
366
367 ---------------------------
368 type variable_name = value;
369 ---------------------------
370
371 Some examples are −
372
373 ---------------------------
374 extern int d = 3, f = 5; // declaration of d and f.
375 int d = 3, f = 5; // definition and initializing d and f.
376 byte z = 22; // definition and initializes z.
377 char x = 'x'; // the variable x has the value 'x'.
378 ---------------------------
379
380 For definition without an initializer: variables with static storage
381 duration are implicitly initialized
382 with NULL (all bytes have the value 0); the initial
383 value of all other variables are undefined.
384
385[[[[[[]]]]]] Variable Declaration in C [[[[[[]]]]]]
386
387 A variable declaration provides assurance to the compiler that
388 there exists a variable with the given type and name so that the
389 compiler can proceed for further compilation without requiring
390 the complete detail about the variable. A variable definition
391 has its meaning at the time of compilation only, the compiler
392 needs actual variable definition at the time of linking the
393 program.
394
395 A variable declaration is useful when you are using multiple
396 files and you define your variable in one of the files which
397 will be available at the time of linking of the program. You
398 will use the keyword extern to declare a variable at any place.
399 Though you can declare a variable multiple times in your C program,
400 it can be defined only once in a file, a function, or a block of code.
401
402 Example
403 Try the following example, where variables have been declared
404 at the top, but they have been defined and initialized inside
405 the main function −
406
407 ---------------------------
408 #include <stdio.h>
409
410 // Variable declaration:
411 extern int a, b;
412 extern int c;
413 extern float f;
414
415 int main () {
416
417 /* variable definition: */
418 int a, b;
419 int c;
420 float f;
421
422 /* actual initialization */
423 a = 10;
424 b = 20;
425
426 c = a + b;
427 printf("value of c : %d \n", c);
428
429 f = 70.0/3.0;
430 printf("value of f : %f \n", f);
431
432 return 0;
433 }
434 ---------------------------
435
436 When the above code is compiled and executed, it produces the
437 following result −
438
439 ---------------------------
440 value of c : 30
441 value of f : 23.333334
442 ---------------------------
443
444 The same concept applies on function declaration where you
445 provide a function name at the time of its declaration and
446 its actual definition can be given anywhere else. For example −
447
448 ---------------------------
449 // function declaration
450 int func();
451
452 int main() {
453
454 // function call
455 int i = func();
456 }
457
458 // function definition
459 int func() {
460 return 0;
461 }
462 ---------------------------
463
464[[[[[[]]]]]] Lvalues and Rvalues in C [[[[[[]]]]]]
465
466 There are two kinds of expressions in C −
467
468 lvalue −- Expressions that refer to a memory location are called
469 "lvalue" expressions. An lvalue may appear as either the left-hand
470 or right-hand side of an assignment.
471
472 rvalue −- The term rvalue refers to a data value that is stored
473 at some address in memory. An rvalue is an expression that cannot
474 have a value assigned to it which means an rvalue may appear on
475 the right-hand side but not on the left-hand side of an assignment.
476
477 Variables are lvalues and so they may appear on the left-hand side
478 of an assignment. Numeric literals are rvalues and so they may not
479 be assigned and cannot appear on the left-hand side. Take a look at
480 the following valid and invalid statements −
481
482 ---------------------------
483 int g = 20; // valid statement
484 10 = 20; // invalid statement; would generate compile-time error
485 ---------------------------
486
487
488###############################################################
489###############################################################
490###############################################################
491### Constants #################################################
492###############################################################
493###############################################################
494###############################################################
495
496 Constants refer to fixed values that the program may not alter
497 during its execution. These fixed values are also called literals.
498
499 Constants can be of any of the basic data types like an integer
500 constant, a floating constant, a character constant, or a string
501 literal. There are enumeration constants as well.
502
503 Constants are treated just like regular variables except that
504 their values cannot be modified after their definition.
505
506[[[[[[]]]]]] Integer Literals [[[[[[]]]]]]
507
508 An integer literal can be a decimal, octal, or hexadecimal constant.
509 A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0
510 for octal, and nothing for decimal.
511
512 An integer literal can also have a suffix that is a combination of
513 U and L, for unsigned and long, respectively. The suffix can be
514 uppercase or lowercase and can be in any order.
515
516 Here are some examples of integer literals −
517
518 ---------------------------
519 212 /* Legal */
520 215u /* Legal */
521 0xFeeL /* Legal */
522 078 /* Illegal: 8 is not an octal digit */
523 032UU /* Illegal: cannot repeat a suffix */
524 ---------------------------
525
526 Following are other examples of various types of integer literals −
527
528 ---------------------------
529 85 /* decimal */
530 0213 /* octal */
531 0x4b /* hexadecimal */
532 30 /* int */
533 30u /* unsigned int */
534 30l /* long */
535 30ul /* unsigned long */
536 ---------------------------
537
538[[[[[[]]]]]] Floating-point Literals [[[[[[]]]]]]
539
540 A floating-point literal has an integer part, a decimal point,
541 a fractional part, and an exponent part. You can represent floating
542 point literals either in decimal form or exponential form.
543
544 While representing decimal form, you must include the decimal
545 point, the exponent, or both; and while representing exponential
546 form, you must include the integer part, the fractional part,
547 or both. The signed exponent is introduced by e or E.
548
549 Here are some examples of floating-point literals −
550
551 ---------------------------
552 3.14159 /* Legal */
553 314159E-5L /* Legal */
554 510E /* Illegal: incomplete exponent */
555 210f /* Illegal: no decimal or exponent */
556 .e55 /* Illegal: missing integer or fraction */
557 ---------------------------
558
559[[[[[[]]]]]] Character Constants [[[[[[]]]]]]
560
561 Character literals are enclosed in single quotes, e.g., 'x' can be
562 stored in a simple variable of char type.
563
564 A character literal can be a plain character (e.g., 'x'), an escape
565 sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').
566
567 There are certain characters in C that represent special meaning
568 when preceded by a backslash for example, newline (\n) or tab (\t).
569
570 ________________________________________________________________
571 Escape sequence Meaning
572 ________________________________________________________________
573 \\ \ character
574 \' ' character
575 \" " character
576 \? ? character
577 \a Alert or bell
578 \b Backspace
579 \f Form feed
580 \n Newline
581 \r Carriage return
582 \t Horizontal tab
583 \v Vertical tab
584 \ooo Octal number of one to three digits
585 \xhh . . . Hexadecimal number of one or more digits
586 _________________________________________________________________
587
588 Following is the example to show a few escape sequence characters −
589
590 ---------------------------
591 #include <stdio.h>
592
593 int main() {
594 printf("Hello\tWorld\n\n");
595
596 return 0;
597 }
598 ---------------------------
599
600[[[[[[]]]]]] String Literals [[[[[[]]]]]]
601
602 String literals or constants are enclosed in double quotes "".
603 A string contains characters that are similar to character
604 literals: plain characters, escape sequences, and universal characters.
605
606 You can break a long line into multiple lines using string
607 literals and separating them using white spaces.
608
609 Here are some examples of string literals. All the three forms
610 are identical strings.
611
612 ---------------------------
613 "hello, dear"
614
615 "hello, \
616
617 dear"
618
619 "hello, " "d" "ear"
620 ---------------------------
621
622[[[[[[]]]]]] Define Constants [[[[[[]]]]]]
623
624 There are two simple ways in C to define constants −
625
626 1 Using #define preprocessor.
627 2 Using const keyword.
628
629 ---------------------------
630 #define identifier value
631 ---------------------------
632
633 The following example explains it in detail −
634
635 ---------------------------
636 #include <stdio.h>
637
638 #define LENGTH 10
639 #define WIDTH 5
640 #define NEWLINE '\n'
641
642 int main() {
643 int area;
644
645 area = LENGTH * WIDTH;
646 printf("value of area : %d", area);
647 printf("%c", NEWLINE);
648
649 return 0;
650 }
651 ---------------------------
652
653
654[[[[[[]]]]]] The const Keyword [[[[[[]]]]]]
655
656 You can use const prefix to declare constants with a
657 specific type as follows −
658
659 ---------------------------
660 const type variable = value;
661 ---------------------------
662
663 The following example explains it in detail −
664
665 ---------------------------
666 #include <stdio.h>
667
668 int main() {
669 const int LENGTH = 10;
670 const int WIDTH = 5;
671 const char NEWLINE = '\n';
672 int area;
673
674 area = LENGTH * WIDTH;
675 printf("value of area : %d", area);
676 printf("%c", NEWLINE);
677
678 return 0;
679 }
680 ---------------------------
681
682
683
684###############################################################
685###############################################################
686###############################################################
687### Operators #################################################
688###############################################################
689###############################################################
690###############################################################
691
692 An operator is a symbol that tells the compiler to perform
693 specific mathematical or logical functions. C language is
694 rich in built-in operators and provides the following types
695 of operators −
696
697 Arithmetic Operators
698 Relational Operators
699 Logical Operators
700 Bitwise Operators
701 Assignment Operators
702 Misc Operators
703
704 We will, in this chapter, look into the way each operator works.
705
706[[[[[[]]]]]] Arithmetic Operators [[[[[[]]]]]]
707
708 The following table shows all the arithmetic operators supported
709 by the C language. Assume variable A holds 10 and variable B
710 holds 20 then −
711
712 ___________________________________________________________________
713 Operator Description Example
714 ___________________________________________________________________
715 + Adds two operands. A + B = 30
716 − Subtracts second operand from the first. A − B = -10
717 * Multiplies both operands. A * B = 200
718 / Divides numerator by de-numerator. B / A = 2
719
720 % Modulus Operator and remainder B % A = 0
721 of after an integer division.
722
723 ++ Increment operator increases A++ = 11
724 the integer value by one.
725
726 -- Decrement operator decreases the A-- =
727 integer value by one.
728 ___________________________________________________________________
729
730[[[[[[]]]]]] Relational Operators [[[[[[]]]]]]
731
732 The following table shows all the relational operators supported
733 by C. Assume variable A holds 10 and variable B holds 20 then −
734
735 ________________________________________________________
736 Operator Description Example
737 ________________________________________________________
738
739 == Checks if the values of two (A == B) is not true.
740 operands are equal or not.
741 If yes, then the condition
742 becomes true.
743
744 != Checks if the values of two (A != B) is true.
745 operands are equal or not.
746 If the values are not equal,
747 then the condition becomes
748 true.
749
750 > Checks if the value of left (A > B) is not true.
751 operand is greater than the
752 value of right operand. If
753 yes, then the condition
754 becomes true.
755
756 < Checks if the value of left (A < B) is true.
757 operand is less than the
758 value of right operand.
759 If yes, then the condition
760 becomes true.
761
762 >= Checks if the value of left (A >= B) is not true.
763 operand is greater than or
764 equal to the value of right
765 operand. If yes, then the
766 condition becomes true.
767
768 <= Checks if the value of left (A <= B) is true.
769 operand is less than or
770 equal to the value of right
771 operand. If yes, then the
772 condition becomes true.
773 ________________________________________________________
774
775[[[[[[]]]]]] Logical Operators [[[[[[]]]]]]
776
777 Following table shows all the logical operators supported
778 by C language. Assume variable A holds 1 and variable B
779 holds 0, then −
780
781 ___________________________________________________________________
782 Operator Description Example
783 ___________________________________________________________________
784 && Called Logical AND operator. If both (A && B) is false.
785 the operands are non-zero, then the
786 condition becomes true.
787
788 || Called Logical OR Operator. If any (A || B) is true.
789 of the two operands is non-zero, then
790 the condition becomes true.
791
792 ! Called Logical NOT Operator. It is !(A && B) is true.
793 used to reverse the logical state of
794 its operand. If a condition is true,
795 then Logical NOT operator will make it false.
796 ___________________________________________________________________
797
798
799[[[[[[]]]]]] Assignment Operators [[[[[[]]]]]]
800
801 The following table lists the assignment operators
802 supported by the C language −
803
804 _________________________________________________________________
805 Operator Description Example
806 _________________________________________________________________
807 = Simple assignment operator. Assigns C = A + B will
808 values from right side operands to assign the value
809 left side operand of A + B to C
810
811 += Add AND assignment operator. It C += A is equivalent
812 adds the right operand to the left to C = C + A
813 operand and assign the result to the
814 left operand.
815
816 -= Subtract AND assignment operator. It C -= A is equivalent
817 subtracts the right operand from the to C = C - A
818 left operand and assigns the result to
819 the left operand.
820
821 *= Multiply AND assignment operator. It C *= A is equivalent
822 multiplies the right operand with the to C = C * A
823 left operand and assigns the result
824 to the left operand.
825
826 /= Divide AND assignment operator. It C /= A is equivalent
827 divides the left operand with the to C = C / A
828 right operand and assigns the result
829 to the left operand.
830
831 %= Modulus AND assignment operator. It C %= A is equivalent
832 takes modulus using two operands and to C = C % A
833 assigns the result to the left operand.
834
835 <<= Left shift AND assignment operator. C <<= 2 is same as
836 C = C << 2
837
838 >>= Right shift AND assignment operator. C >>= 2 is same as
839 C = C >> 2
840
841 &= Bitwise AND assignment operator. C &= 2 is same as
842 C = C & 2
843
844 ^= Bitwise exclusive OR and assignment C ^= 2 is same as
845 operator. C = C ^ 2
846
847 |= Bitwise inclusive OR and assignment C |= 2 is same as
848 operator. C = C | 2
849 _________________________________________________________________
850
851
852###############################################################
853###############################################################
854###############################################################
855### Decision Making and IF statements #########################
856###############################################################
857###############################################################
858###############################################################
859
860 Decision making structures require that the programmer
861 specifies one or more conditions to be evaluated or tested
862 by the program, along with a statement or statements
863 to be executed if the condition is determined to be true,
864 and optionally, other statements to be executed if the
865 condition is determined to be false.
866
867 Show below is the general form of a typical decision
868 making structure found in most of the programming languages −
869
870
871 NOTE: This IS an 'if statement'
872
873 START
874 |
875 V
876 condition ---------
877 | |
878 V |
879 if condition if condition
880 is true is false
881 | |
882 V |
883 code |
884 | |
885 V |
886 END <-------------
887
888 NOTE: This IS an 'if statement'
889
890
891 C programming language assumes any non-zero and non-null
892 values as true, and if it is either zero or null, then
893 it is assumed as false value.
894
895 C programming language provides the following types of
896 decision making statements.
897
898
899 if statement: An if statement consists of a boolean
900 expression followed by one or more
901 statements.
902
903 if...else statement: An if statement can be followed
904 by an optional else statement, which
905 executes when the Boolean expression
906 is false.
907
908 nested if statements: You can use one if or else if
909 statement inside another if or
910 else if statement(s).
911
912 switch statement: A switch statement allows a variable
913 to be tested for equality against
914 a list of values.
915
916 nested switch statements: You can use one switch statement
917 inside another switch
918 statement(s).
919
920
921[[[[[[]]]]]] if-else statement [[[[[[]]]]]]
922
923
924 An if statement can be followed by an optional else
925 statement, which executes when the Boolean
926 expression is false.
927
928 ---------------------------
929 if(boolean_expression) {
930 /* statement(s) will execute if true */
931 } else {
932 /* statement(s) will execute if false */
933 }
934 ---------------------------
935
936 If the Boolean expression evaluates to true, then the
937 if block will be executed, otherwise, the else block
938 will be executed.
939
940 C programming language assumes any non-zero and non-null
941 values as true, and if it is either zero or null, then
942 it is assumed as false value.
943
944 START
945 |
946 V
947 condition ---------
948 | |
949 V |
950 if condition if condition
951 is true is false
952 | |
953 V V
954 if code else code
955 | |
956 V |
957 END <-------------
958
959
960[[[[[[]]]]]] 'if- else if - else' statement [[[[[[]]]]]]
961
962 An if statement can be followed by an optional
963 else if...else statement, which is very useful to
964 test various conditions using single if...else if
965 statement.
966
967 When using if...else if..else statements, there are
968 few points to keep in mind −
969
970 -An if can have zero or one else's and it
971 must come after any else if's.
972
973 -An if can have zero to many else if's and they
974 must come before the else.
975
976 -Once an else if succeeds, none of the remaining else
977 if's or else's will be tested.
978
979 The syntax of an if...else if...else statement in
980 C programming language is −
981
982 ---------------------------
983 if(boolean_expression 1) {
984 /* Executes when the boolean expression 1 is true */
985 } else if( boolean_expression 2) {
986 /* Executes when the boolean expression 2 is true */
987 } else if( boolean_expression 3) {
988 /* Executes when the boolean expression 3 is true */
989 } else {
990 /* executes when the none of the above condition is true */
991 }
992 ---------------------------
993
994 The flow is as follows
995 START
996 |
997 V
998 condition ---------
999 | |
1000 V |
1001 if condition if condition
1002 is true is false
1003 | |
1004 V |
1005 if code |
1006 | |
1007 | V
1008 | else-if condition --------------
1009 | | |
1010 | V |
1011 | else-if condition else-if condition
1012 | is true is false
1013 | | |
1014 | V |
1015 | else-if code |
1016 | | |
1017 | | |
1018 | | |
1019 | | |
1020 | | |
1021 | | else code
1022 | | |
1023 V | |
1024 END <------------------------------------
1025
1026
1027
1028###############################################################
1029###############################################################
1030###############################################################
1031### Loops #####################################################
1032###############################################################
1033###############################################################
1034###############################################################
1035
1036 You may encounter situations, when a block of code needs
1037 to be executed several number of times. In general,
1038 statements are executed sequentially: The first
1039 statement in a function is executed first, followed
1040 by the second, and so on.
1041
1042 Programming languages provide various control structures
1043 that allow for more complicated execution paths.
1044
1045 A loop statement allows us to execute a statement or group
1046 of statements multiple times. Given below is the general
1047 form of a loop statement in most of the programming languages −
1048
1049 break statement: Terminates the loop or switch
1050 statement and transfers execution
1051 to the statement immediately
1052 following the loop or switch.
1053
1054 continue statement: Causes the loop to skip the
1055 remainder of its body and
1056 immediately retest its condition
1057 prior to reiterating.
1058
1059 goto statement: Transfers control to the labeled
1060 statement.
1061 NOTE − You can terminate an infinite loop by pressing Ctrl + C keys.
1062
1063
1064[[[[[[]]]]]] While loop [[[[[[]]]]]]
1065
1066 A while loop in C programming repeatedly executes
1067 a target statement as long as a given condition is true.
1068
1069 The syntax of a while loop in C programming language is −
1070
1071 ---------------------------
1072 while(condition) {
1073 statement(s);
1074 }
1075 ---------------------------
1076
1077 Here, statement(s) may be a single statement or
1078 a block of statements. The condition may be any
1079 expression, and true is any nonzero value. The
1080 loop iterates while the condition is true.
1081
1082 When the condition becomes false, the program
1083 control passes to the line immediately following
1084 the loop.
1085
1086 START
1087 |
1088 V
1089 ---> condition -----------
1090 | | |
1091 | V |
1092 | if condition if condition
1093 | is true is false
1094 | | |
1095 | V V
1096 ------- code END
1097
1098
1099[[[[[[]]]]]] For loop [[[[[[]]]]]]
1100
1101 A for loop is a repetition control structure that
1102 allows you to efficiently write a loop that needs
1103 to execute a specific number of times.
1104
1105 The syntax of a for loop in C programming language is −
1106
1107 ---------------------------
1108 for ( init; condition; increment ) {
1109 statement(s);
1110 }
1111 ---------------------------
1112
1113 Here is the flow of control in a 'for' loop −
1114
1115 init: The init step is executed first, and only once.
1116 This step allows you to declare and initialize any
1117 loop control variables. You are not required to put
1118 a statement here, as long as a semicolon appears.
1119
1120 condition: Next, the condition is evaluated. If it is
1121 true, the body of the loop is executed. If it is
1122 false, the body of the loop does not execute and
1123 the flow of control jumps to the next statement
1124 just after the 'for' loop.
1125
1126 increment: After the body of the 'for' loop executes, the
1127 flow of control jumps back up to the increment
1128 statement. This statement allows you to update
1129 any loop control variables. This statement can
1130 be left blank, as long as a semicolon appears
1131 after the condition.
1132
1133 The condition is now evaluated again. If it is true, the loop
1134 executes and the process repeats itself (body of loop, then
1135 increment step, and then again condition). After the condition
1136 becomes false, the 'for' loop terminates.
1137
1138
1139 START
1140 |
1141 V
1142 init
1143 |
1144 V
1145 --> condition --------
1146 | | |
1147 | V |
1148 | if condition if condition
1149 | is true is false
1150 | | |
1151 | V |
1152 | code |
1153 | | |
1154 | V |
1155 --- increment |
1156 |
1157 V
1158 END
1159
1160
1161[[[[[[]]]]]] infinite loop [[[[[[]]]]]]
1162
1163 A loop becomes an infinite loop if a condition
1164 never becomes false. The for loop is traditionally
1165 used for this purpose. Since none of the three
1166 expressions that form the 'for' loop are required,
1167 you can make an endless loop by leaving the conditional
1168 expression empty.
1169
1170 ---------------------------
1171 #include <stdio.h>
1172
1173 int main () {
1174
1175 for( ; ; ) {
1176 printf("This loop will run forever.\n");
1177 }
1178
1179 return 0;
1180 }
1181 ---------------------------
1182
1183 When the conditional expression is absent, it is assumed to
1184 be true. You may have an initialization and increment
1185 expression, but C programmers more commonly use the for(;;)
1186 construct to signify an infinite loop.
1187
1188
1189[[[[[[]]]]]] Do while loop [[[[[[]]]]]]
1190
1191 Unlike for and while loops, which test the loop condition
1192 at the top of the loop, the do...while loop in C programming
1193 checks its condition at the bottom of the loop.
1194
1195 A do...while loop is similar to a while loop, except the fact
1196 that it is guaranteed to execute at least one time.
1197
1198 The syntax of a do...while loop in C programming language is −
1199
1200 ---------------------------
1201 do {
1202 statement(s);
1203 } while( condition );
1204 ---------------------------
1205
1206 Notice that the conditional expression appears at the end
1207 of the loop, so the statement(s) in the loop executes once
1208 before the condition is tested.
1209
1210 If the condition is true, the flow of control jumps back
1211 up to do, and the statement(s) in the loop executes again.
1212 This process repeats until the given condition becomes
1213 false.
1214
1215 START
1216 |
1217 V
1218 ------> code
1219 | |
1220 | V
1221 | condition -----------
1222 | | |
1223 | V |
1224 | if condition if condition
1225 | is true is false
1226 | | |
1227 |_________| V
1228 END
1229
1230 -- THIS LINE IS INTENTIONALLY LEFT BLANK --
1231 -- THIS LINE IS INTENTIONALLY LEFT BLANK --
1232 -- THIS LINE IS INTENTIONALLY LEFT BLANK --
1233 -- THIS LINE IS INTENTIONALLY LEFT BLANK --
1234 -- THIS LINE IS INTENTIONALLY LEFT BLANK --
1235 -- THIS LINE IS INTENTIONALLY LEFT BLANK --
1236 -- THIS LINE IS INTENTIONALLY LEFT BLANK --
1237 -- THIS LINE IS INTENTIONALLY LEFT BLANK --