Conditional statements Flashcards

1
Q

Types of conditional statements in C

A

include<stdio.h></stdio.h>

IF-ELSE

if (condition){
// do this if true
}
else if (condition){
// do this if true
}
else {
// do this
}

if only single statement then no need for {} but it is preferred

SWITCH

switch(number){

case c1://do something
break;
case c2://do something
break;
default://do something
}

number and alphabets like (1,2,3 or “a”,”b”,”c”) can be used,default is like else statement
order of the case is not important here,nested case is also supported

int main(){
int day;
printf(“enter day “);
scanf(“%d”,&day);

switch (day)
{
case 1 : printf("monday\n");
    break;
case 2 : printf("tuesday\n");
    break;    
case 3 : printf("thursday\n");
    break;    
case 4 : printf("wednesday\n");
    break;
case 5 : printf("thursday\n");
    break;
case 6 : printf("friday\n");
    break;
case 7 : printf("sunday\n");
    break;                            
default : printf("invalid number");
}

}
if we don’t put break then every statement after the match will be executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Ternary operation in C

A

include<stdio.h></stdio.h>

used as a shorthand version of if-else statement

condition? if true do this : else do this

int main(){
int age;
printf(“enter age :”);
scanf(“%d”,&age);

age>=18?printf("adult\n"):printf("young");

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

nested if else statement

A

int main(){
int number;
printf(“enter number :”);
scanf(“%d”,&number);

if (number>0){
    printf("positive\n");
    if (number%2==0){
        printf("even number\n");
    }
    else{
        printf("odd number\n");
    }
}
else{
    printf("negative number");
}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Find the error

int x=2;
if (x=1){
printf(“x is equal to 1”);
}
else{
printf(“x is not equal to 1”);
}

A

x is equal to 1
here x is replaced by 1
x=1 any thing above 0 is considered true

if x was 0 then
the second statement gets executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

code to check if a char is upper or lower

A

include<stdio.h></stdio.h>

int main(){

char ch;
printf("enter character");
scanf("%c",&ch);

if (ch>='A' && ch<='Z'){
    printf("upper case");
}
else if(ch>='a' && ch<='z'){
    printf("lower case");
}
else{
    printf("not english");
}

}

here we are comparing ASCII key of alphabets
“A”-65
“Z”-90

“a”-97
“z”-122

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

armstrong number or not

A

include<stdio.h></stdio.h>

int main(){
int n,r,sum=0,temp;
printf(“enter the number : “);
scanf(“%d”,&n);

temp=n;
while(n>0){
    r=n%10;
    sum=sum+(r*r*r);
    n=n/10;
}

if (temp==sum){
    printf("armstrong number");

}
else{
    printf("not armstrong number");

}
return 0;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

natural number or not

A

include<stdio.h></stdio.h>

int main(){
int number;
printf(“enter the number : “);
scanf(“%d”,&number);

if (number>=1){
    printf("number is natural\n");
}
else{
    printf("number is not natural");
}

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly