Chapter - 3: The Decision Control

Point out the errors, if any, in the following programs:


F
Sections
1
Exercises

A

B

C

D

E

F

G

H

I

J

K

1

(a)

#include<stdio.h>
int main()
{
	int i = 2, j = 5 ;
	
	if ( i == 2 && j == 5 )
		printf ( "\nSatisfied at last" ) ; 
	
	return 0;
}

No Error


(b)

#include<stdio.h>
int main()
{
	int code, flag ;
	
	if ( code == 1 & flag == 0 )
		printf ( "\nThe eagle has landed" ) ; 
	
	return 0;
}

No Error


(c)

#include<stdio.h>
int main()
{
	
	char spy = 'a', password = 'z' ;
	
	if ( spy == 'a' or password == 'z' )
		printf ( "\nAll the birds are safe in the nest" ) ; 
	
	return 0;
}

No Error


(d)

#include<stdio.h>
int main()
{
	
	int i = 10, j = 20 ;
	
	if ( i = 5 ) && if ( j = 10 )
		printf ( "\nHave a nice day" ) ;
	
	return 0;
}

Error: Wrong use of && operator.


(e)

#include<stdio.h>
int main()
{
	
	int x = 10 , y = 20;
 
 	if ( x >= 2 and y <=50 )
 		printf ( "\n%d", x ) ; 
	
	return 0;
}

No Error


(f)

#include<stdio.h>
int main()
{
	
	int a, b ;
 
 	if ( a == 1 & b == 0 )
 		printf ( "\nGod is Great" ) ; 
	
	return 0;
}

No Error


(g)

#include<stdio.h>
int main()
{
	int x = 2;
	
	if ( x == 2 && x != 0 ) ;
	{
		printf ( "\nHi" ) ;
		printf( "\nHello" ) ;
	}
	else
		printf( "Bye" ) ; 
	
	return 0;
}

Error: else if used without any if. First if is ended as semicolon and printf() functions are there. Then else the statement comes is not part of the if statement.


(h)

#include<stdio.h>
int main()
{
	int i = 10, j = 10 ;

	if ( i && j == 10)
		printf ( "\nHave a nice day" )  ; 
	
	return 0;
}

No Error


© 2020 Garbage Valuegarbage value logo