Wednesday, 1 May 2019

ch-2 if statement

If Statement

#include<stdio.h>

 main()

 {
  int a=0;
  if(a=0)
  printf("a is zero");
  else
  printf("not zero");

}

 main()

 {
  int a=0,b=1;
  if(a=0)
  printf("a is zero");
  else
  printf("not zero\n");
  printf("are you ok");

}

#include<stdio.h>

 main()
 {
  int a=0,b=1;
  if(a==0)
  printf("a is zero");
  else
  printf("not zero\n");
  printf("are you ok");
}


#include<stdio.h>
main()
{
  float a=1.2;
  if (a==1.2)
  printf("yes");
  else
  printf("no");

}

ans No

#include<stdio.h>
main()
{
  float a=1.0;
  if (a==1.0)
  printf("yes");
  else
  printf("no");

}
ans yes


#include<stdio.h>

 main()

 {
  int a=0,b=1;
  if(b>1)
  if(a==0)
  printf("a is zero");
  else
  printf("not zero\n");
  printf("are you ok");

}

#include<stdio.h>

 main()

 {
  int a=0,b=1;
  if(b>1 || b==1)
  if(a==0)
  printf("a is zero");
  else
  printf("not zero\n");
  printf("are you ok");
}

#include<stdio.h>
 main()
 {
  int a=0,b=1;
  if(b>1 && b==1)
  if(a==0)
  printf("a is zero");
  else
  printf("not zero\n");
  printf("are you ok");
}

#include<stdio.h>
 main()
 {
  int a=0,b=1;
  if(1)
  if(a==0)
  printf("a is zero");
  else
  printf("not zero\n");
  printf("are you ok");
}
#include<stdio.h>
 main()
 {
  int a=0,b=1;
  if(0)
  if(a==0)
  printf("a is zero");
  else
  printf("not zero\n");

  printf("are you ok");
}
#include<stdio.h>

 main()

 {
  int a=0,b=1;
  if(a||0)
  if(a==0)
  printf("a is zero");
  else
  printf("not zero\n");
  printf("are you ok");
}

#include<stdio.h>
  main()
  {
  int a=0,b=1;
  if(a||1)
  if(a==0)
  printf("a is zero");
  else
  printf("not zero\n");
  printf("are you ok");
}

#include<stdio.h>
main()
{
char a;
 printf("enter r,g,y");
 scanf("%c",&a);
 switch(a)
 {
 case 'r':
printf("stop");
break;
case 'g':
printf("go");
break;
case 'y':
printf("clear");
default:
printf("you enter wrong");
 }
}

#include<stdio.h>
main()
{
char a;
 printf("enter r,g,y");
 scanf("%c",&a);
 switch(a)
 {
 case 'r':
printf("stop");
break;
case 'g':
printf("go");
break;
case 'y':
printf("clear");
default:
printf("you enter wrong");

 }
}

#include<stdio.h>
main()
{
 int a=10;
 (a==10?printf("yes"):printf("no"));
}

Fill in the blank:

A. Ternary operator  B. Conditional Operator   C. || D. break

1. The conditional operator (?:) is a ____Ternary operator _______ 

2 ___|| ______ is a logical OR operator

3. The ___break_____ statement when executed in a switch statement causes immediate exit from the structure 



4. A pair of question mark and colon is known as _conditional operator_______



Question:

Compare the use of switch statements with the use of nested if statements. Which is more convenient?

What will be the output of the following program segment?

 main() { int x=3,
y=5; 
if(x==3)
 printf(“\n%d”,x); 
else 
printf(“\n%d”,y); } 




MCQ:
What will be output if you compile and execute the following „C‟ code 
void main() 
{
 float a=5.2;
 if(a==5.2) 
printf(“equal”); 
else
 if(a<5.2) 
printf(“less than”); 
else printf(“greater than”); 

} A) Equal 
B) Less than 
C) Greater than 
D) Compilation Error

Determine the order of evaluation of operations within the expression   A<B&&C||D>A  
A) &&, ||, <, >
 B) <, >, &&, || 
C) >, <, &&, || 
D) &&,<, >, ||  

1.3 What is the output of this C code ? 
#include 
int main()
 { 
int x=2,y=0; 
int z=y&&(y/=10); 
printf(“%d\n”,z); 
return 0;
 }
a) 1
b) 0
c) undefined behavior due to order of evaluation 
d) 2

ans b) 0

What will be the output of the following code?
 main()
 {
 int x = 0, y = 0; 
 if(x > 0) if(y > 0) 
 printf("True"); 
 else 
 printf("False");
 } 
A) Blank screen
B) True 
C) False 
D) Error because of dangling else problem 

true/false
1. break keyword is mandatory in switch case structure. 

Question/Answer
Write a „C‟program that displays the recommended actions depending on the color of a traffic light using the switch statement.


Explain the difference between = and == operator with example.







No comments:

Post a Comment

Index

ch-1 Introduction