Unit 1 (Third)
Unit 1 (Third)
(Third One)
• {
• if( expression1 ) if(a<b)
• {
• statement block1;
• }
• else
• {
• statement block2;
• }
• }
• else
• {
• statement block3;
• }
• #include <stdio.h>
• void main( )
• {
• int a, b, c;
• printf("Enter 3 numbers...");
• scanf("%d%d%d",&a, &b, &c);
• if(a > b)
• {
• if(a > c)
• {
• printf("a is the greatest");
• }
• else
• {
• printf("c is the greatest");
• }
• }
• else
• {
• if(b > c)
• {
• printf("b is the greatest");
• }
• else
• {
• printf("c is the greatest");
• }
• }
• }
If else ladder
• The expression is tested from the top(of the ladder) downwards.
• As soon as a true condition is found, the statement associated with it is
executed.
• Syntax :
if(expression1) expression means - condition
{
statement block1;
}
else if(expression2)
{
statement block2;
}
else if(expression3 )
{
statement block3;
}
else
default statement;
• #include <stdio.h>
• void main( )
• {
• int a;
• printf("Enter a number...");
• scanf("%d", &a);
• if(a%5 == 0 && a%8 == 0)
• {
• printf("Divisible by both 5 and 8");
• }
• else if(a%8 == 0)
• {
• printf("Divisible by 8");
• }
• else if(a%5 == 0)
• {
• printf("Divisible by 5");
• }
• else
• {
• printf("Divisible by none");
• }
• }
Switch Statement
• C switch statement is used when you have multiple possibilities for the if statement.
• Switch case will allow you to choose from multiple options.
• After the end of each block it is necessary to insert a break statement because if the
programmers do not use the break statement, all consecutive blocks of codes will get
executed from every case
• switch(variable)
• {
• case 1:
• //execute your code
• break;
• case n:
• //execute your code
• break;
• default:
• //execute your code
• break;
• }
• #include<stdio.h>
• void 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:
• 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;
• }
– Repetitive Statements: Looping is a powerful technique through
which a group of statements are executed repeatedly until
specified condition is satisfied. These are also known as iterative
statement. ‘C’ provides three types of loop control structure.
•
• For Loop : It is used when the programmer known in
advance the number of times a statement or block of
statement will be executed.
• The syntax of for loop is:
• for (initialization; condition; update Statement)
• { // codes
• }
for(a=5; a<=10; a=a+2)
for(a=10; a>=1;a--)
• While Loop: The while loop is used when the
number of times the loop has to be executed
is not known in advance. The syntax of while
loop is
• Initialization;
• while (;)
• {
• Statement;
• inc/dec;
• }
Do while syntax
• do
• {
– Statemets—1
– Staements – 2
– Statements – 3
– Inc/dec(++ , --)
}while(condition);
Transfer Statements
• Continue
• #include<stdio.h>
• void main()
• { int age;
• g: //label name
• printf("you are Eligible\n");
• s: //label name
• printf("you are not Eligible");
• printf("Enter you age:");
• scanf("%d", &age);
• if(age>=18)
• goto g; //goto label g
• else
• goto s; //goto label s
• }
• #include <stdio.h>
• int main()
• {
• int i = 0;
• start:
• if (i < 5)
• {
• printf("%d ", i);
• i++;
• goto start; // This jumps back to the 'start' label }
• return 0;
• }
• Goto label;
– --
– ---
– ---
– Lable : statement, xyz
– Goto xyz;
– Xyz:
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.
Example :
• #include<stdio.h>
• int main(){
• int i=1;//initializing a local variable
• //starting a loop from 1 to 10
• for(i=1;i<=10;i++){
• if(i==5){//if value of i is equal to 5, it will continue the loop
• continue;
• }
• printf("%d \n",i);
• }//end of for loop
• return 0;
• }
• #include <stdio.h>
int main()
{
int i;
for(i=0;i<20;i++)
{
if(i%2==0)
{
continue;
}
printf("%d ",i);
}
}
• #include <stdio.h>
void checkEven(int num)
{
if (num%2 == 0)
goto even_no;
else
goto odd_no;
even_no:
printf("The number is even.\t");
goto even_no;
odd_no:
printf("The number is odd.\t");
goto odd_no;
}
void main() {
int i = 10;
checkEven(i);
}
• #include <stdio.h>
• int main ()
• {
• /* local variable definition */
• int a = 10;
• LOOP:
• do
• {
• if( a == 15)
• { /* skip the iteration */
• a = a + 1;
• //goto
• //LOOP;
• continue;
• //break;
• }
• printf("value of a: %d\n", a);
• a++;
• }while( a < 15 );
• return 0;
•