C if else Statement
The if statement in C language is used to perform operation on the basis of condition. By using if-else statement, you can perform operation either condition is true or false.
There are many ways to use if statement in C language:
- If statement
- If-else statement
- If else-if ladder
- Nested if
If Statement
The single if statement in C language is used to execute the code if condition is true. The syntax of if statement is given below:
Flowchart of if statement in C
Let's see a simple example of c language if statement.
Output
enter a number:4 4 is even number
enter a number:5
If-else Statement
The if-else statement in C language is used to execute the code if condition is true or false. The syntax of if-else statement is given below:
Flowchart of if-else statement in C
Let's see the simple example of even and odd number using if-else statement in C language.
Output
enter a number:4 4 is even number
enter a number:5 5 is odd number
If else-if ladder Statement
The if else-if statement is used to execute one code from multiple conditions. The syntax of if else-if statement is given below:
Flowchart of else-if ladder statement in C
The example of if-else-if statement in C language is given below.
Output
enter a number:4 number is not equal to 10, 50 or 100
enter a number:50 number is equal to 50
C Switch Statement
The switch statement in C language is used to execute the code from multiple conditions. It is like if else-if ladder statement.
The syntax of switch statement in c language is given below:
Rules for switch statement in C language
1) The switch expression must be of integer or character type.
2) The case value must be integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break statement found in switch case, all the cases will be executed after matching the case value. It is known as fall through state of C switch statement.
Let's try to understand it by the examples. We are assuming there are following variables.
Valid Switch | Invalid Switch | Valid Case | Invalid Case |
---|---|---|---|
switch(x) | switch(f) | case 3; | case 2.5; |
switch(x>y) | switch(x+2.5) | case 'a'; | case x; |
switch(a+b-2) | case 1+2; | case x+2; | |
switch(func(x,y)) | case 'x'>'y'; | case 1,2,3; |
Flowchart of switch statement in C
Let's see a simple example of c language switch statement.
Output
enter a number:4 number is not equal to 10, 50 or 100
enter a number:50 number is equal to 50
C Switch statement is fall-through
In C language, switch statement is fall through, it means if you don't use break statement in switch case, all the case after matching case will be executed.
Let's try to understand the fall through state of switch statement by the example given below.
Output
enter a number:10 number is equals to 10 number is equals to 50 number is equals to 100 number is not equal to 10, 50 or 100
enter a number:50 number is equal to 50 number is equals to 100 number is not equal to 10, 50 or 100
C Loops
The loops in C language are used to execute a block of code or a part of the program several times.
In other words, it iterates a code or group of code many times.
Why use loops in C language?
Suppose that you have to print table of 2, then you need to write 10 lines of code.
By using the loop statement, you can do it by 2 or 3 lines of code only.
Advantage of loops in C
1) It saves code.
2) It helps to traverse the elements of array (which is covered in next pages).
Types of C Loops
There are three types of loops in C language that is given below:
- do while
- while
- for
do-while loop in C
It iterates the code until condition is false. Here, condition is given after the code. So at least once, code is executed whether condition is true or false.
It is better if you have to execute the code at least once.
The syntax of do-while loop in c language is given below:
while loop in C
Like do while, it iterates the code until condition is false. Here, condition is given before the code. So code may be executed 0 or more times.
It is better if number of iteration is not known by the user.
The syntax of while loop in c language is given below:
for loop in C
Like while, it iterates the code until condition is false. Here, initialization, condition and increment/decrement is given before the code. So code may be executed 0 or more times.
It is good if number of iteration is known by the user.
The syntax of for loop in c language is given below:
do while loop in C
To execute a part of program or code several times, we can use do-while loop of C language. The code given between the do and while block will be executed until condition is true.
In do while loop, statement is given before the condition, so statement or code will be executed at lease one time. In other words, we can say it is executed 1 or more times.
It is better if you have to execute the code at least once.
do while loop syntax
The syntax of C language do-while loop is given below:
Flowchart of do while loop
do while example
There is given the simple program of c language do while loop where we are printing the table of 1.
Output
1 2 3 4 5 6 7 8 9 10
Program to print table for the given number using do while loop
Output
Enter a number: 5 5 10 15 20 25 30 35 40 45 50
Enter a number: 10 10 20 30 40 50 60 70 80 90 100
Infinitive do while loop
If you pass 1 as a expression in do while loop, it will run infinite number of times.
while loop in C
The while loop in C language is used to iterate the part of program or statements many times.
In while loop, condition is given before the statement. So it is different from the do while loop. It can execute the statements 0 or more times.
When use while loop in C
The C language while loop should be used if number of iteration is uncertain or unknown.
Syntax of while loop in C language
The syntax of while loop in c language is given below:
Flowchart of while loop in C
Example of while loop in C language
Let's see the simple program of while loop that prints table of 1.
Output
1 2 3 4 5 6 7 8 9 10
Program to print table for the given number using while loop in C
Output
Enter a number: 50 50 100 150 200 250 300 350 400 450 500
Enter a number: 100 100 200 300 400 500 600 700 800 900 1000
Infinitive while loop in C
If you pass 1 as a expression in while loop, it will run infinite number of times.
for loop in C
The for loop in C language is also used to iterate the statement or a part of the program several times, like while and do-while loop.
But, we can initialize and increment or decrement the variable also at the time of checking the condition in for loop.
Unlike do while loop, the condition or expression in for loop is given before the statement, so it may execute the statement 0 or more times.
When use for loop in C
For loop is better if number of iteration is known by the programmer.
Syntax of for loop in C
The syntax of for loop in c language is given below:
Flowchart of for loop in C
Example of for loop in C language
Let's see the simple program of for loop that prints table of 1.
Output
1 2 3 4 5 6 7 8 9 10
C Program : Print table for the given number using C for loop
Output
Enter a number: 2 2 4 6 8 10 12 14 16 18 20
Enter a number: 1000 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
Infinitive for loop in C
If you don't initialize any variable, check condition and increment or decrement variable in for loop, it is known as infinitive for loop.
In other words, if you place 2 semicolons in for loop, it is known as infinitive for loop.
If you run this program, you will see above statement infinite times.
C break statement
The break statement in C language is used to break the execution of loop (while, do while and for) and switch case.
In case of inner loops, it terminates the control of inner loop only.
There can be two usage of C break keyword:
- With switch case
- With loop
Syntax:
The jump statement in c break syntax can be while loop, do while loop, for loop or switch case.
Flowchart of break in c
Example of C break statement with switch case
Click here to see the example of C break with switch statement.
Example of C break statement with loop
Output
1 2 3 4 5
As you can see on console output, loop from 1 to 10 is not printed after i==5.
C break statement with inner loop
In such case, it breaks only inner loop, but not outer loop.
Output
1 1 1 2 1 3 2 1 2 2 3 1 3 2 3 3
As you can see the output on console, 2 3 is not printed because there is break statement after printing i==2 and j==2. But 3 1, 3 2 and 3 3 is printed because break statement works for inner loop only.
C continue statement
The continue statement in C language is used to continue the execution of loop (while, do while and for). It is used with if condition within the loop.
In case of inner loops, it continues the control of inner loop only.
Syntax:
The jump statement can be while, do while and for loop.
Example of continue statement in c
Output
1 2 3 4 6 7 8 9 10
As you can see, 5 is not printed on the console because loop is continued at i==5.
C continue statement with inner loop
In such case, C continue statement continues only inner loop, but not outer loop.
Output
1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3
As you can see, 2 2 is not printed on the console because inner loop is continued at i==2 and j==2.
C goto statement
The goto statement is known as jump statement in C language. It is used to unconditionally jump to other label. It transfers control to other parts of the program.
It is rarely used today because it makes program less readable and complex.
Syntax:
goto example
Let's see a simple example to use goto statement in C language.
Output:
You are not eligible to vote! Enter you age: 11 You are not eligible to vote! Enter you age: 44 You are eligible to vote!
Type Casting in C
Type casting allows us to convert one data type into other. In C language, we use cast operator for type casting which is denoted by (type).
Syntax:
Note: It is always recommended to convert lower value to higher for avoiding data loss.
Without Type Casting:
With Type Casting:
Type Casting example
Let's see a simple example to cast int value into float.
Output:
f : 2.250000
By R.T.Reddy
#include
ReplyDelete#include
void main(){
int number=0;
clrscr();
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
getch();
}
what happens if we dont write (,number) in printf