C Control Statements
C Control Statements
C if else Statement
The if-else statement in C is used to perform the
operations based on some specific condition. The
operations specified in if block is executed if and only if
the given condition is true.
o If statement
o If-else statement
o If else-if ladder
o Nested if
If Statement
The if statement is used to check some given condition
and perform some operations depending upon the
correctness of that condition. It is mostly used in the
scenario where we need to perform the different
operations for the different conditions. The syntax of the
if statement is given below.
1. if(expression){
2. //code to be executed
3. }
Output
Enter a number:4
4 is even number
enter a number:5
#include<stdio.h>
main()
{
float a, b, c, big;
printf(“Input a b c\n”);
scanf(“%f %f %f”, &a, &b, &c);
/* Computing the largest of three numbers*/
big=a;
if(b>big) big=b;
if(c>big) big=c;
printf(“Largest of the three numbers=%7.2f
\n”,big);
}
Run 1:
Input a b c
10.00 20.00 45.00
Largest of the three numbers=45.00
Run 2:
Input a b c
78.00 43.00 23.00
Largest of the three numbers=78.00
Alternative Approach to find the largest of
three numbers
1. #include <stdio.h>
2. int main()
3. {
4. int a, b, c;
5. printf("Enter three numbers?");
6. scanf("%d %d %d",&a,&b,&c);
7. if(a>b && a>c)
8. {
9. printf("%d is largest",a);
10. }
11. if(b>a && b > c)
12. {
13. printf("%d is largest",b);
14. }
15. if(c>a && c>b)
16. {
17. printf("%d is largest",c);
18. }
19. if(a == b && a == c)
20. {
21. printf("All are equal");
22. }
23. }
Output
Enter three numbers?
12 23 34
34 is largest
If-else Statement
The if-else statement is used to perform two operations for a
single condition. The if-else statement is an extension to the if
statement using which, we can perform two different
operations, i.e., one is for the correctness of that condition, and
the other is for the incorrectness of the condition. Here, we must
notice that if and else block cannot be executed simultaneously.
Using if-else statement is always preferable since it always
invokes an otherwise case with every if condition. The syntax
of the if-else statement is given below.
1. if(expression){
2. //code to be executed if condition is true
3. }else{
4. //code to be executed if condition is false
5. }
Program to check if a number is EVEN or ODD.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number%2==0){
7. printf("%d is even number",number);
8. }
9. else{
10. printf("%d is odd number",number);
11. }
12. return 0;
13. }
Output
enter a number:4
4 is even number
enter a number:5
5 is odd number
Output
Enter your age?18
You are eligible to vote...
Enter your age?13
Sorry ... you can't vote
1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are f
alse
12. }
The example of an if-else-if statement in C language is
given below.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number==10){
7. printf("number is equals to 10");
8. }
9. else if(number==50){
10. printf("number is equal to 50");
11. }
12. else if(number==100){
13. printf("number is equal to 100");
14. }
15. else{
16. printf("number is not equal to 10, 50 or 100");
17. }
18. return 0;
19. }
Output
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
Output
Enter your marks?10
Sorry you are fail ...
Enter your marks?40
You scored grade C ...
Enter your marks?90
Congrats ! you scored grade A ...
C Switch Statement
The switch statement in C is an alternate to if-else-if
ladder statement which allows us to execute multiple
operations for the different possibles values of a single
variable called switch variable. Here, We can define
various statements in the multiple cases for the different
values of a single variable.
The syntax of switch statement is given below:
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
11. code to be executed if all cases are not matche
d;
12. }
1. int x,y,z;
2. char a,b;
3. float f;
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. switch(number){
7. case 10:
8. printf("number is equals to 10");
9. break;
10. case 50:
11. printf("number is equal to 50");
12. break;
13. case 100:
14. printf("number is equal to 100");
15. break;
16. default:
17. printf("number is not equal to 10, 50 or 100");
18. }
19. return 0;
20. }
Output
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
Output
hi
C Switch statement is fall-through
In C language, the switch statement is fall through; it
means if you don't use a break statement in the switch
case, all the cases after the matching case will be
executed.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4.
5. printf("enter a number:");
6. scanf("%d",&number);
7.
8. switch(number){
9. case 10:
10. printf("number is equal to 10\n");
11. case 50:
12. printf("number is equal to 50\n");
13. case 100:
14. printf("number is equal to 100\n");
15. default:
16. printf("number is not equal to 10, 50 or 100");
17. }
18. return 0;
19. }
Output
enter a number:10
number is equal to 10
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
Output
enter a number:50
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
1. #include <stdio.h>
2. int main () {
3.
4. int i = 10;
5. int j = 20;
6.
7. switch(i) {
8.
9. case 10:
10. printf("the value of i evaluated in outer sw
itch: %d\n",i);
11. case 20:
12. switch(j) {
13. case 20:
14. printf("The value of j evaluated in nest
ed switch: %d\n",j);
15. }
16. }
17.
18. printf("Exact value of i is : %d\n", i );
19. printf("Exact value of j is : %d\n", j );
20.
21. return 0;
22. }
Output
the value of i evaluated in outer switch:
10
The value of j evaluated in nested switch:
20
Exact value of i is : 10
Exact value of j is : 20
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. switch(number){
7. case 10:
8. printf("number is equals to 10");
9. break;
10. case 50:
11. printf("number is equal to 50");
12. break;
13. case 100:
14. printf("number is equal to 100");
15. break;
16. default:
17. printf("number is not equal to 10, 50 or 100");
18. }
19. return 0;
20. }
Output
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
Output
hi
C Switch statement is fall-through
Output
enter a number:10
number is equal to 10
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
Output
enter a number:50
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
Nested switch case statement
We can use as many switch statement as we want inside a
switch statement. Such type of statements is called nested
switch case statements. Consider the following example.
1. #include <stdio.h>
2. int main () {
3.
4. int i = 10;
5. int j = 20;
6.
7. switch(i) {
8.
9. case 10:
10. printf("the value of i evaluated in outer switch:
%d\n",i);
11. case 20:
12. switch(j) {
13. case 20:
14. printf("The value of j evaluated in nested s
witch: %d\n",j);
15. }
16. }
17.
18. printf("Exact value of i is : %d\n", i );
19. printf("Exact value of j is : %d\n", j );
20.
21. return 0;
22. }
Output
the value of i evaluated in outer switch:
10
The value of j evaluated in nested switch:
20
Exact value of i is : 10
Exact value of j is : 20
if-else vs switch
1. if(expression)
2. {
3. // statements;
4. }
5. else
6. {
7. // statements;
8. }
What is a switch statement?
1. switch(expression)
2. {
3. case constant 1:
4. // statements;
5. break;
6. case constant 2:
7. // statements;
8. break;
9. case constant n:
10. // statements;
11. break;
12. default:
13. // statements;
14. }
Similarity b/w if-else and switch
o Definition
if-else
Based on the result of the expression in the 'if-else' statement,
the block of statements will be executed. If the condition is
true, then the 'if' block will be executed otherwise 'else' block
will execute.
Switch statement
o Expression
If-else
It can contain a single expression or multiple expressions for
multiple choices. In this, an expression is evaluated based on
the range of values or conditions. It checks both equality and
logical expressions.
Switch
It contains only a single expression, and this expression is
either a single integer object or a string object. It checks only
equality expression.
o Evaluation
If-else
An if-else statement can evaluate almost all the types of data
such as integer, floating-point, character, pointer, or Boolean.
Switch
A switch statement can evaluate either an integer or a
character.
o Sequence of Execution
If-else
Switch
o Default Execution
If-else
Switch
o Values
If-else
Switch
o Use
If-else
Switch
o Editing
If-else
Switch
o Speed
If-else
Switch
If-else switch
C Loops
The looping can be defined as repeating the same
process multiple times until a specific condition satisfies.
There are three types of loops used in the C language.
Here, we are going to learn all the aspects of C loops.
Advantage of loops in C
1) It provides code reusability.
Types of C Loops
There are three types of loops in C Program that is given
below:
1. do while
2. while
3. for
do-while loop in C
1. do{
2. //code to be executed
3. }while(condition);
while loop in C
The while loop in c is to be used in the scenario where we
don't know the number of iterations in advance. The
block of statements is executed in the while loop until the
condition specified in the while loop is satisfied. It is also
called a pre-tested loop.
1. while(condition){
2. //code to be executed
3. }
for loop in C
The for loop is used in the case where we need to execute
some part of the code until the given condition is
satisfied. The for loop is also called as a per-tested loop.
It is better to use for loop if the number of iteration is
known in advance.
Example 1
#include<stdio.h>
void main ()
{
int i;
for(i=1;i<=10;i++)
printf(“%i”, 5*i);
}
Output
5 10 15 20 25 30 35 40 45 50
{
statement;
….//Compound statement body
statement;
} //No semicolon here
iii. for(j=0;j<25;j++);// Loop with no body
v. for(;j<25;j++)
printf(“%d\n”, j); // Initialization expression not
used
vi. for(;;j++)
printf(“%d\n”, j); // Initialization and test
expression not used
vii. for(;;)
printf(“ I cannot stop.”); // Initialization, test and
update expression not used
do while loop in C
The do while loop is a post tested loop. Using the do-
while loop, we can repeat the execution of several parts
of the statements. The do-while loop is mainly used in the
case where we need to execute the loop at least once. The
do-while loop is mostly used in menu-driven programs
where the termination condition depends upon the end
user.
do while loop syntax
The syntax of the C language do-while loop is given
below:
1. do{
2. //code to be executed
3. }while(condition);
i. do // No semicolon here
statement; // Single statement body
while(n<100); // Semicolon here
Example 1
1. #include<stdio.h>
2. #include<stdlib.h>
3. void main ()
4. {
5. char c;
6. int choice,dummy;
7. do{
8. printf("\n1. Print Hello\n2. Print C
Programme\n3. Exit\n");
9. scanf("%d",&choice);
10. switch(choice)
11. {
12. case 1 :
13. printf("Hello");
14. break;
15. case 2:
16. printf("C Programme ");
17. break;
18. case 3:
19. exit(0);
20. break;
21. default:
22. printf("please enter valid choice");
23. }
24. printf("do you want to enter more?");
25. scanf("%d",&dummy);
26. scanf("%c",&c);
27. }while(c=='y');
28. }
Output
1. Print Hello
2. Print C Programme
3. Exit
1
Hello
do you want to enter more?
y
1. Print Hello
2. Print C Programme
3. Exit
2
C Programme
do you want to enter more?
n
do while example
1. do{
2. //statement
3. }while(1);
while loop in C
While loop is also known as a pre-tested loop. In general, a
while loop allows a part of the code to be executed multiple
times depending upon a given boolean condition. It can be
viewed as a repeating if statement. The while loop is mostly
used in the case where the number of iterations is known in
advance.
Syntax of while loop in C language
The syntax of while loop in c language is given below:
1. while(condition){
2. //code to be executed
3. }
Output
1
2
3
4
5
6
7
8
9
10
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
1. while(1){
2. //statement
3. }
for loop in C
The for loop in C language is used to iterate the
statements or a part of the program several times. It is
frequently used to traverse the data structures like the
array and linked list.
Syntax of for loop in C
1. #include<stdio.h>
2. int main(){
3. int i=0;
4. for(i=1;i<=10;i++){
5. printf("%d \n",i);
6. }
7. return 0;
8. }
Output
1
2
3
4
5
6
7
8
9
10
Output
Sum of first 7 positive even numbers=56
Sum of their squares=560
C Program: Print table for the given number using C
for loop
14. #include<stdio.h>
15. int main(){
16. int i=1,number=0;
17. printf("Enter a number: ");
18. scanf("%d",&number);
19. for(i=1;i<=10;i++){
20. printf("%d \n",(number*i));
21. }
22. return 0;
23. }
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
Example 3
Output
RUN1
Enter the number: 1234
Sum of the digits of the number=10
Reverse of the number=4321
The number is not a palindrome
RUN2
Enter the number: 1221
Sum of the digits of the number=6
Reverse of the number=1221
The number is a palindrome
Properties of Expression 1
o The expression represents the initialization of the
loop variable.
o We can initialize more than one variable in
Expression 1.
o Expression 1 is optional.
Example 1
1. #include <stdio.h>
2. int main()
3. {
4. int a,b,c;
5. for(a=0,b=12,c=23;a<2;a++)
6. {
7. printf("%d ",a+b+c);
8. }
9. }
Output
35 36
Example 2
1. #include <stdio.h>
2. int main()
3. {
4. int i=1;
5. for(;i<5;i++)
6. {
7. printf("%d ",i);
8. }
9. }
Output
1 2 3 4
Properties of Expression 2
o Expression 2 is a conditional expression. It checks for
Example 1
1. #include <stdio.h>
2. int main()
3. {
4. int i;
5. for(i=0;i<=4;i++)
6. {
7. printf("%d ",i);
8. }
9. }
Output
0 1 2 3 4
Example 2
1. #include <stdio.h>
2. int main()
3. {
4. int i,j,k;
5. for(i=0,j=0,k=0;i<4,k<8,j<10;i++)
6. {
7. printf("%d %d %d\n",i,j,k);
8. j+=2;
9. k+=3;
10. }
11. }
Output
0 0 0
1 2 3
2 4 6
3 6 9
4 8 12
Example 3
1. #include <stdio.h>
2. int main()
3. {
4. int i;
5. for(i=0;;i++)
6. {
7. printf("%d",i);
8. }
9. }
Output
infinite loop
Properties of Expression 3
o Expression 3 is used to update the loop variable.
o We can update more than one variable at the same
time.
o Expression 3 is optional.
Example 1
1. #include<stdio.h>
2. void main ()
3. {
4. int i=0,j=2;
5. for(i = 0;i<5;i++,j=j+2)
6. {
7. printf("%d %d\n",i,j);
8. }
9. }
Output
0 2
1 4
2 6
3 8
4 10
Loop body
The braces {} are used to define the scope of the loop.
However, if the loop contains only one statement, then
we don't need to use braces. A loop without a body is
possible. The braces work as a block separator, i.e., the
value variable declared inside for loop is valid only for that
block and not outside. Consider the following example.
1. #include<stdio.h>
2. void main ()
3. {
4. int i;
5. for(i=0;i<10;i++)
6. {
7. int i = 20;
8. printf("%d ",i);
9. }
10. }
Output
20 20 20 20 20 20 20 20 20 20
1. #include<stdio.h>
2. void main ()
3. {
4. for(;;)
5. {
6. printf("welcome to C Programming");
7. }
8. }
Nested Loops in C
C supports nesting of loops in C. Nesting of loops is the
feature in C that allows the looping of statements inside
another loop. Let's observe an example of nesting loops in C.
Any number of loops can be defined inside another loop, i.e.,
there is no restriction for defining any number of loops. The
nesting level can be defined at n times. You can define any
type of loop inside another loop; for example, you can define
'while' loop inside a 'for' loop.
1. #include <stdio.h>
2. int main()
3. {
4. int n;// variable declaration
5. printf("Enter the value of n :");
6. // Displaying the n tables.
7. for(int i=1;i<=n;i++) // outer loop
8. {
9. for(int j=1;j<=10;j++) // inner loop
10. {
11. printf("%d\t",(i*j)); // printing the value.
12. }
13. printf("\n");
14. }
Explanation of the above code
Output:
1. while(condition)
2. {
3. while(condition)
4. {
5. // inner loop statements.
6. }
7. // outer loop statements.
8. }
1. #include <stdio.h>
2. int main()
3. {
4. int rows; // variable declaration
5. int columns; // variable declaration
6. int k=1; // variable initialization
7. printf("Enter the number of rows :"); // input the n
umber of rows.
8. scanf("%d",&rows);
9. printf("\nEnter the number of columns :"); // input
the number of columns.
10. scanf("%d",&columns);
11. int a[rows][columns]; //2d array declaration
Output:
1. do
2. {
3. do
4. {
5. // inner loop statements.
6. }while(condition);
7. // outer loop statements.
8. }while(condition);
1. #include <stdio.h>
2. int main()
3. {
4. /*printing the pattern
5. ********
6. ********
7. ********
8. ******** */
9. int i=1;
10. do // outer loop
11. {
12. int j=1;
13. do // inner loop
14. {
15. printf("*");
16. j++;
17. }while(j<=8);
18. printf("\n");
19. i++;
20. }while(i<=4);
21. }
Output:
Explanation of the above code.
1. for(; ;)
2. {
3. // body of the for loop.
4. }
1. #include <stdio.h>
2. int main()
3. {
4. for(;;)
5. {
6. printf("Hello C Programme");
7. }
8. return 0;
9. }
Output
while loop
1. while(1)
2. {
3. // body of the loop..
4. }
1. #include <stdio.h>
2. int main()
3. {
4. int i=0;
5. while(1)
6. {
7. i++;
8. printf("i is :%d",i);
9. }
10. return 0;
11. }
Output
do..while loop
1. do
2. {
3. // body of the loop..
4. }while(1);
goto statement
1. infinite_loop;
2. // body statements.
3. goto infinite_loop;
Macros
Output
Till now, we have seen various ways to define an infinite
loop. However, we need some approach to come out of
the infinite loop. In order to come out of the infinite loop,
we can use the break statement.
1. #include <stdio.h>
2. int main()
3. {
4. char ch;
5. while(1)
6. {
7. ch=getchar();
8. if(ch=='n')
9. {
10. break;
11. }
12. printf("hello");
13. }
14. return 0;
15. }
1. #include <stdio.h>
2. int main()
3. {
4. int i=1;
5. while(i<=10);
6. {
7. printf("%d", i);
8. i++;
9. }
10. return 0;
11. }
1. #include <stdio.h>
2. int main()
3. {
4. char ch='n';
5. while(ch='y')
6. {
7. printf("hello");
8. }
9. return 0;
10. }
The above code will execute the 'for loop' infinite number
of times. As we put the condition (i>=1), which will always
be true for every condition, it means that "hello" will be
printed infinitely.
1. #include <stdio.h>
2. int main()
3. {
4. while(1)
5. {
6. for(int i=1;i<=10;i++)
7. {
8. if(i%2==0)
9. {
10. break;
11. }
12. }
13. }
14. return 0;
15. }
1. #include <stdio.h>
2. int main()
3. {
4. float x = 3.0;
5. while (x != 4.0) {
6. printf("x = %f\n", x);
7. x += 0.1;
8. }
9. return 0;
10. }
In the above code, the loop will run infinite times as the
computer represents a floating-point value as a real value.
The computer will represent the value of 4.0 as 3.999999
or 4.000001, so the condition (x !=4.0) will never be false.
The solution to this problem is to write the condition as
(k<=4.0).
C break statement
The break is a keyword in C which is used to bring the
program control out of the loop. The break statement is
used inside loops or switch statement. The break
statement breaks the loop one by one, i.e., in the case of
nested loops, it breaks the inner loop first and then
proceeds to outer loops. The break statement in C can be
used in the following two scenarios:
Example
1. #include<stdio.h>
2. #include<stdlib.h>
3. void main ()
4. {
5. int i;
6. for(i = 0; i<10; i++)
7. {
8. printf("%d ",i);
9. if(i == 5)
10. break;
11. }
12. printf("came outside of loop i = %d",i);
13.
14. }
Output
0 1 2 3 4 5 came outside of loop i = 5
Example of C break statement with switch case
1. #include<stdio.h>
2. int main(){
3. int i=1,j=1;//initializing a local variable
4. for(i=1;i<=3;i++){
5. for(j=1;j<=3;j++){
6. printf("%d &d\n",i,j);
7. if(i==2 && j==2){
8. break;//will break loop of j only
9. }
10. }//end of for loop
11. return 0;
12. }
Output
1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
1. #include<stdio.h>
2. void main ()
3. {
4. int i = 0;
5. while(1)
6. {
7. printf("%d ",i);
8. i++;
9. if(i == 10)
10. break;
11. }
12. printf("came out of while loop");
13. }
Output
0 1 2 3 4 5 6 7 8 9 came out of
while loop
1. #include<stdio.h>
2. void main ()
3. {
4. int n=2,i,choice;
5. do
6. {
7. i=1;
8. while(i<=10)
9. {
10. printf("%d X %d = %d\n",n,i,n*i);
11. i++;
12. }
13. printf("do you want to continue with the ta
ble of %d , enter any non-
zero value to continue.",n+1);
14. scanf("%d",&choice);
15. if(choice == 0)
16. {
17. break;
18. }
19. n++;
20. }while(1);
21. }
Output
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
do you want to continue with the table of
3 , enter any non-zero value to continue.1
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30
do you want to continue with the table of
4 , enter any non-zero value to continue.0
C continue statement
The continue statement in C language is used to bring the
program control to the beginning of the loop. The continue
statement skips some lines of code inside the loop and
continues with the next iteration. It is mainly used for a
condition so that we can skip some code for a particular
condition.
Syntax:
1. //loop statements
2. continue;
3. //some lines of the code which is to be skipped
Continue statement example 1
1. #include<stdio.h>
2. void main ()
3. {
4. int i = 0;
5. while(i!=10)
6. {
7. printf("%d", i);
8. continue;
9. i++;
10. }
11. }
Output
infinite loop
Output
1
2
3
4
6
7
8
9
10
1. #include<stdio.h>
2. int main(){
3. int i=1,j=1;//initializing a local variable
4. for(i=1;i<=3;i++){
5. for(j=1;j<=3;j++){
6. if(i==2 && j==2){
7. continue;//will continue loop of j only
8. }
9. printf("%d %d\n",i,j);
10. }
11. }//end of for loop
12. return 0;
13. }
Output
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
C goto statement
The goto statement is known as jump statement in C. As the
name suggests, goto is used to transfer the program control to
a predefined label. The goto statment can be used to repeat
some part of the code for a particular condition. It can also be
used to break the multiple loops which can't be done by using
a single break statement. However, using goto is avoided
these days since it makes the program less readable and
complicated.
Syntax:
1. label:
2. //some part of the code;
3. goto label;
goto example
Let's see a simple example to use goto statement in C
language.
1. #include <stdio.h>
2. int main()
3. {
4. int num,i=1;
5. printf("Enter the number whose table you want to print?
");
6. scanf("%d",&num);
7. table:
8. printf("%d x %d = %d\n",num,i,num*i);
9. i++;
10. if(i<=10)
11. goto table;
12. }
Output:
Enter the number whose table you want to
print?10
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100
1. #include <stdio.h>
2. int main()
3. {
4. int i, j, k;
5. for(i=0;i<10;i++)
6. {
7. for(j=0;j<5;j++)
8. {
9. for(k=0;k<3;k++)
10. {
11. printf("%d %d %d\n",i,j,k);
12. if(j == 3)
13. {
14. goto out;
15. }
16. }
17. }
18. }
19. out:
20. printf("came out of the loop");
21. }
0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
0 3 0
came out of the loop
Type Casting in C
Typecasting allows us to convert one data type into other.
In C language, we use cast operator for typecasting which
is denoted by (type).
Syntax:
1. (type)value;
Note: It is always recommended to convert the lower
value to higher for avoiding data loss.
1. int f= 9/4;
2. printf("f : %d\n", f );//Output: 2
1. #include<stdio.h>
2. int main(){
3. float f= (float)9/4;
4. printf("f : %f\n", f );
5. return 0;
6. }
Output:
f : 2.250000