Beruflich Dokumente
Kultur Dokumente
Control Statements
if else statement
the switch - case statement
the while do loop
the do while loop
the for loop
If else condition
Syntax:
if (<expression>)
{
<statement>;
}
else
{
<statement>;
}
if (x>10)
{
x=0;
else
{
x=x+1;
}
}
if (b == 100)
{
program statements
}
else
{
program statements
}
If condition
The
can be used without the else part.
Whatif-else
aboutstatement
this?
if (x!=0)
{
if (y>0 || x>0)
printf(x not zero);
{
}
}
The printf statement will only be executed if x is not
zero.
if (y>0 && x>0)
{
printf(x and y are more than zero);
}
Example 1
Write a program to determine whether a person is eligible
to vote.
#include <stdio.h>
void main()
{
int age;
printf("\n Enter the age: ");
scanf("%d", "&age");
if(age >= 18)
{
printf("\n You are eligible to vote");
return 0;
}
Example 2
Write a program to find whether the given number is even
or odd.
.
#include <stdio.h>
void main()
{
int num;
printf("\n Enter any number: ");
scanf("%d",&num);
if(num%2 == 0)
{
printf("\n %d is an even number", num);
else
{
printf("\n %d is an odd number", num);
return 0;
}
}
}
Example 3
Write a program to print Congratulation if a student pass
the exam.
#include<stdio.h>
int main(void)
{
int score;
printf("Insert the amount of score :\n");
scanf("%d", &score);
if (score >= 50)
//test on ==, !=
{
printf ("Congratulations!\n");
}
printf ("Your score is %d\n", score);
return (0);
}
Example 4
Write a program to print Congratulation and the grade if
a student pass the exam.
#include<stdio.h>
void main(void)
{ int score;
char grade = 'F';
printf("Insert the amount of score :\n");
scanf("%d", &score);
Example 5
/*Name: Er Teck Khiang*/
/*Title: Lecture 4: Programme 9*/
/*Date: 29/04/05*/
#include<stdio.h>
int main(void)
{
int score;
printf("Insert the amount of score :\n");
scanf("%d", &score);
if (score >= 50)
{
if (score >= 85)
{
printf ("Congratulations! \n");
else
{
printf ("You got a pass.\n");
}
printf ("Your score is %d\n", score);
return (0);
}
}
}
Common Mistake
#include<stdio.h>
void main()
{
printf(input an interger);
scanf(%d, j);
if (j>0);
{
printf(\nA positive number was entered!);
}
return 0;
}
if if else - else:
An option else can be added
if (mark > 80)
{
printf(Congratulation!\n);
}
else if (mark > 50)
{
printf(you pass this subject\n);
}
else
{
printf(You fail.\n);
}
Example 6
#include<stdio.h>
int main(void)
else if (score
{
{
int score;
grade =
char grade;
}
printf("Insert the score :\n");
else if (score
scanf("%d", &score);
{
grade =
if (score >= 85)
}
{
else
grade = 'A';
{
}
grade =
else if (score >= 80)
}
{
grade = 'B';
printf (Score
}
printf (Grade
else if (score >= 70)
return (0);
{
}
grade = 'C';
}
>= 60)
'D';
>= 50)
'E';
'F';
is %d\n", score);
is %c\n", grade);
if if - else:
An option else can be added
if (mark > 80)
{
printf(Congratulation!\n);
}
if (mark > 50)
{
printf(you pass this subject\n);
}
else
{
printf(You fail.\n);
}
Example 7
#include<stdio.h>
int main(void)
if (score >= 60 && score < 70)
{
{
int score;
grade = 'D';
char grade;
}
printf("Insert the score :\n");
if (score >= 50 && score < 60)
scanf("%d", &score);
{
grade = 'E';
if (score >= 85 && score <=100)
}
{
If (score >= 0 && score < 50)
grade = 'A';
{
}
grade = 'F';
if (score >= 80 && score < 85)
}
{
grade = 'B';
printf (Score is %d\n", score);
}
printf (Grade is %c\n", grade);
if (score >= 70 && score < 80)
return (0);
{
}
grade = 'C';
}
Example 8
#include<stdio.h>
int main(void)
{
int x, y, z, min;
}
}
}
Sleepy mode???
Lets have some practical
Question 1
Produce if statement to determine whether variable
int y is greater than 10 and less than 15 and print out
the message within the range.
solution:
if (y > 10 && y < 15)
{
printf(%c,y);
}
Question 2
Write a program to test whether a number
entered is positive, negative or equal to zero.
Nested if
#include<stdio.h>
int main( )
{
int number;
printf(Enter a number between 1 and 99);
scanf(%d,&number);
if (number > 0 && number < 100)
{
printf(Number within the range\n);
if (number < 10)
{
printf(one digit); }
else
{
printf(two digit); }
}
else
{ printf(number not within range); }
return 0;
}
Question 3
use the if statement to check that an input
integer p is between 1 and 9, but not 5.
When the above condition is met, your program
should print out the character p with the
message p is between 1 and 9 and is not a 5 but
is - followed by the value of p.
a solution..
main()
{
scanf(%d,&p);
if (p > 0 && p !=5 && p<10)
{
printf(p is between 1 and 9 and not 5);
printf(p is %d,p);
}
... rest of program
}
Tutorial 1
Tutorial 2
Tutorial 3