CodeClub, SMVDU

“Sometimes it is the people no one can imagine anything of who do the things no one can imagine.” - Alan Turing

Let Us Revisit

  • Computer Programs and Programming Languages
  • Variables and Datatypes
  • Hello World!
  • printf() and scanf() functions
  • Basic Operations
  • Conditional Statements(if-else)

Lets Program

Write a program to input a number n and print Hello World n times.

#include<stdio.h>
void main(){
    int n;
    printf("Enter the number of times");
    scanf("%d",&n);
    if(n>0)
        printf("Hello World");
    n=n-1;
    if(n>0)
        printf("Hello World");
    n=n-1;
    if(n>0)
        printf("Hello World");
    n=n-1;
    .
    .
    .
    .
    //till when n becomes equal to zero.
}

Things are being repeated

How many times: n times

Statements repeating :

  • If condition
  • n=n-1;  (update)

While Loop

Syntax:

Initialization

while(condition){

      statement 1;

      statement 2;

      .....

}

So our Code for the problem

#include<stdio.h>
void main(){
    int n;
    scanf("%d",&n);
    while(n>0){
        printf("Hello World!");
        n=n-1;
    }
}

The Other Way

#include<stdio.h>
void main(){
    int n,t;
    scanf("%d",&n);
    t=0;
    while(t<n){
        printf("Hello World");
        t=t+1;
    }
}
Made with Slides.com