Download presentation
Presentation is loading. Please wait.
1
CSCE 206 Lab Structured Programming in C
Fall 2018 Lecture 3 Ehsanul Haque Nirjhar
2
Today’s objective If-else logic block If-else if-else logic block
switch logic block
3
if-else
4
Sample Code-1 #include <stdio.h> int main(void) { int a;
printf("Enter a number:"); // Asks user for input scanf("%d",&a); if (a>0) printf("%d is a positive number",a); } else printf("%d is not a positive number",a); return 0;
5
if-else if-else
6
Sample Code-2 #include <stdio.h> int main(void) { int a; printf("Enter a number:"); // Asks user for input scanf("%d",&a); if (a>0) printf("%d is a positive number",a); } else if (a<0) printf("%d is a negative number",a); else printf("%d is zero",a); return 0;
7
Operators
8
Sample Code-3 #include <stdio.h> int main(void) { int a; printf("Enter a number:"); // Asks user for input scanf("%d",&a); if (a>0 || a==0) printf("%d is not a negative number",a); } else printf("%d is a negative number",a); return 0;
9
Nested if-else block
10
Sample Code-4 #include <stdio.h> int main(void) { int a; printf("Enter a number:"); // Asks user for input scanf("%d",&a); if (a>=0) if(a==0) printf("%d is zero",a); } else printf("%d is a positive number",a); printf("%d is a negative number",a); return 0;
11
switch case
12
Sample Code-5 #include <stdio.h> int main(void) { int a; printf("Enter a number:"); // Asks user for input scanf("%d",&a); switch(a){ case 0: printf("Zero"); break; } case 1: printf("One"); default: printf("No data"); return 0;
13
Now it’s your turn!
14
Practice Problem-1 Write a program that takes an integer as input and determines whether it is odd or even. It gives a proper sentence as output. Tips: Use if-else block
15
Practice Problem-2 Write a program that takes 3 integers as input and prints out the largest number of them. Tips: Use if-else if-else block
16
Practice Problem-3 Try problem 1 using switch case.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.