Sie sind auf Seite 1von 16

If Else If 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.

if(condition) statement 1 else statement 2


Where:
if and else are keywords condition is relational expression statement 1 & 2 are results

Write a program that will output CONGRATULATIONS YOU PASSED!, if the students grade is greater than or equal to 60. Otherwise output, SORRY YOU FAILED!.

#include<stdio.h> int grade; main() { clrscr(); printf(Enter student grade:); scanf(%d, &grade); if (grade >= 75) printf(CONGRATULATIONS YOU PASSED!); else printf(SORRY YOU FAILED!) getche(); }

if (Condition1) statement1; else if(Condition2) statement2; else if(Condition3) statement3; else statement_else;

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.

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!

#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; }

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!

#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:
Entry Number 1 2 3 4 Other entry number High School Status Freshman Sophomore Junior Senior 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.

Design

a program which determines if the supply age by the user is qualified to vote or not. The qualifying is 18 and above.

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.

Das könnte Ihnen auch gefallen