Upload
danica-corpuz
View
191
Download
1
Embed Size (px)
Citation preview
Conditional Statements
If ElseIf Else If
Switch
are statements that check an expression/condition then may or may not execute a statement or group of statements depending on the result of the condition.
Conditional Statements
if(condition)statement 1
elsestatement 2
Where:if and else are keywordscondition is relational expressionstatement 1 & 2 are results
If Else Statement
Write a program that will output “CONGRATULATIONS YOU PASSED!”, if the student’s grade is greater than or equal to 60. Otherwise output, “SORRY YOU FAILED!”.
Example:
#include<stdio.h>int grade;main(){
clrscr();printf(“Enter student grade:”);scanf(“%d”, &grade);if (grade >= 75)printf(“CONGRATULATIONS YOU
PASSED!”);elseprintf(“SORRY YOU FAILED!”)
getche();}
if (Condition1)statement1;
else if(Condition2)statement2;
else if(Condition3)statement3;
elsestatement_else;
If Else If Statement
In an if-else-if statement, the expressions are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed, and the rest of the ladder will not be executed. If none of the condition is true, the final else is executed.
Remember!
Write a program that will ask the user to input an integer then output the equivalent day of the week. 1 is Sunday, 2 is Monday and so on. If the input number is not within 1-7, output is “Day is not available!”
Example
#include<stdio.h>main(){Int day;printf(“Enter an integer:”);scanf(“%d”, &day”);
If(day==1)printf(“Sunday”);
else if (day==2)printf(“Monday”);
;;else printf(“Day is not available!”);
getch();}
switch(variable){
case constant1:statement sequence1;break;
case constant2:statement sequence2;break;
;;default:
statement sequence_default;}
Switch Statement
Write a program that will ask the user to input an integer then output the equivalent day of the week. 1 is Sunday, 2 is Monday and so on. If the input number is not within 1-7, output is “Day is not available!”
Example
#include<stdio.h>main(){int day;printf(‘Enter an integer:”);scanf(‘%d”, &day);
switch(day){case 1:
printf(“Sunday”);break;
case 2:printf(“Monday”);break;
default:printf(“Day is not available!”);
}getch();}
If Else Statement Design a program which identifies the larger of two
numbers supplied by the user.
If Else If Statement Design a program to display the year level if students
based on their year entry number. Here are the given criteria:
Hands On
Entry Number High School Status1 Freshman2 Sophomore3 Junior4 Senior
Other entry number Out-of-school youth
Switch Statement Write a program that will ask the user to
input 2 integers and a character (A, S, M, D). If the user inputs ‘A’ for the character, add the 2 numbers, if ‘S’ subtract the 2 numbers, if ‘M’ multiply and if ‘D’ divide the numbers. Output the computed value.
Hands On
Design a program which determines if the supply age by the user is qualified to vote or not. The qualifying is 18 and above.
Evaluation
Write a program that accepts the input magic number. If the input is right, the magic words will be displayed. The magic number is 143 and its corresponding magic words are “I love you”. If the input number is wrong, displays “Sorry, better luck next time”.
Assignment