Algorithm, Pseudocode, The if Structure, The if/else Structure, The if/else if/else Structure, Increment/Decrement Operators


Lecture 4

Algorithm

The solution of any computing problem involves a series of action in a specific order. This procedure of solving problems is called algorithm. For example, the going to class algorithm of a student involves-
  1. Get out of bed
  2. Brush the teeth
  3. Finish toilet
  4. Get shower
  5. Get dressed
  6. Take breakfast
  7. Go to the class

However, if you slightly change the order of the procedure as follows-

  1. Get out of bed
  2. Brush the teeth
  3. Finish toilet
  4. Get dressed
  5. Get Shower
  6. Take breakfast
  7. Go to the class

Then the student must let his/her dress get wet. Specifying the order in which the program will be executed is called program control.

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 - in producing the final code. 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. In fact, 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 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. So, as a general form, we can see the if selection structure as-

if (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 pseudocode can be written in C as-

if (grade>40)
      printf (“Passed \n”);

The if/else Selection Structure

The if selection structure performs an indicated action only when the condition is true. Otherwise, the action is skipped. 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. So, as a general form, we can see the if/else selection structure as-

if (condition){
      body of if
}
else{
      body of else
}


The pseudocode can be written in C as-
if (grade>40)
      printf (“Passed \n”);
else
      printf (“Failed \n”);

Note- An if structure may not have any else statement followed by it but an else structure must have an if structure preceded.

The if/else if/else Selection Structure

Now, consider a big scenario where you may require a nested if else 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 program to provide the desired output is shown here.

#include<stdio.h>
#include<conio.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();
}

If you provide your number 80, the program will tell you that your grade is C. What happens here can be debugged by pressing F7 continuously until the program reaches the end.

1.    clrscr ( ); The output screen is cleared.
2.    int grade; A location in the memory is created and grade indicates that location, currently holding a garbage value.
3.    printf (“…”); Prints the string on the output.
4.    scanf(“…”, …); Scans the number from the user and puts that value into the memory location where grade is pointing. The garbage value is destroyed.
5.    if (…) the condition is false. Hence, compiler won’t go into if body.
6.    else if (…) the condition is false. Hence, compiler won’t go into the body of the first else if.
7.    else if (…) the condition is true. Hence, compiler will print Grade: C.
8.    The compiler ignores all other else if there and the last else. It reaches getch() and waits for a key-stroke.

Now, guess what happens if we replace the above code as follows-

#include<stdio.h>
#include<conio.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();
}

This program will output Grade: C Grade: D Grade: E- this is not desirable.

Increment and Decrement Operators


C provides the unary increment operator ++ and unary decrement operator --. If a variable a is incremented by 1, the increment operator ++ can be used instead of expression a=a+1. But the meaning differs based on the place of the operator to the variable.

a++ means use the current value of a in the expression then increase a by 1.
++a means increase the current value of a by 1 and then use the value in expression.
a-- means use the current value of a in the expression then decrease a by 1.
--a means decrease the current value of a by 1 and then use the value in expression.

If unary increment/ decrement operators are placed in front of the variable, they are called preincrement/ predecrement operators otherwise they are called postincrement/ postdecrement operators.
#include<stdio.h>
#include<conio.h>

void main(){
      int a=5, b=10;         
      printf("%d\n", a);
      printf("%d\n", a++);
      printf("%d\n", a);
     
      a=5;
      printf("%d\n", a);
      printf("%d\n", ++a);
      printf("%d\n", a);

      printf("%d\n", b);
      printf("%d\n", b--);
      printf("%d\n", b);
     
      b=10;
      printf("%d\n", b);
      printf("%d\n", --b);
      printf("%d\n", b);

      getch();
}
 The output of the program is-
5
5
6
5
6
6
10
10
9
10
9
9


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by CelebrityDisk | Written by Alamin - link | Grants For Single Moms