















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to the if statement in c programming, including its syntax and various logic operators such as equal to, not equal to, less than, greater than, less than or equal to, and greater than or equal to. It also covers compound operators like logical and, logical or, and logical not.
Typology: Slides
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















If the expression is true (not zero), thestatement is executed.
If the expression is false, it is notexecuted.
if (expression) {
statement 1;statement 2;statement 3; }
Equal to
if (x==10)
Not equal to
if (x!=10)
Less than
if (x<10)
Greater than
if (x>10)
Less than / equal to
if (x<=10)
Greater than / equal to
if (x>=10)
Single statements:
if
(
condition)
true_statement; else
false_statement
;
Multiple statements:
if
(
condition)
{
…… } else
{
…… }
int winner =3;printf(“…and the winner of ICC is ”);if (winner==1)
printf(“Pakistan”); else if (winner==2)
printf(“England”); else if (winner==3)
printf(“WI”); else
printf(“Australia”);
}
switch
(expression)
{
value_
:
statements_1;
break;
value_
:
statements_2;
break;
...value_n
:
statements_n;
break
default
switch
(winner)
{
case
1
:
printf(“Pakistan”);
break;
case
2
:
printf(“England”);
break;
. value_n
:
statements_n;
break
default:
for
(initialization; test;
increment)
An example while loop looks like this:
void
main(void)
char
ch;
while
(ch
ch
‘q’)
… ch
getchar(
i=0;while(i<10){
body
of
the
loop;
i++;
} is
equivalent
to
for(i=0;
i<10;
i++)
body
of
the
loop;
degree
increment;
Using for
for(;;){
printf(“Would
you
please
stop
teaching
C\n”);
Using while
while(1){
printf(“Would
you
please
stop
teaching
C\n”);
void main(void){
int deg_c, deg_f;deg_c = 0;while (deg_c <= 100){
deg_f = (deg_c-32)*5/9;printf("%4d degrees F = %4d degrees C\n", deg_c, deg_f);deg_c = deg_c + 10;
} }