C Programming Part 7
C Programming Part 7
{
true statement;
}
else
{
false statement;
}
statement-x;
If the condition is true , then the true statement and statement-x will be executed and if the
condition is false, then the false statement and statement-x is executed.
Or
If test expression is true, codes inside the body of if statement is executed and, codes inside the
body of else statement is skipped.
If test expression is false, codes inside the body of else statement is executed and, codes inside
the body of if statement is skipped.
Flowchart
Example:
// Program to check whether an integer entered by the user is odd or even
#include <stdio.h>
int main()
{
C PROGRAMMING Page 61
int number;
printf("Enter an integer: ");
scanf("%d",&number);
// True if remainder is 0
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
Output
Enter an integer: 7
7 is an odd integer.
Syntax : if(condition-1)
{ {
if (condition-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
C PROGRAMMING Page 62
}
statement-x;
If the condition-1 is false, the statement-3 and statement-x will be executed. Otherwise it
continues to perform the second test. If the condition-2 is true, the true statement-1 will be
executed otherwise the statement-2 will be executed and then the control is transferred to the
statement-x
Flowchart
Example
#include<stdio.h>
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 !=var2)
{
printf("var1 is not equal to var2");
//Below – if-else is nested inside another if block
if (var1 >var2)
{
printf("var1 is greater than var2");
}
else
{
printf("var2 is greater than var1");
}
}
else
C PROGRAMMING Page 63
{
printf("var1 is equal to var2");
}
…
Else if ladder.
The if else-if statement is used to execute one code from multiple conditions.
Syntax : if( condition-1)
{
statement-1;
}
else if(condition-2)
{
statement-2;
}
else if(condition-3)
{
statement-3;
}
else if(condition-n)
{
statement-n;
}
else
{
default-statement;
}
statement-x;
Flowchart
C PROGRAMMING Page 64
Example
#include<stdio.h>
#include<conio.h>
void main(){
int number=0;
clrscr();
printf("enter a number:");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
}
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
getch();
}
C PROGRAMMING Page 65
Points to Remember
1. In if statement, a single statement can be included without enclosing it into curly braces { }
2. int a = 5;
3. if(a > 4)
4. printf("success");
No curly braces are required in the above case, but if we have more than one statement
5. == must be used for comparison in the expression of if condition, if you use = the expression will
8. printf("hello");
Switch statement : when there are several options and we have to choose only one option
from the available ones, we can use switch statement. Depending on the selected option, a
particular task can be performed. A task represents one or more statements.
Syntax:
switch(expression)
{
case value-1:
statement/block-1;
break;
case value-2:
statement/block t-2;
break;
case value-3:
statement/block -3;
break;
case value-4:
statement/block -4;
break;
default:
default- statement/block t;
break;
C PROGRAMMING Page 66
}
The expression following the keyword switch in any „C‟ expression that must yield an integer
value. It must be ab integer constants like 1,2,3 .
The keyword case is followed by an integer or a character constant, each constant in each
must be different from all the other.
First the integer expression following the keyword switch is evaluated. The value it gives
is searched against the constant values that follw the case statements. When a match is found, the
program executes the statements following the case. If no match is found with any of the case
statements, then the statements follwing the default are executed.
Example
#include<stdio.h>
main()
{
int a;
printf("Please enter a no between 1 and 5: ");
scanf("%d",&a);
switch(a)
{
case 1:
printf("You chose One");
break;
case 2:
C PROGRAMMING Page 67
printf("You chose Two");
break;
case 3:
printf("You chose Three");
break;
case 4:
printf("You chose Four");
break;
case 5: printf("You chose Five.");
break;
default :
printf("Invalid Choice. Enter a no between 1 and 5"); break;
}
}
Flowchart
C PROGRAMMING Page 68
Points to Remember
It isn't necessary to use break after each block, but if you do not use it, all the consecutive block
1. int i = 1;
2. switch(i)
3. {
4. case 1:
5. printf("A"); // No break
6. case 2:
7. printf("B"); // No break
8. case 3:
9. printf("C");
10. break;
11. }
Output : A B C
The output was supposed to be only A because only the first case matches, but as there is no
break statement after the block, the next blocks are executed, until the cursor encounters a
break.
default case can be placed anywhere in the switch case. Even if we don't include the default case
How it Works
C PROGRAMMING Page 69
A sequence of statements are executed until a specified condition is true. This sequence of
statements to be executed is kept inside the curly braces { } known as the Loop body. After
every execution of loop body, condition is verified, and if it is found to be true the loop body is
executed again. When the condition check returns false, the loop body is not executed.
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/repeat a code or group of code many times.
Or Looping means a group of statements are executed repeatedly, until some logical condition
is satisfied.
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.
C PROGRAMMING Page 70