Category: Learn C Online

while Statement in C Programming Language 23

while loop in C programming language

In this article, we will understand the while loop in C programming language. Let us suppose you want to execute a block of statement 5 times. There is one approach using goto statement as shown below. #include<stdio.h> #include<conio.h> void main() { int i=0; clrscr(); label1: i=i+1; printf(“%d This will be repeated 5 times\n”, i); if(i!=5) goto label1; printf(“End of the program”); getch(); } Output: 1 This will be repeated 5 times 2 This will be repeated 5 times 3 This will be repeated 5 times 4 This will be repeated 5 times 5 This will be repeated 5 times End...

break statement in C 17

break statement in C

break statement in C language is the keyword known to the compiler. The break statement is used to break from any kind of loop. Let us understand the use of break statement using an example. Example: for(i=0;i<10;i++) { /*block of statement*/ } Let us suppose that we want to come out of for loop when the value of i equals to 5. The solution to this is using break statement. The problem can be solved using break statement as follows: for(i=0;i<10;i++) { /*block of statement*/ if(i==5) break; } When the break statement in C is executed the control is transferred...

The continue Statement in C Language 25

The continue statement in C Language

Let us understand the continue statement in C programming language. continue statement works similar to break statement in C. Instead of terminating the loop statement, it will force next iteration of the loop. continue statement can be used with for loop, while loop and do-while loop. Sometimes there arises a situation where we don’t want to execute the certain statements within the loop but we want to continue the execution of the loop till the condition is satisfied. This can be done using continue statement. Consider the below example, for(i=0;i<10;i++) { /*statement1*/ /*statement2*/ /*statement3*/ /*statement4*/ } Let us suppose, when the...

The goto Keyword in C Language 16

goto statement in C Language

goto statement in C programming language is used for unconditional jump from one part of the program to another part of the program. It is always suggested not to use goto statement as this reduces the readability of the program. Using goto statement in C programming language is considered as poor programming approach. The goto statement consists of two parts: label and goto keyword. Example: /*use of goto statement*/ #include<stdio.h.> #include<conio.h> void main(){ int a; goto label; a = 10; printf(“%d”, a); label: a = 20; printf(“%d”, a); } Output: 20 When goto label is encountered, control goes to the...

switch statement in C 40

switch statement in C

The switch statement in C is very powerful decision making statement. It reduces the complexity of the program. Hence increases the readability of the program. Switch statement accepts single input from the user and based on that input executes a particular block of statements. Syntax of switch statement in C: switch(expression){ case constant 1: perform this; case constant 2: perform this; case constant 3: perform this; case constant 4: perform this; . . . default: perform this; } Control passes to the statement whose case constant-expression matches the value of switch ( expression ). The switch statement can include any...

Conditional operator in C 49

Conditional operator in C

Conditional operator in C is also known as ternary operator. It is called ternary operator because it takes three arguments. It evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false. Syntax: condition ? result1 : result2; If the condition is true, result1 is returned else result2 is returned. Examples: 10==5 ? 11: 12; // returns 12, since 10 not equal to 5. 10!=5 ? 4 : 3; // returns 4, since 10 not equal to 5. 12>8 ? a : b; // returns the value of a, since...

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet