Todays topics Algorithm Pseudo code The if Structure

  • Slides: 18
Download presentation
Today’s topics • • • Algorithm Pseudo code The if Structure The if/else Structure

Today’s topics • • • Algorithm Pseudo code The if Structure The if/else Structure

Algorithm • The solution of any computing problem involves a series of action in

Algorithm • The solution of any computing problem involves a series of action in a specific order. • This procedure of solving problems is called algorithm.

Algorithm • What are the procedures you follow before you come to the class?

Algorithm • What are the procedures you follow before you come to the class?

Pseudocode • Pseudocode is an outline of a program, written in a form that

Pseudocode • Pseudocode is an outline of a program, written in a form that can easily be converted into real programming statements • Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules • It is simply one step - an important one - for producing the final code

Pseudocode • The benefit of pseudocode is that it enables the programmer to concentrate

Pseudocode • The benefit of pseudocode is that it enables the programmer to concentrate on the algorithms without worrying about all the syntactic details of a particular programming language • You can write pseudocode without even knowing what programming language you will use for the final implementation

The if Selection Structure • A selection structure is used to choose among alternative

The if Selection Structure • A selection structure is used to choose among alternative courses of action if student’s grade is more than 40 print “passed” next pseudocode statement • In this pseudocode, if grade of a student is more than 40 then the print command will be executed. Otherwise, if the student’s grade is not more than 40, the compiler will move to next pseudocode statement

The if Selection Structure • So, as a general form, we can see the

The if Selection Structure • So, as a general form, we can see the if selection structure asif (condition){ body of if } • The condition needs to be true to get into the body of if. Otherwise, the compiler will go to the next segment of codes

The if Selection Structure • The pseudocode can be written in C asif (grade>40){

The if Selection Structure • The pseudocode can be written in C asif (grade>40){ printf (“Passed n”); }

The if/else Selection Structure • The if/else structure allows the programmer to specify that

The if/else Selection Structure • The if/else structure allows the programmer to specify that different actions are to be performed when the condition is true and when the condition is false if student’s grade is more than 40 print “passed” else print “failed” • If the condition of if is true, then compiler will print passed and if the condition of if is false, the compiler will print failed.

The if/else Selection Structure • So, as a general form, we can see the

The if/else Selection Structure • So, as a general form, we can see the if/else selection structure asif (condition){ body of if } else{ body of else }

The if/else Selection Structure • The pseudocode can be written in C asif (grade>40)

The if/else Selection Structure • The pseudocode can be written in C asif (grade>40) { printf (“Passed n”); } Else { printf (“Failed n”); }

The if/else Selection Structure • An if structure may not have any else statement

The if/else Selection Structure • An if structure may not have any else statement followed by it but an else structure must have a if structure preceded.

The if/else Selection Structure • Now, consider a big scenario where you may require

The if/else Selection Structure • Now, consider a big scenario where you may require a nested if else structure.

The if/else Selection Structure if student’s grade is more than 90 print “Grade: A”

The if/else Selection Structure if student’s grade is more than 90 print “Grade: A” else if student’s grade is more than 80 print “Grade: B” else if student’s grade is more than 70 print “Grade: C” else if student’s grade is more than 60 print “Grade: D” else if student’s grade is more than 50 print “Grade: E” else print “Grade: F”

The if/else Selection Structure #include<stdio. h> void main(){ int grade; printf("Enter your grade: ");

The if/else Selection Structure #include<stdio. h> void main(){ int grade; printf("Enter your grade: "); scanf("%d", &grade); if (grade>90) printf("Grade: A"); else if (grade>80) printf("Grade: B"); else if (grade>70) printf("Grade: C"); else if (grade>60) printf("Grade: D"); else if (grade>50) printf("Grade: E"); else printf("Grade: F"); getch();

The if/else Selection Structure • Now, guess what happens if we replace the above

The if/else Selection Structure • Now, guess what happens if we replace the above code as follows-

The if/else Selection Structure #include<stdio. h> void main(){ int grade; printf("Enter your grade: ");

The if/else Selection Structure #include<stdio. h> void main(){ int grade; printf("Enter your grade: "); scanf("%d", &grade); if (grade>90) printf("Grade: A"); if (grade>80) printf("Grade: B"); if (grade>70) printf("Grade: C"); if (grade>60) printf("Grade: D"); if (grade>50) printf("Grade: E"); else printf("Grade: F"); getch();

The if/else Selection Structure • This program will output Grade: C Grade: D Grade:

The if/else Selection Structure • This program will output Grade: C Grade: D Grade: E- this is not desirable