Bài giảng Lập trình C - Session 4: Condition

 Explain the Selection Construct
- If Statement

 - If – else statement

 - Multi if statement

 - Nested if statement

 

 Switch statement

 

ppt18 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1656 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng Lập trình C - Session 4: Condition, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Condition Session 4 Elementary Programming with C/Session 5/ * of 19 Objectives Explain the Selection Construct - If Statement - If – else statement - Multi if statement - Nested if statement Switch statement Elementary Programming with C/Session 5/ * of 19 Conditional Statement Conditional statements enable us to change the flow of the program A conditional statement evaluates to either a true or a false value Example : To find whether a number is even or odd we proceed as follows : Accept a number Find the remainder by dividing the number by 2 If the remainder is zero, the number is “EVEN” Or if the remainder is not zero the number is “ODD” Elementary Programming with C/Session 5/ * of 19 Selection Constructs C supports two types of selection statements The if statement The switch statement Elementary Programming with C/Session 5/ * of 19 The if statement-1 Syntax: If the if expression evaluates to true, the block following the if statement or statements are executed Elementary Programming with C/Session 5/ * of 19 The if statement-2 	 	#include 	void main() 	{ 	int x, y; 	char a = ‘y’; 	x = y = 0; 	if (a == ‘y’) 	{ 	x += 5; 	printf(“The numbers are %d and \t%d”, x, y); 	} 	} Program to display the values based on a condition Example Elementary Programming with C/Session 5/ * of 19 The if – else statement-1 Syntax: Elementary Programming with C/Session 5/ * of 19 The if – else statement-2 If the if expression does not evaluate to true then the statements following the else expression take over control The else statement is optional. It is used only if a  statement or a sequence of statements are to be executed in case the if expression evaluates to false If the if expression evaluates to true, the block following the if statement or statements are executed Elementary Programming with C/Session 5/ * of 19 The if – else statement -3 #include void main() { 	int num , res ; 	 	printf(“Enter a number :”); 	scanf(“%d”,&num); 	res = num % 2; 	if (res == 0) 	printf(“Then number is Even”); 	else 	printf(“The number is Odd”); } Program to display whether a number is Even or Odd Example Elementary Programming with C/Session 5/ * of 19 The if–else–if statement-1 Syntax: Elementary Programming with C/Session 5/ * of 19 The if – else – if statement is also known as the if-else-if ladder or the if-else-if staircase The conditions are evaluated from the top downwards The if–else–if statement-2 Elementary Programming with C/Session 5/ * of 19 #include 	main() 	{ 	int x; 	x = 0; 	clrscr (); 	printf(“Enter Choice (1 - 3) : “); 	scanf(“%d”, &x); 	if (x == 1) 	printf (“\nChoice is 1”); 	else if ( x == 2) 	printf (“\nChoice is 2”); 	else if ( x == 3) 	printf (“\nChoice is 3”); 	else 	printf (“\nInvalid Choice “); 	} Program to display a message based on a value The if–else–if statement-3 Example Elementary Programming with C/Session 5/ * of 19 Nested if-1 The nested if is an if statement, which is placed within another if or else In C, an else statement always refers to the nearest if statement that is within the same block as the else statement and is not already associated with an if Elementary Programming with C/Session 5/ * of 19 Note that the inner else is associated with if(exp3) According to ANSI standards, a compiler should support at least 15 levels of nesting Syntax: Nested if-2 Elementary Programming with C/Session 5/ * of 19 Nested if-3 	#include 	void main () 	 { 	int x, y; 	x = y = 0; 	clrscr (); 	printf (“Enter Choice (1 - 3) : “); 	scanf (“%d”, &x); 	if (x == 1) 	{ 	printf(“\nEnter value for y (1 - 5) : “); 	scanf (“%d”, &y); 	if (y 	main () 	{ 	char ch; 	clrscr (); 	 	printf (“\nEnter a lower cased alphabet (a - z) : “); 	scanf(“%c”, &ch); 	 	 	contd……. Program to check whether the entered lowercase character is vowel 	or ‘z’ or a consonant Example Elementary Programming with C/Session 5/ * of 19 if (ch ‘z’) 	printf(“\nCharacter not a lower cased alphabet”); else 	switch (ch) 	{ 	case ‘a’ : 	case ‘e’ : 	case ‘i’ : 	case ‘o’ : 	case ‘u’ : 	printf(“\nCharacter is a vowel”); 	break; 	case ‘z’ : 	printf (“\nLast Alphabet (z) was entered”); 	break; 	default : 	printf(“\nCharacter is a consonant”); 	break; 	} 	} The switch statement-4 Example 

File đính kèm:

  • pptBài giảng Lập trình C - Session 4 Condition.ppt
Tài liệu liên quan